Skip to content

Commit dc8c6e3

Browse files
committed
EHN: New featues and Improvements
1 parent 9c31bb7 commit dc8c6e3

File tree

1 file changed

+104
-27
lines changed

1 file changed

+104
-27
lines changed

labfis.py

Lines changed: 104 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,115 @@
11
from math import floor, ceil, trunc, log, log10
22
from numbers import Number
33

4+
class LabFloatError(Exception):
5+
def __init__(self, *args):
6+
if args:
7+
if args[0] == 0:
8+
self.message = "Essa operação não é suportado nesta classe, apenas as operações mencionadas na apostila de Laboratório de Física 2 do IFSC."
9+
elif args[0] == 1:
10+
self.message = "Too many args in list, expected '[...,[val,err],...]' got '{0}'".format(args[1])
11+
elif isinstance(args[0],str):
12+
self.message = "{0}{1}".format(args[0],args[1])
13+
else:
14+
self.message = None
15+
16+
def __str__(self):
17+
if self.message:
18+
return '{0}'.format(self.message)
19+
else:
20+
return 'LabFloatError has been raised'
21+
422
class labfloat:
5-
errsupport = "Essa operação não é suportado nesta classe, apenas as operações mencionadas na apostila de Laboratório de Física 2 do IFSC."
23+
def __init__(self, *args, **kwargs):
24+
mean = kwargs.get('mean',0.0)
25+
uncertainty = kwargs.get('uncertainty',0.0)
26+
27+
if args:
28+
if len(args) == 1:
29+
mean = args[0]
30+
elif len(args) == 2:
31+
mean = args[0]
32+
uncertainty = args[1]
33+
else:
34+
raise LabFloatError("Too many arguments, expected (val,err), got: ",args)
635

7-
def __init__(self, mean = 0.0, uncertainty = 0.0):
836
self.mean = float(mean)
9-
self.uncertainty = float(abs(uncertainty))
37+
self.uncertainty = abs(float(uncertainty))
38+
39+
@classmethod
40+
def list(cls,listargs):
41+
listlabfloat = []
42+
for j in range(len(listargs)):
43+
if len(listargs[j]) == 2:
44+
listlabfloat += [cls(listargs[j][0],listargs[j][1])]
45+
elif len(listargs[j]) == 1:
46+
listlabfloat += [cls(listargs[j][0])]
47+
elif len(listargs[j]) > 2:
48+
raise LabFloatError(1,listargs[j])
49+
return(listlabfloat)
50+
51+
def format(self):
52+
su = "%.16f" % self.uncertainty
53+
i = su.find(".")
54+
if i == -1:
55+
r = - len(su) + 1
56+
m = round(self.mean, r)
57+
u = round(self.uncertainty, r)
58+
return((m,u))
59+
else:
60+
r = -i
61+
r += 1
62+
for digit in su:
63+
if digit == "0":
64+
r += 1
65+
elif digit != ".":
66+
m = round(self.mean, r)
67+
u = round(self.uncertainty, r)
68+
return((m,u))
1069

11-
def __str__(self):
70+
m = round(self.mean, r)
71+
u = round(self.uncertainty, r)
72+
return((m,u))
73+
74+
def split(self):
1275
if self.uncertainty == 0:
13-
return("{:g}".format(self.mean))
76+
return(["{:g}".format(self.mean)])
77+
else:
78+
m, u = self.format()
79+
return(["{:g}".format(m),"{:g}".format(u)])
80+
81+
def tex(self):
82+
val = self.split()
83+
if len(val) == 1:
84+
m = val[0].split("e")
85+
if len(m) > 1:
86+
m = m[0]+"\cdot 10^{"+m[1]+"}"
87+
else:
88+
m = m[0]
89+
return("{0}".format(m))
1490
else:
15-
su = "%.16f" % self.uncertainty
16-
i = su.find(".")
17-
if i == -1:
18-
r = - len(su) + 1
19-
m = round(self.mean, r)
20-
u = round(self.uncertainty, r)
21-
return("({:g} ± {:g})".format(m, u))
91+
m,u = val
92+
m = m.split("e")
93+
u = u.split("e")
94+
if len(m) > 1:
95+
m = m[0]+"\cdot 10^{"+m[1]+"}"
2296
else:
23-
r = -i
24-
r += 1
25-
for digit in su:
26-
if digit == "0":
27-
r += 1
28-
elif digit != ".":
29-
m = round(self.mean, r)
30-
u = round(self.uncertainty, r)
31-
return("({:g} ± {:g})".format(m, u))
97+
m = m[0]
98+
if len(u) > 1:
99+
u = u[0]+"\cdot 10^{"+u[1]+"}"
100+
else:
101+
u = u[0]
102+
return("({0}\, \pm \,{1})".format(m, u))
32103

33-
m = round(self.mean, r)
34-
u = round(self.uncertainty, r)
35-
return("({:g} ± {:g})".format(m, u))
104+
def __str__(self):
105+
val = self.split()
106+
if len(val) == 1:
107+
return("{0}".format(val[0]))
108+
else:
109+
return("({0} ± {1})".format(*val))
110+
111+
def __repr__(self):
112+
return self.__str__()
36113

37114
def __pos__(self):
38115
return self
@@ -156,13 +233,13 @@ def __rtruediv__(self, other):
156233

157234
def __pow__(self, other):
158235
if isinstance(other, labfloat):
159-
raise Exception(self.errsupport)
236+
raise LabFloatError(0)
160237
if isinstance(other, Number):
161238
return labfloat(self.mean ** other, other * self.mean ** (other-1) * self.uncertainty)
162239

163240
def __rpow__(self, other):
164241
if isinstance(other, labfloat):
165-
raise Exception(self.errsupport)
242+
raise LabFloatError(0)
166243
if isinstance(other, (float, int, hex, oct, complex)):
167244
return labfloat(other ** self.mean, other ** self.mean * log(other) * self.uncertainty)
168245

@@ -182,4 +259,4 @@ def __oct__(self):
182259
return oct(self.mean)
183260

184261
def __hex__(self):
185-
return hex(self.mean)
262+
return hex(self.mean)

0 commit comments

Comments
 (0)