-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunit.py
More file actions
333 lines (278 loc) · 11.3 KB
/
Copy pathunit.py
File metadata and controls
333 lines (278 loc) · 11.3 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import sys
class BaseUnit:
def __init__(self, t, l, m, i, T, n, L):
self.t = t
self.l = l
self.m = m
self.i = i
self.T = T
self.n = n
self.L = L
def __repr__(self):
string = ""
if self.t:
string += 's'
if self.t != 1:
string += str(self.t)
string += " "
if self.l:
string += 'm'
if self.l != 1:
string += str(self.l)
string += " "
if self.m:
string += 'kg'
if self.m != 1:
string += str(self.m)
string += " "
if self.i:
string += 'A'
if self.i != 1:
string += str(self.i)
string += " "
if self.T:
string += 'K'
if self.T != 1:
string += str(self.T)
string += " "
if self.n:
string += 'mol'
if self.n != 1:
string += str(self.n)
string += " "
if self.L:
string += 'cd'
if self.L != 1:
string += str(self.L)
return string.strip()
def __mul__(self, other: 'BaseUnit'):
return BaseUnit(self.t + other.t, self.l + other.l, self.m + other.m, self.i + other.i, self.T + other.T, self.n + other.n, self.L + other.L)
def __truediv__(self, other: 'BaseUnit'):
return BaseUnit(self.t - other.t, self.l - other.l, self.m - other.m, self.i - other.i, self.T - other.T, self.n - other.n, self.L - other.L)
def __eq__(self, other: 'BaseUnit'):
return (self.t == other.t) and (self.l == other.l) and (self.m == other.m) and (self.i == other.i) and (self.T == other.T) and (self.n == other.n) and (self.L == other.L)
def __pow__(self, power):
return BaseUnit(self.t*power, self.l*power, self.m*power, self.i*power, self.T*power, self.n*power, self.L*power)
Second = BaseUnit(1, 0, 0, 0, 0, 0, 0)
Metre = BaseUnit(0, 1, 0, 0, 0, 0, 0)
Kilogram = BaseUnit(0, 0, 1, 0, 0, 0, 0)
Ampere = BaseUnit(0, 0, 0, 1, 0, 0, 0)
Kelvin = BaseUnit(0, 0, 0, 0, 1, 0, 0)
Mole = BaseUnit(0, 0, 0, 0, 0, 1, 0)
Candela = BaseUnit(0, 0, 0, 0, 0, 0, 1)
class Unit:
def __init__(self, base_unit: BaseUnit, si_value: float = 1, name: str = ''):
self.base_unit = base_unit
self.si_value = si_value
self.name = name
def __repr__(self):
return self.name if self.name else str(self.si_value) + ' ' + str(self.base_unit)
def __add__(self, other: 'Unit'):
return (self.base_unit == other.base_unit)
def __eq__(self, other: 'Unit'):
return self.__add__(other) and (self.si_value == other.si_value)
def __mul__(self, other):
if type(other) == type(self):
return Unit(self.base_unit * other.base_unit, self.si_value * other.si_value)
elif type(other) in (int, float):
return Unit(self.base_unit, self.si_value * other)
else:
raise TypeError(f'Unit can only be multiplied by other unit or by int/float. Tried to multiply by {type(other)}.')
def __pow__(self, power):
return Unit(self.base_unit**power, self.si_value**power, ((f'({self.name})' if len(self.name)>1 else f'{self.name}') + f'{power}') if self.name else '')
# -- Length
u_m = Unit(Metre, 1, 'm')
u_ft = Unit(Metre, 0.30480, 'ft')
u_cm = Unit(Metre, 0.01, 'cm')
u_mm = Unit(Metre, 0.001, 'mm')
u_km = Unit(Metre, 1000, 'km')
u_yd = Unit(Metre, 0.9144, 'yd')
u_in = Unit(Metre, 0.0254, 'in')
u_mile = Unit(Metre, 1609.344, 'mile')
u_nm = Unit(Metre, 1852, 'nm')
u_au = Unit(Metre, 149597870700, 'au')
u_pc = Unit(Metre, 30856775814913673, 'pc')
u_ly = Unit(Metre, 9460730472580800, 'ly')
# -- Area
u_m2 = u_m**2
u_ft2 = u_ft**2
u_cm2 = u_cm**2
u_mm2 = u_mm**2
u_km2 = u_km**2
u_yd2 = u_yd**2
u_in2 = u_in**2
u_mile2 = u_mile**2
u_nm2 = u_nm**2
u_au2 = u_au**2
u_pc2 = u_pc**2
u_ly2 = u_ly**2
# -- Volume
u_m3 = u_m**3
u_ft3 = u_ft**3
u_cm3 = u_cm**3
u_mm3 = u_mm**3
u_km3 = u_km**3
u_yd3 = u_yd**3
u_in3 = u_in**3
u_mile3 = u_mile**3
u_nm3 = u_nm**3
u_au3 = u_au**3
u_pc3 = u_pc**3
u_ly3 = u_ly**3
# -- Time
u_s = Unit(Second, 1, 's')
u_ms = Unit(Second, 0.001, 'ms')
u_us = Unit(Second, 0.000001, 'us')
u_ns = Unit(Second, 1e-9, 'ns')
u_min = Unit(Second, 60, 'min')
u_h = Unit(Second, 3600, 'h')
u_d = Unit(Second, u_h.si_value * 24, 'd')
u_sd = Unit(Second, 86164.0905, 'sd')
u_week = Unit(Second, u_d.si_value * 7, 'week')
u_month = Unit(Second, u_d.si_value * 30, 'month')
u_y = Unit(Second, u_d.si_value * 365, 'y')
u_ty = Unit(Second, u_d.si_value * 365.24219, 'ty')
# -- Mass
u_kg = Unit(Kilogram, 1, 'kg')
u_g = Unit(Kilogram, 0.001, 'g')
u_t = Unit(Kilogram, 1000, 't')
u_lb = Unit(Kilogram, 0.45359237, 'lb')
u_un = Unit(Kilogram, u_lb.si_value / 16, 'un')
u_slug = Unit(Kilogram, 14.59390, 'slug')
# -- Force
u_N = Unit(Kilogram*Metre/Second**2, 1, 'N')
u_kN = Unit(u_N.base_unit, 1000, 'kN')
u_MN = Unit(u_N.base_unit, 1000000, 'MN')
u_GN = Unit(u_N.base_unit, 1000000000, 'GN')
u_lbf = Unit(u_N.base_unit, 4.4482216152605, 'lbf')
# -- Velocity
u_ms = Unit(Metre/Second, 1, 'm/s')
u_kph = Unit(Metre/Second, u_km.si_value/u_h.si_value, 'km/h')
u_mph = Unit(Metre/Second, 0.44704, 'mph')
u_kt = Unit(Metre/Second, u_nm.si_value/u_h.si_value, 'kt')
u_kn = Unit(Metre/Second, u_nm.si_value/u_h.si_value, 'kn')
u_ks = Unit(Metre/Second, 1000, 'km/s')
u_mach= Unit(Metre/Second, 343, 'Mach')
u_c = Unit(Metre/Second, 299792458, 'c')
# -- Pressure
u_Pa = Unit(u_N.base_unit/Metre**2, 1, 'Pa')
u_kPa = Unit(u_Pa.base_unit, 1000, 'kPa')
u_MPa = Unit(u_Pa.base_unit, 1000000, 'MPa')
u_GPa = Unit(u_Pa.base_unit, 1000000000, 'GPa')
u_Atm = Unit(u_Pa.base_unit, 101325, 'atm')
u_bar = Unit(u_Pa.base_unit, 100000, 'bar')
u_psi = Unit(u_Pa.base_unit, u_lbf.si_value/u_in2.si_value, 'psi')
u_mmHg= Unit(u_Pa.base_unit, 133.3223684, 'mmHg') # approx
u_inHg= Unit(u_Pa.base_unit, 3386.3886667, 'inHg') # approx
# -- Power
u_W = Unit(u_N.base_unit*Metre/Second, 1, 'W')
u_mW = Unit(u_W.base_unit, 0.001, 'mW')
u_kW = Unit(u_W.base_unit, 1000, 'kW')
u_MW = Unit(u_W.base_unit, 1000000, 'MW')
u_hp = Unit(u_W.base_unit, 550 * u_lbf.si_value * u_ft.si_value / u_s.si_value, 'hp')
u_nuclear = Unit(u_W.base_unit, 1000000000, 'Nuclear')
units = (
u_m , u_ft , u_cm , u_mm , u_km , u_yd , u_in , u_mile , u_nm , u_au , u_pc , u_ly ,
u_m2, u_ft2, u_cm2, u_mm2, u_km2, u_yd2, u_in2, u_mile2, u_nm2, u_au2, u_pc2, u_ly2,
u_m3, u_ft3, u_cm3, u_mm3, u_km3, u_yd3, u_in3, u_mile3, u_nm3, u_au3, u_pc3, u_ly3,
u_s, u_ms, u_us, u_ns, u_min, u_h, u_d, u_sd, u_week, u_month, u_y, u_ty,
u_kg, u_g, u_t, u_lb, u_un, u_slug,
u_N, u_kN, u_MN, u_GN, u_lbf,
u_ms, u_kph, u_mph, u_kt, u_kn, u_ks, u_mach, u_c,
u_Pa, u_kPa, u_MPa, u_GPa, u_Atm, u_bar, u_psi, u_mmHg, u_inHg,
u_W, u_mW, u_kW, u_MW, u_hp, u_nuclear,
)
varunits = """u_m , u_ft , u_cm , u_mm , u_km , u_yd , u_in , u_mile , u_nm , u_au , u_pc , u_ly ,
u_m2, u_ft2, u_cm2, u_mm2, u_km2, u_yd2, u_in2, u_mile2, u_nm2, u_au2, u_pc2, u_ly2,
u_m3, u_ft3, u_cm3, u_mm3, u_km3, u_yd3, u_in3, u_mile3, u_nm3, u_au3, u_pc3, u_ly3,
u_s, u_ms, u_us, u_ns, u_min, u_h, u_d, u_sd, u_week, u_month, u_y, u_ty,
u_kg, u_g, u_t, u_lb, u_un, u_slug,
u_N, u_kN, u_MN, u_GN, u_lbf,
u_ms, u_kph, u_mph, u_kt, u_kn, u_ks, u_mach, u_c,
u_Pa, u_kPa, u_MPa, u_GPa, u_Atm, u_bar, u_psi, u_mmHg, u_inHg,
u_W, u_mW, u_kW, u_MW, u_hp, u_nuclear"""
class Quantity:
def __init__(self, unit: Unit, value: float):
self.unit = unit
self.value = value
def __repr__(self):
return str(self.value) + ' (' + str(self.unit) + ')'
def __add__(self, other: 'Quantity'):
if not (self.unit + other.unit):
raise Exception('Cannot add units with different bases')
return Quantity(self.unit, self.value + other.value*other.unit.si_value/self.unit.si_value)
def __neg__(self):
return Quantity(self.unit, -self.value)
def __sub__(self, other):
return self.__add__(other.__neg__())
def __mul__(self, other):
return Quantity(self.unit * other.unit, self.value * other.value)
def __pow__(self, power):
return Quantity(self.unit**power, self.value**power)
def to_si(self):
return Quantity(Unit(self.unit.base_unit), self.unit.si_value * self.value)
def in_si(self):
return self.unit.si_value * self.value
'''
v1 = Quantity(u_ms, 12)
v2 = Quantity(u_mph, 12)
print(v1)
print(v2)
print(str(v1 - v2))
l1 = Quantity(u_m, 1)
l2 = Quantity(u_ft, 1)
print(l1*l2)
l3 = l2**4
print(l3.unit.base_unit)
print(l3.to_si().unit.base_unit)
'''
if __name__ == '__main__':
argv = sys.argv
if len(argv) == 2:
if argv[1] == 'help':
print("unit is a small utility created to allow arithmetic operations without worrying about physical units.")
print()
print("Syntax")
print("\t help - prints this page")
print("\t <unit_name> - prints information about the unit, in the form \n\t\t <name> (<dimensioning>) <SI value>")
print("\t list - lists all default units")
print("\t varlist - lists all default units' variable names")
print("\t <unit_name> -> <unit_name> - converts first unit to second")
print("\t <unit_name> to <unit_name> - converts first unit to second")
print("\t <amount> <unit_name> -> <unit_name> - converts given quantity of the first unit to the second unit")
print("\t <amount> <unit_name> to <unit_name> - converts given quantity of the first unit to the second unit")
elif argv[1] == 'list':
print(units)
elif argv[1] == 'varlist':
print(varunits)
else:
flag = False
for unit in units:
if argv[1] == unit.name:
print(f"{unit.name} ({unit.base_unit}) {unit.si_value}")
flag = True
if not flag:
print(f'No unit with name {argv[1]} found')
elif len(argv) == 4:
if argv[2] in ('->', 'to'):
ufrom = None
uto = None
for unit in units:
if argv[1] == unit.name:
ufrom = unit
if argv[3] == unit.name:
uto = unit
if ufrom and uto:
print(f'1 ({ufrom}) =', Quantity(uto, 0) + Quantity(ufrom, 1))
elif len(argv) == 5:
if argv[3] in ('->', 'to'):
ufrom = None
uto = None
value = float(argv[1])
for unit in units:
if argv[2] == unit.name:
ufrom = unit
if argv[4] == unit.name:
uto = unit
if ufrom and uto:
print(f'{value} ({ufrom}) =', Quantity(uto, 0) + Quantity(ufrom, value))