Skip to content

Commit 4ca289d

Browse files
authored
Feature/cleanup (#28)
* fix: flake8 compliant code * fix: fixed code formatting
1 parent 1f021c1 commit 4ca289d

8 files changed

+120
-182
lines changed

src/hydrodiy/data/containers.py

+48-67
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99
class Vector(object):
1010
""" Vector data container. Implements min, max and default values. """
1111

12-
def __init__(self, names, defaults=None, mins=None, maxs=None, \
13-
check_bounds=True, check_hitbounds=False, accept_nan=False):
12+
def __init__(self, names, defaults=None, mins=None,
13+
maxs=None, check_bounds=True, check_hitbounds=False,
14+
accept_nan=False):
1415

1516
# Set parameter names
1617
if names is None:
@@ -26,8 +27,8 @@ def __init__(self, names, defaults=None, mins=None, maxs=None, \
2627
self._check_bounds = bool(check_bounds)
2728
self._check_hitbounds = bool(check_hitbounds)
2829
if self._check_hitbounds and not self._check_bounds:
29-
raise ValueError("check_hitbounds cannot be True while "+\
30-
"check_bounds is False")
30+
raise ValueError("check_hitbounds cannot be True while " +
31+
"check_bounds is False")
3132
self._hitbounds = False
3233

3334
# Set number of parameters
@@ -36,40 +37,40 @@ def __init__(self, names, defaults=None, mins=None, maxs=None, \
3637
self._nval = nval
3738

3839
# Define names indexes
39-
self._names_index = {nm:i for nm, i \
40-
in zip(self._names, np.arange(nval))}
40+
self._names_index = {nm: i for nm, i
41+
in zip(self._names, np.arange(nval))}
4142

4243
# Set mins and maxs
4344
self._mins = -np.inf * np.ones(nval)
4445
self._maxs = np.inf * np.ones(nval)
4546

46-
if not mins is None:
47+
if mins is not None:
4748
# Just convert mins to proper array
4849
self._mins, _ = self.__checkvalues__(mins, False)
4950

50-
if not maxs is None:
51+
if maxs is not None:
5152
# Convert maxs to proper array and checks it is greater than mins
5253
maxs2, hit = self.__checkvalues__(maxs, True)
5354

5455
if hit:
55-
raise ValueError(("Expected maxs within [{0}, {1}]," +\
56-
" got {2}").format(self._mins, self._maxs, maxs))
56+
raise ValueError("Expected maxs within " +
57+
f"[{self._mins}, {self._maxs}], got {maxs}.")
5758
self._maxs = maxs2
5859

5960
# Set defaults values
60-
if not defaults is None:
61+
if defaults is not None:
6162
self._defaults, hit = self.__checkvalues__(defaults, True)
6263

6364
if hit:
64-
raise ValueError(("Expected defaults within [{0}, {1}]," +\
65-
" got {2}").format(self._mins, self._maxs, defaults))
65+
raise ValueError("Expected defaults within " +
66+
f"[{self._mins}, {self._maxs}]," +
67+
f"got {defaults}.")
6668
else:
6769
self._defaults = np.clip(np.zeros(nval), self._mins, self._maxs)
6870

6971
# Set values to defaults
7072
self._values = self._defaults.copy()
7173

72-
7374
@classmethod
7475
def from_dict(cls, dct):
7576
""" Create vector from dictionary """
@@ -87,16 +88,15 @@ def from_dict(cls, dct):
8788
maxs.append(dct["data"][i]["max"])
8889
values.append(dct["data"][i]["value"])
8990

90-
vect = Vector(names, defaults, mins, maxs, \
91-
check_bounds=bool(dct["check_bounds"]), \
92-
check_hitbounds=bool(dct["check_hitbounds"]),
93-
accept_nan=bool(dct["accept_nan"]))
91+
vect = Vector(names, defaults, mins, maxs,
92+
check_bounds=bool(dct["check_bounds"]),
93+
check_hitbounds=bool(dct["check_hitbounds"]),
94+
accept_nan=bool(dct["accept_nan"]))
9495
vect._hitbounds = bool(dct["hitbounds"])
9596
vect.values = values
9697

9798
return vect
9899

99-
100100
def __getattribute__(self, name):
101101
# Except _names and _hitbounds to avoid infinite recursion
102102
if name in ["_names", "_check_hitbounds", "_hitbounds"]:
@@ -108,7 +108,6 @@ def __getattribute__(self, name):
108108

109109
return super(Vector, self).__getattribute__(name)
110110

111-
112111
def __setattr__(self, name, value):
113112
# Except _names and _hitbounds to avoid infinite recursion
114113
if name in ["_names", "_check_hitbounds", "_hitbounds"]:
@@ -128,27 +127,24 @@ def __setattr__(self, name, value):
128127
or (value > self._maxs[idx])
129128

130129
# Store clipped value
131-
self.values[idx] = min(max(value, \
132-
self.mins[idx]), self.maxs[idx])
130+
self.values[idx] = min(max(value, self.mins[idx]),
131+
self.maxs[idx])
133132

134133
else:
135134
super(Vector, self).__setattr__(name, value)
136135

137-
138136
def __str__(self):
139-
return "vector ["+", ".join(["{0}:{1:3.3e}".format(key, self[key]) \
140-
for key in self.names]) + "]"
141-
137+
return "vector ["+", ".join(["{0}:{1:3.3e}".format(key, self[key])
138+
for key in self.names]) + "]"
142139

143140
def __checkvalues__(self, val, check_hitbounds):
144141
""" Check vector is of proper length and contains no nan"""
145142

146143
val = np.atleast_1d(val).flatten().astype(np.float64)
147144

148145
if val.shape[0] != self.nval:
149-
raise ValueError("Expected vector of length "+\
150-
"{0}, got {1}".format(\
151-
self.nval, val.shape[0]))
146+
raise ValueError("Expected vector of length "
147+
+ f"{self.nval}, got {val.shape[0]}.")
152148

153149
if np.any(np.isnan(val)) and not self._accept_nan:
154150
raise ValueError("Cannot process values with NaN")
@@ -163,126 +159,111 @@ def __checkvalues__(self, val, check_hitbounds):
163159

164160
return val, hit
165161

166-
167162
def __setitem__(self, key, value):
168-
if not key in self._names_index:
169-
raise ValueError("Expected key in {0}, got {1}".format(\
170-
self._names, key))
163+
if key not in self._names_index:
164+
raise ValueError(f"Expected key in {self._names}, got {key}.")
171165

172166
setattr(self, key, value)
173167

174-
175168
def __getitem__(self, key):
176-
if not key in self._names_index:
177-
raise ValueError("Expected key in {0}, got {1}".format(\
178-
self._names, key))
169+
if key not in self._names_index:
170+
raise ValueError(f"Expected key in {self._names}, got {key}.")
179171

180172
return getattr(self, key)
181173

182-
183174
@property
184175
def nval(self):
185176
""" Number of values in vector """
186177
return self._nval
187178

188-
189179
@property
190180
def check_bounds(self):
191181
""" Are the bounds checked or not """
192182
return self._check_bounds
193183

194-
195184
@property
196185
def check_hitbounds(self):
197186
""" Is the hit to bounds checked or not """
198187
return self._check_hitbounds
199188

200-
201189
@property
202190
def hitbounds(self):
203191
""" Has the boundaries been reached when setting data ? """
204192
return self._hitbounds
205193

206-
207194
@property
208195
def accept_nan(self):
209196
""" Accept nan values """
210197
return self._accept_nan
211198

212-
213199
@property
214200
def names(self):
215201
""" Names of vector elements """
216202
return self._names
217203

218-
219204
@property
220205
def mins(self):
221206
""" Minimum values of vector elements """
222207
return self._mins
223208

224-
225209
@property
226210
def maxs(self):
227211
""" Maximum values of vector elements """
228212
return self._maxs
229213

230-
231214
@property
232215
def defaults(self):
233216
""" Default values of vector elements """
234217
return self._defaults
235218

236-
237219
@property
238220
def values(self):
239221
""" Get data """
240222
return self._values
241223

242-
243224
@values.setter
244225
def values(self, val):
245226
""" Set data """
246-
self._values, self._hitbounds = self.__checkvalues__(val, \
247-
self._check_hitbounds)
248-
227+
ck = self._check_hitbounds
228+
self._values, self._hitbounds = self.__checkvalues__(val, ck)
249229

250230
def reset(self):
251231
""" Reset vector values to defaults """
252232
self.values = self.defaults
253233

254-
255234
def clone(self):
256235
""" Clone the vector """
257-
clone = Vector(self.names, self.defaults, self.mins, \
258-
self.maxs, self.check_hitbounds)
236+
clone = Vector(self.names, self.defaults, self.mins,
237+
self.maxs, self.check_hitbounds)
259238

260239
clone.values = self.values.copy()
261240

262241
return clone
263242

264-
265243
def to_dict(self):
266244
""" Write vector data to json format """
267245

268-
dct = {"nval": self.nval, "hitbounds":self.hitbounds, \
269-
"check_bounds":self.check_bounds, \
270-
"check_hitbounds":self.check_hitbounds, \
271-
"accept_nan":self.accept_nan, \
272-
"data":[]}
246+
dct = {
247+
"nval": self.nval,
248+
"hitbounds": self.hitbounds,
249+
"check_bounds": self.check_bounds,
250+
"check_hitbounds": self.check_hitbounds,
251+
"accept_nan": self.accept_nan,
252+
"data": []
253+
}
273254

274255
for i in range(self.nval):
275-
elem = {"name":self.names[i], \
276-
"value":self.values[i], \
277-
"min":self.mins[i], \
278-
"max":self.maxs[i], \
279-
"default":self.defaults[i]}
256+
elem = {
257+
"name": self.names[i],
258+
"value": self.values[i],
259+
"min": self.mins[i],
260+
"max": self.maxs[i],
261+
"default": self.defaults[i]
262+
}
280263
dct["data"].append(elem)
281264

282265
return dct
283266

284-
285267
def to_series(self):
286268
""" Write vector data to json format """
287269
return pd.Series(self.values, index=self.names)
288-

0 commit comments

Comments
 (0)