-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhys_unit.py
89 lines (73 loc) · 2.76 KB
/
Phys_unit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from copy import deepcopy
#________________________Phys_unit________________________
#defines a real value with unit and optional description
#e.g.: R1 = Phys_unit(104, "Ω", "Resistor R1")
class Phys_unit():
unit: str
descr: str
def __init__(self, value, unit, description=""):
if issubclass(type(value), Phys_unit) == True:
self.value = value.value
else:
self.value = value
self.unit = unit
self.descr = description
def __call__(self):
return self.value
def __str__(self):
return f"{self.descr} : {self.value} {self.unit}"
def __add__(self, other):
if issubclass(type(other), Phys_unit) == True:
ret_instance = deepcopy(self)
ret_instance.value = self.value + other.value
return ret_instance
else:
return self.value + other
def __radd__(self, other):
return other+self.value
def __sub__(self, other):
if issubclass(type(other), Phys_unit) == True:
ret_instance = deepcopy(self)
ret_instance.value = self.value - other.value
return ret_instance
else:
return self.value - other
def __rsub__(self, other):
return other-self.value
def __mul__(self, other):
ret_instance = deepcopy(self)
if issubclass(type(other), Phys_unit) == True:
ret_instance.value = self.value * other.value
return ret_instance
else:
ret_instance.value = self.value * other
return ret_instance
def __rmul__(self, other):
return self.__mul__(other)
def __truediv__(self, other):
ret_instance = deepcopy(self)
if issubclass(type(other), Phys_unit) == True:
ret_instance.value = self.value / other.value
return ret_instance
else:
ret_instance.value = self.value / other
return ret_instance
def __rtruediv__(self, other):
ret_instance = deepcopy(self)
ret_instance.value = other / self.value
return ret_instance
def __pow__(self, other):
if issubclass(type(other), Phys_unit) == True:
ret_instance = deepcopy(self)
ret_instance.value = self.value ** other.value
return ret_instance
else:
ret_instance = deepcopy(self)
ret_instance.value = self.value ** other
return ret_instance
def __rpow__(self, other):
return other**self.value
def __round__(self, other):
ret_instance = deepcopy(self)
ret_instance.value = round(self.value, other)
return ret_instance