Skip to content

Commit 3e958ea

Browse files
committed
Getters and Setters
1 parent cbd234d commit 3e958ea

File tree

1 file changed

+67
-59
lines changed

1 file changed

+67
-59
lines changed

labfis/uncertainty.py

Lines changed: 67 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ def __init__(self, *args, **kwargs):
7373
elif len(args) > 2:
7474
raise LabFloatError(1, args)
7575

76-
self.mean = float(mean)
77-
self.uncertainty = abs(float(uncertainty))
76+
self._mean = float(mean)
77+
self._uncertainty = abs(float(uncertainty))
7878

7979
@classmethod
8080
def list(cls, listargs):
@@ -92,12 +92,12 @@ def list(cls, listargs):
9292
return(listlabfloat)
9393

9494
def format(self):
95-
su = "%.16f" % self.uncertainty
95+
su = "%.16f" % self._uncertainty
9696
i = su.find(".")
9797
if i == -1:
9898
r = - len(su) + 1
99-
m = round(self.mean, r)
100-
u = round(self.uncertainty, r)
99+
m = round(self._mean, r)
100+
u = round(self._uncertainty, r)
101101
return((m, u))
102102
else:
103103
r = -i
@@ -106,16 +106,16 @@ def format(self):
106106
if su[j] == "0":
107107
r += 1
108108
elif su[j+1] == "9" and r > 0:
109-
m = round(self.mean, r-1)
110-
u = round(self.uncertainty, r)
109+
m = round(self._mean, r-1)
110+
u = round(self._uncertainty, r)
111111
return((m, u))
112112
elif su[j] != ".":
113-
m = round(self.mean, r)
114-
u = round(self.uncertainty, r)
113+
m = round(self._mean, r)
114+
u = round(self._uncertainty, r)
115115
return((m, u))
116116

117-
m = round(self.mean, r)
118-
u = round(self.uncertainty, r)
117+
m = round(self._mean, r)
118+
u = round(self._uncertainty, r)
119119
return((m, u))
120120

121121
def split(self):
@@ -132,10 +132,10 @@ def tex(self, *args, **kwargs):
132132
else:
133133
precision = [args[0], args[0]]
134134

135-
if self.uncertainty == 0:
135+
if self._uncertainty == 0:
136136
if precision:
137137
precision[0] = str(precision[0])
138-
m = eval("'{:."+precision[0]+"e}'.format(self.mean)")
138+
m = eval("'{:."+precision[0]+"e}'.format(self._mean)")
139139
else:
140140
m = self.split()[0]
141141
m = m.split("e")
@@ -170,72 +170,80 @@ def __str__(self):
170170
def __repr__(self):
171171
return self.__str__()
172172

173+
@property
174+
def mean(self):
175+
return self._mean
176+
177+
@property
178+
def uncertainty(self):
179+
return self._uncertainty
180+
173181
def __getitem__(self, idx):
174-
vals = [self.mean, self.uncertainty]
182+
vals = (self.uncertainty, self.mean)
175183
return vals[idx]
176184

177185
def __pos__(self):
178186
return self
179187

180188
def __neg__(self):
181-
return labfloat(-self.mean, self.uncertainty)
189+
return labfloat(-self._mean, self._uncertainty)
182190

183191
def __abs__(self):
184-
return labfloat(abs(self.mean), self.uncertainty)
192+
return labfloat(abs(self._mean), self._uncertainty)
185193

186194
def __round__(self, n):
187-
return labfloat(round(self.mean, n), round(self.uncertainty, n))
195+
return labfloat(round(self._mean, n), round(self._uncertainty, n))
188196

189197
def __floor__(self):
190-
return labfloat(floor(self.mean), floor(self.uncertainty))
198+
return labfloat(floor(self._mean), floor(self._uncertainty))
191199

192200
def __ceil__(self):
193-
return labfloat(ceil(self.mean), ceil(self.uncertainty))
201+
return labfloat(ceil(self._mean), ceil(self._uncertainty))
194202

195203
def __trunc__(self):
196-
return labfloat(trunc(self.mean), trunc(self.uncertainty))
204+
return labfloat(trunc(self._mean), trunc(self._uncertainty))
197205

198206
def __eq__(self, other):
199207
if isinstance(other, labfloat):
200-
return abs(self.mean - other.mean) < 2 * (self.uncertainty + other.uncertainty)
208+
return abs(self._mean - other.mean) < 2 * (self._uncertainty + other.uncertainty)
201209
if isinstance(other, Number):
202-
return abs(self.mean - other) < 2 * self.uncertainty
210+
return abs(self._mean - other) < 2 * self._uncertainty
203211

204212
def __ne__(self, other):
205213
if isinstance(other, labfloat):
206-
return abs(self.mean - other.mean) > 3 * (self.uncertainty + other.uncertainty)
214+
return abs(self._mean - other.mean) > 3 * (self._uncertainty + other.uncertainty)
207215
if isinstance(other, Number):
208-
return abs(self.mean - other) > 3 * self.uncertainty
216+
return abs(self._mean - other) > 3 * self._uncertainty
209217

210218
def __lt__(self, other):
211219
if isinstance(other, labfloat):
212-
return self.mean + self.uncertainty < other.mean - other.uncertainty
220+
return self._mean + self._uncertainty < other.mean - other.uncertainty
213221
if isinstance(other, Number):
214-
return self.mean + self.uncertainty < other
222+
return self._mean + self._uncertainty < other
215223

216224
def __gt__(self, other):
217225
if isinstance(other, labfloat):
218-
return self.mean - self.uncertainty > other.mean + other.uncertainty
226+
return self._mean - self._uncertainty > other.mean + other.uncertainty
219227
if isinstance(other, Number):
220-
return self.mean - self.uncertainty > other
228+
return self._mean - self._uncertainty > other
221229

222230
def __le__(self, other):
223231
if isinstance(other, labfloat):
224-
return self.mean + self.uncertainty <= other.mean + other.uncertainty
232+
return self._mean + self._uncertainty <= other.mean + other.uncertainty
225233
if isinstance(other, Number):
226-
return self.mean + self.uncertainty <= other
234+
return self._mean + self._uncertainty <= other
227235

228236
def __ge__(self, other):
229237
if isinstance(other, labfloat):
230-
return self.mean - self.uncertainty >= other.mean - other.uncertainty
238+
return self._mean - self._uncertainty >= other.mean - other.uncertainty
231239
if isinstance(other, Number):
232-
return self.mean - self.uncertainty >= other
240+
return self._mean - self._uncertainty >= other
233241

234242
def __add__(self, other):
235243
if isinstance(other, labfloat):
236-
return labfloat(self.mean + other.mean, sqrt(self.uncertainty ** 2 + other.uncertainty ** 2))
244+
return labfloat(self._mean + other.mean, sqrt(self._uncertainty ** 2 + other.uncertainty ** 2))
237245
if isinstance(other, Number):
238-
return labfloat(self.mean + other, self.uncertainty)
246+
return labfloat(self._mean + other, self._uncertainty)
239247

240248
def __radd__(self, other):
241249
return self.__add__(other)
@@ -245,24 +253,24 @@ def __iadd__(self, other):
245253

246254
def __sub__(self, other):
247255
if isinstance(other, labfloat):
248-
return labfloat(self.mean - other.mean, sqrt(self.uncertainty ** 2 + other.uncertainty ** 2))
256+
return labfloat(self._mean - other.mean, sqrt(self._uncertainty ** 2 + other.uncertainty ** 2))
249257
if isinstance(other, Number):
250-
return labfloat(self.mean - other, self.uncertainty)
258+
return labfloat(self._mean - other, self._uncertainty)
251259

252260
def __rsub__(self, other):
253261
if isinstance(other, labfloat):
254-
return labfloat(other.mean - self.mean, sqrt(other.uncertainty ** 2 + self.uncertainty ** 2))
262+
return labfloat(other.mean - self._mean, sqrt(other.uncertainty ** 2 + self._uncertainty ** 2))
255263
if isinstance(other, Number):
256-
return labfloat(other - self.mean, self.uncertainty)
264+
return labfloat(other - self._mean, self._uncertainty)
257265

258266
def __isub__(self, other):
259267
return self.__sub__(other)
260268

261269
def __mul__(self, other):
262270
if isinstance(other, labfloat):
263-
return labfloat(self.mean * other.mean, sqrt((other.mean * self.uncertainty) ** 2 + (self.mean * other.uncertainty) ** 2))
271+
return labfloat(self._mean * other.mean, sqrt((other.mean * self._uncertainty) ** 2 + (self._mean * other.uncertainty) ** 2))
264272
if isinstance(other, Number):
265-
return labfloat(self.mean * other, abs(other * self.uncertainty))
273+
return labfloat(self._mean * other, abs(other * self._uncertainty))
266274

267275
def __rmul__(self, other):
268276
return self.__mul__(other)
@@ -272,9 +280,9 @@ def __imul__(self, other):
272280

273281
def __div__(self, other):
274282
if isinstance(other, labfloat):
275-
return labfloat(self.mean / other.mean, sqrt((self.uncertainty / other.mean) ** 2 + (self.mean * other.uncertainty / (other.mean ** 2)) ** 2))
283+
return labfloat(self._mean / other.mean, sqrt((self._uncertainty / other.mean) ** 2 + (self._mean * other.uncertainty / (other.mean ** 2)) ** 2))
276284
if isinstance(other, Number):
277-
return labfloat(self.mean / other, abs(self.uncertainty / other))
285+
return labfloat(self._mean / other, abs(self._uncertainty / other))
278286

279287
def __truediv__(self, other):
280288
return self.__div__(other)
@@ -287,24 +295,24 @@ def __itruediv__(self, other):
287295

288296
def __rdiv__(self, other):
289297
if isinstance(other, labfloat):
290-
return labfloat(other.mean / self.mean, sqrt((other.uncertainty / self.mean) ** 2 + (other.mean * self.uncertainty / (self.mean ** 2)) ** 2))
298+
return labfloat(other.mean / self._mean, sqrt((other.uncertainty / self._mean) ** 2 + (other.mean * self._uncertainty / (self._mean ** 2)) ** 2))
291299
if isinstance(other, Number):
292-
return labfloat(other / self.mean, abs(other * self.uncertainty / self.mean ** 2))
300+
return labfloat(other / self._mean, abs(other * self._uncertainty / self._mean ** 2))
293301

294302
def __rtruediv__(self, other):
295303
return self.__rdiv__(other)
296304

297305
def __pow__(self, other):
298306
if isinstance(other, labfloat):
299-
return labfloat(self.mean ** other.mean, sqrt((other.mean * self.mean ** (other.mean - 1) * self.uncertainty) ** 2 + (self.mean ** other.mean * log(abs(self.mean)) * other.uncertainty) ** 2))
307+
return labfloat(self._mean ** other.mean, sqrt((other.mean * self._mean ** (other.mean - 1) * self._uncertainty) ** 2 + (self._mean ** other.mean * log(abs(self._mean)) * other.uncertainty) ** 2))
300308
if isinstance(other, Number):
301-
return labfloat(self.mean ** other, abs(other * self.mean ** (other - 1) * self.uncertainty))
309+
return labfloat(self._mean ** other, abs(other * self._mean ** (other - 1) * self._uncertainty))
302310

303311
def __rpow__(self, other):
304312
if isinstance(other, labfloat):
305-
return labfloat(other.mean ** self.mean, sqrt((self.mean * other.mean ** (self.mean - 1) * other.uncertainty) ** 2 + (other.mean ** self.mean * log(abs(other.mean)) * self.uncertainty) ** 2))
313+
return labfloat(other.mean ** self._mean, sqrt((self._mean * other.mean ** (self._mean - 1) * other.uncertainty) ** 2 + (other.mean ** self._mean * log(abs(other.mean)) * self._uncertainty) ** 2))
306314
if isinstance(other, Number):
307-
return labfloat(other ** self.mean, abs(other ** self.mean * log(abs(other)) * self.uncertainty))
315+
return labfloat(other ** self._mean, abs(other ** self._mean * log(abs(other)) * self._uncertainty))
308316

309317
def __ipow__(self, other):
310318
return self.__pow__(other)
@@ -313,34 +321,34 @@ def sqrt(self):
313321
return self.__pow__(0.5)
314322

315323
def cos(self):
316-
return labfloat(cos(self.mean), abs(-(sin(self.mean)) * self.uncertainty))
324+
return labfloat(cos(self._mean), abs(-(sin(self._mean)) * self._uncertainty))
317325

318326
def sin(self):
319-
return labfloat(sin(self.mean), abs(cos(self.mean) * self.uncertainty))
327+
return labfloat(sin(self._mean), abs(cos(self._mean) * self._uncertainty))
320328

321329
def tan(self):
322-
return labfloat(tan(self.mean), sqrt((cos(self.mean) ** -4) * self.uncertainty ** 2))
330+
return labfloat(tan(self._mean), sqrt((cos(self._mean) ** -4) * self._uncertainty ** 2))
323331

324332
def arcsin(self):
325-
return labfloat(asin(self.mean), self.uncertainty/sqrt(1 - self.mean ** 2))
333+
return labfloat(asin(self._mean), self._uncertainty/sqrt(1 - self._mean ** 2))
326334

327335
def arccos(self):
328-
return labfloat(acos(self.mean), abs(-self.uncertainty/sqrt(1 - self.mean ** 2)))
336+
return labfloat(acos(self._mean), abs(-self._uncertainty/sqrt(1 - self._mean ** 2)))
329337

330338
def arctan(self):
331-
return labfloat(atan(self.mean), self.uncertainty/(1 + self.mean ** 2))
339+
return labfloat(atan(self._mean), self._uncertainty/(1 + self._mean ** 2))
332340

333341
def __int__(self):
334-
return int(self.mean)
342+
return int(self._mean)
335343

336344
def __float__(self):
337-
return float(self.mean)
345+
return float(self._mean)
338346

339347
def __complex__(self):
340-
return complex(self.mean)
348+
return complex(self._mean)
341349

342350
def __oct__(self):
343-
return oct(self.mean)
351+
return oct(self._mean)
344352

345353
def __hex__(self):
346-
return hex(self.mean)
354+
return hex(self._mean)

0 commit comments

Comments
 (0)