diff --git a/storage.py b/storage.py index f363fb3..4e8ecd1 100644 --- a/storage.py +++ b/storage.py @@ -8,14 +8,15 @@ import h5py import collections import logging -log = logging.getLogger(__name__) # __name__ is "foo.bar" here +log = logging.getLogger(__name__) def unwrapArray(a,recursive=True,readH5pyDataset=True): """ This function takes an object (like a dictionary) and recursively - unwraps it solving many issues like the fact that many objects are - packaged as 0d array - This funciton has also some specific hack for handling h5py limit to - handle for example the None object or the numpy unicode ... + unwraps it solving issues like: + * the fact that many objects are packaged as 0d array + This funciton has also some specific hack for handling h5py limits: + * handle the None python object + * numpy unicode ... """ # is h5py dataset convert to array if isinstance(a,h5py.Dataset) and readH5pyDataset: a = a[...] @@ -140,11 +141,16 @@ class DataStorage(dict): To initialize it: - data = DataStorage( dict( a=(1,2,3),b="add"),filename='store.npz' ) + data = DataStorage( a=(1,2,3),b="add",filename='store.npz' ) - data = DataStorage( a=(1,2,3), b="add" ) + # recursively by default + # data.a will be a DataStorage instance + data = DataStorage( a=dict( b = 1)) ); - reads from file if it exists + # data.a will be a dictionary + data = DataStorage( a=dict( b = 1),recursive=False ) + + # reads from file if it exists data = DataStorage( 'mysaveddata.npz' ) ; DOES NOT READ FROM FILE (even if it exists)!! @@ -156,6 +162,7 @@ class DataStorage(dict): def __init__(self,*args,filename='data_storage.npz',recursive=True,**kwargs): # self.filename = kwargs.pop('filename',"data_storage.npz") self.filename = filename + self._recursive = recursive # interpret kwargs as dict if there are if len(kwargs) != 0: fileOrDict = dict(kwargs) @@ -164,6 +171,7 @@ class DataStorage(dict): else: fileOrDict = dict() + d = dict(); # data dictionary if isinstance(fileOrDict,dict): d = fileOrDict @@ -194,8 +202,9 @@ class DataStorage(dict): def __setattr__(self, key, value): """ allows to add fields with data.test=4 """ - #print("__setattr__") - if isinstance(value,(dict,collections.OrderedDict)): value = DataStorage(value) + # check if attr exists is essential (or it fails when defining an instance) + if hasattr(self,"_recursive") and self._recursive and \ + isinstance(value,(dict,collections.OrderedDict)): value = DataStorage(value) super().__setitem__(key, value) super().__setattr__(key,value) @@ -216,6 +225,7 @@ class DataStorage(dict): fmt = "%%%ds %%s" % (nchars) s = ["DataStorage obj containing (sorted): ",] for k in keys: + if k[0] == "_": continue obj = self[k] if isinstance(obj,np.ndarray): value_str = "array, size %s, type %s"% ("x".join(map(str,obj.shape)),obj.dtype) @@ -236,6 +246,7 @@ class DataStorage(dict): def keys(self): keys = list(super().keys()) keys = [k for k in keys if k != 'filename' ] + keys = [k for k in keys if k[0] != '_' ] return keys def save(self,fname=None):