datastock 0.0.33__py3-none-any.whl → 0.0.34__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
datastock/_class0.py CHANGED
@@ -17,7 +17,17 @@ class DataStock0(object):
17
17
  def __init__(self):
18
18
  self.__object = object()
19
19
 
20
- def to_dict(self, flatten=None, sep=None, asarray=None, with_types=None):
20
+ def to_dict(
21
+ self,
22
+ flatten=None,
23
+ sep=None,
24
+ asarray=None,
25
+ excluded=None,
26
+ # copy vs ref
27
+ copy=None,
28
+ # dtypes
29
+ returnas=None,
30
+ ):
21
31
  """ Return a flat dict view of the object's attributes
22
32
 
23
33
  Useful for:
@@ -34,51 +44,28 @@ class DataStock0(object):
34
44
 
35
45
  Return
36
46
  ------
37
- dout : dict
38
- dict containing all the objects attributes (optionally flattened)
47
+ returnas: str
48
+ - 'types': a dict with only the types
49
+ - 'values': a dict with the values
50
+ - 'both': 2 seperate dicts, one with types, one with values
51
+ - 'blended': a dict with both types and values (save compatible)
39
52
 
40
53
  """
41
54
 
42
- # ------------
43
- # check inputs
44
-
45
- flatten = _generic_check._check_var(
46
- flatten, 'flatten',
47
- default=False,
48
- types=bool,
55
+ return _generic_utils.to_dict(
56
+ self,
57
+ flatten=flatten,
58
+ sep=sep,
59
+ asarray=asarray,
60
+ excluded=excluded,
61
+ # copy vs ref
62
+ copy=copy,
63
+ # dtypes
64
+ returnas=returnas,
49
65
  )
50
66
 
51
- # ---------------------------
52
- # Get list of dict attributes
53
-
54
- dout = copy.deepcopy({
55
- k0: getattr(self, k0)
56
- for k0 in dir(self)
57
- if isinstance(getattr(self, k0), dict)
58
- and k0 != '__dict__'
59
- and '__dlinks' not in k0
60
- and not (
61
- hasattr(self.__class__, k0)
62
- and isinstance(getattr(self.__class__, k0), property)
63
- )
64
- })
65
-
66
- # -------------------
67
- # optional flattening
68
-
69
- if flatten is True:
70
- dout = _generic_utils.flatten_dict(
71
- dout,
72
- parent_key=None,
73
- sep=sep,
74
- asarray=asarray,
75
- with_types=with_types,
76
- )
77
-
78
- return dout
79
-
80
67
  @classmethod
81
- def from_dict(cls, din=None, reshape=None, sep=None):
68
+ def from_dict(cls, din=None, isflat=None, sep=None):
82
69
  """ Populate the instances attributes using an input dict
83
70
 
84
71
  The input dict must be properly formatted
@@ -92,7 +79,7 @@ class DataStock0(object):
92
79
  The separator that was used to format fd keys (cf. self.to_dict())
93
80
  """
94
81
 
95
- if reshape is True:
82
+ if isflat is True:
96
83
  din = _generic_utils.reshape_dict(din, sep=sep)
97
84
 
98
85
  # ---------------------
@@ -109,12 +96,19 @@ class DataStock0(object):
109
96
 
110
97
  return obj
111
98
 
112
- def copy(self):
99
+ def copy(self, excluded=None, sep=None):
113
100
  """ Return another instance of the object, with the same attributes
114
101
 
115
102
  If deep=True, all attributes themselves are also copies
116
103
  """
117
- return self.__class__.from_dict(din=copy.deepcopy(self.to_dict()))
104
+ return self.__class__.from_dict(
105
+ din=self.to_dict(
106
+ flatten=False,
107
+ excluded=excluded,
108
+ returnas='values',
109
+ copy=True,
110
+ )
111
+ )
118
112
 
119
113
  def get_nbytes(self):
120
114
  """ Compute and return the object size in bytes (i.e.: octets)
@@ -129,15 +123,15 @@ class DataStock0(object):
129
123
  dsize : dict
130
124
  A dictionnary giving the size of each attribute
131
125
  """
132
- dd = self.to_dict(flatten=True)
126
+ dd = self.to_dict(flatten=True, copy=False, returnas='values')
133
127
  dsize = dd.fromkeys(dd.keys(), 0)
134
128
  total = 0
135
129
  for k0, v0 in dd.items():
136
130
  try:
137
131
  dsize[k0] = np.asarray(v0).nbytes
138
- total += dsize[k0]
139
132
  except Exception as err:
140
133
  dsize[k0] = str(err)
134
+ total += dsize[k0]
141
135
  return total, dsize
142
136
 
143
137
 
@@ -145,16 +139,18 @@ class DataStock0(object):
145
139
  # operator overloading
146
140
  #############################
147
141
 
148
- def __eq__(self, obj, returnas=None, verb=None):
142
+
143
+ def __eq__(self, obj, excluded=None, returnas=None, verb=None):
149
144
  return _generic_utils.compare_obj(
150
145
  obj0=self,
151
146
  obj1=obj,
147
+ excluded=excluded,
152
148
  returnas=returnas,
153
149
  verb=verb,
154
150
  )
155
151
 
156
- def __neq__(self, obj, returnas=None, verb=None):
157
- return not self.__eq__(obj, returnas=returnas, verb=verb)
152
+ def __neq__(self, obj, excluded=None, returnas=None, verb=None):
153
+ return not self.__eq__(obj, excluded=excluded, returnas=returnas, verb=verb)
158
154
 
159
155
  def __hash__(self, *args, **kargs):
160
156
  return self.__object.__hash__(*args, **kargs)
@@ -172,14 +168,30 @@ class DataStock0(object):
172
168
  return_pfe=False,
173
169
  ):
174
170
 
171
+ lsep = ['.', '-', '_', ',', ';', '~', '?']
172
+ if sep is None:
173
+ for ss in lsep:
174
+ c0 = (
175
+ any([ss in k0 for k0 in self.ddata.keys()])
176
+ or any([ss in k0 for k0 in self.dref.keys()])
177
+ or any([
178
+ any([ss in k0 for k0 in self.dobj[k0].keys()])
179
+ for k0 in self._dobj.keys()
180
+ ])
181
+ )
182
+ if not c0:
183
+ sep = ss
184
+ break
185
+
175
186
  # call parent method
176
187
  return _saveload.save(
177
188
  dflat=self.to_dict(
178
189
  flatten=True,
179
190
  sep=sep,
180
191
  asarray=True,
181
- with_types=True,
192
+ returnas='blended',
182
193
  ),
194
+ sep=sep,
183
195
  path=path,
184
196
  name=name,
185
197
  clsname=self.__class__.__name__,
@@ -196,4 +208,4 @@ class DataStock0(object):
196
208
 
197
209
  __all__ = [
198
210
  sorted([k0 for k0 in locals() if k0.startswith('DataStock')])[-1]
199
- ]
211
+ ]