forked from huntfx/unit-convert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unit_convert.py
435 lines (393 loc) · 12.2 KB
/
unit_convert.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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# -*- coding: UTF-8 -*-
"""Easy way of converting units."""
from __future__ import division
__version__ = '1.1.1'
from collections import defaultdict, namedtuple
from copy import deepcopy
class Unit:
Data = 0
Distance = 1
Time = 3
Mass = 4
Temperature = 5
ConversionValue = namedtuple('ConversionValue', 'value offset', defaults=[0])
class UnitConvert(object):
Data = {
'b': {
Unit.Data: ConversionValue(1),
},
'bytes': {
Unit.Data: ConversionValue(1),
},
'kb': {
Unit.Data: ConversionValue(1024),
},
'kilobytes': {
Unit.Data: ConversionValue(1024),
},
'mb': {
Unit.Data: ConversionValue(1048576),
},
'megabytes': {
Unit.Data: ConversionValue(1048576),
},
'gb': {
Unit.Data: ConversionValue(1073741824),
},
'gigabytes': {
Unit.Data: ConversionValue(1073741824),
},
'tb': {
Unit.Data: ConversionValue(1099511627776),
},
'terabytes': {
Unit.Data: ConversionValue(1099511627776),
},
'pb': {
Unit.Data: ConversionValue(1125899906842624),
},
'petabytes': {
Unit.Data: ConversionValue(1125899906842624),
},
'nm': {
Unit.Distance: ConversionValue(0.000000001),
},
'nanometres': {
Unit.Distance: ConversionValue(0.000000001),
},
'μm': {
Unit.Distance: ConversionValue(0.000001),
},
'micrometres': {
Unit.Distance: ConversionValue(0.000001),
},
'mm': {
Unit.Distance: ConversionValue(0.001),
},
'millimetres': {
Unit.Distance: ConversionValue(0.001),
},
'cm': {
Unit.Distance: ConversionValue(0.01),
},
'centimetres': {
Unit.Distance: ConversionValue(0.01),
},
'i': {
Unit.Distance: ConversionValue(0.0254),
},
'inches': {
Unit.Distance: ConversionValue(0.0254),
},
'ft': {
Unit.Distance: ConversionValue(0.3048),
},
'feet': {
Unit.Distance: ConversionValue(0.3048),
},
'm': {
Unit.Distance: ConversionValue(1),
Unit.Time: ConversionValue(60),
},
'metres': {
Unit.Distance: ConversionValue(1),
},
'meters': {
Unit.Distance: ConversionValue(1),
},
'yd': {
Unit.Distance: ConversionValue(0.914400),
},
'yards': {
Unit.Distance: ConversionValue(0.914400),
},
'km': {
Unit.Distance: ConversionValue(1000),
},
'kilometres': {
Unit.Distance: ConversionValue(1000),
},
'kilometers': {
Unit.Distance: ConversionValue(1000),
},
'miles': {
Unit.Distance: ConversionValue(1609.34),
},
'lightyears': {
Unit.Distance: ConversionValue(9460528405000000),
},
'au': {
Unit.Distance: ConversionValue(149598550000),
},
'astronomical_units': {
Unit.Distance: ConversionValue(149598550000),
},
'parsec': {
Unit.Distance: ConversionValue(30856776000000000),
},
'ns': {
Unit.Time: ConversionValue(0.000000001),
},
'nanoseconds': {
Unit.Time: ConversionValue(0.000000001),
},
'μs': {
Unit.Time: ConversionValue(0.000001),
},
'microseconds': {
Unit.Time: ConversionValue(0.000001),
},
'ms': {
Unit.Time: ConversionValue(0.001),
},
'milliseconds': {
Unit.Time: ConversionValue(0.001),
},
'seconds': {
Unit.Time: ConversionValue(1),
},
's': {
Unit.Time: ConversionValue(1),
},
'minutes': {
Unit.Time: ConversionValue(60),
},
'hours': {
Unit.Time: ConversionValue(3600),
},
'h': {
Unit.Time: ConversionValue(3600),
},
'days': {
Unit.Time: ConversionValue(86400),
},
'd': {
Unit.Time: ConversionValue(86400),
},
'weeks': {
Unit.Time: ConversionValue(604800),
},
'w': {
Unit.Time: ConversionValue(604800),
},
'months': {
Unit.Time: ConversionValue(2627424),
},
'years': {
Unit.Time: ConversionValue(31536000),
},
'y': {
Unit.Time: ConversionValue(31536000),
},
'decades': {
Unit.Time: ConversionValue(315360000),
},
'centurys': {
Unit.Time: ConversionValue(3153600000),
},
'g': {
Unit.Mass: ConversionValue(1),
},
'grams': {
Unit.Mass: ConversionValue(1),
},
'mg': {
Unit.Mass: ConversionValue(0.001),
},
'milligrams': {
Unit.Mass: ConversionValue(0.001),
},
'kg': {
Unit.Mass: ConversionValue(1000),
},
'kilograms': {
Unit.Mass: ConversionValue(1000),
},
'oz': {
Unit.Mass: ConversionValue(28.349523125),
},
'ounces': {
Unit.Mass: ConversionValue(28.349523125),
},
'lb': {
Unit.Mass: ConversionValue(453.59237),
},
'lbs': {
Unit.Mass: ConversionValue(453.59237),
},
'pounds': {
Unit.Mass: ConversionValue(453.59237),
},
't': {
Unit.Mass: ConversionValue(1000000),
},
'tons': {
Unit.Mass: ConversionValue(907185),
},
'tonnes': {
Unit.Mass: ConversionValue(1000000),
},
'st': {
Unit.Mass: ConversionValue(6350.29318),
},
'stones': {
Unit.Mass: ConversionValue(6350.29318),
},
'k': {
Unit.Temperature: ConversionValue(1, 273.15),
},
'kelvin': {
Unit.Temperature: ConversionValue(1, 273.15),
},
'c': {
Unit.Temperature: ConversionValue(1),
},
'celcius': {
Unit.Temperature: ConversionValue(1),
},
'f': {
Unit.Temperature: ConversionValue(5 / 9, 32),
},
'farenheit': {
Unit.Temperature: ConversionValue(5 / 9, 32),
},
}
def __init__(self, *args, **kwargs):
"""Convert input kwargs into a sum of possible output values.
It does this by keeping track of the possible "types" (eg. "m"
can be metre or minute). By then providing another keyword such
as "cm", it's obvious time is not intended.
Multiple inputs of the same type will be added together.
Args:
Unit type followed by its size.
Example: UnitConvert('bytes', 52, 'mb', 1)
Kwargs:
Unit types and their size.
Example: UnitConvert(bytes=52, mb=1)
"""
self._types = None
self._totals = defaultdict(int)
# Convert args to kwargs
unit_type = None
for i, item in enumerate(args):
if i % 2:
if unit_type in kwargs:
kwargs[unit_type] += item
else:
kwargs[unit_type] = item
else:
unit_type = item
# Get value from kwargs (eg. {"bytes": 123, "mb": 1})
for unit, value in kwargs.items():
unit = unit.lower()
if unit in self.Data:
unit_data = self.Data[unit]
# Find common type
unit_types = set(unit_data.keys())
if self._types is None:
self._types = unit_types
else:
self._types &= unit_types
# Add to totals
for value_type, conversion_value in unit_data.items():
self._totals[value_type] += float((value - conversion_value.offset) * conversion_value.value)
if not self._types:
raise ValueError('input values do not have a common type')
def _op(self, value, op):
"""Perform an operation to combine two UnitConvert instances."""
if not isinstance(value, UnitConvert):
raise TypeError('must be a UnitConvert instance')
new = deepcopy(self)
new._types &= value._types
if not new._types:
raise ValueError('no common types')
for k, v in value._totals.items():
new._totals[k] = getattr(new._totals[k], op)(v)
return new
def __add__(self, value):
"""Add two UnitConvert instances together."""
return self._op(value, '__add__')
def __sub__(self, value):
"""Subtract one UnitConvert instance from another."""
return self._op(value, '__sub__')
def __mul__(self, value):
"""Multiply all values by an amount."""
new = deepcopy(self)
for k in new._totals:
new._totals[k] *= value
return new
__rmul__ = __mul__
def __floordiv__(self, value):
"""Divide all values by an amount."""
new = deepcopy(self)
for k in new._totals:
new._totals[k] //= value
return new
def __rfloordiv__(self, value):
"""Divide all values by an amount."""
new = deepcopy(self)
for k in new._totals:
new._totals[k] = value // new._totals[k]
return new
def __truediv__(self, value):
"""Divide all values by an amount."""
new = deepcopy(self)
for k in new._totals:
new._totals[k] /= value
return new
def __rtruediv__(self, value):
"""Divide all values by an amount."""
new = deepcopy(self)
for k in new._totals:
new._totals[k] = value / new._totals[k]
return new
def __pow__(self, value):
"""Raise all values to a power."""
new = deepcopy(self)
for k in new._totals:
new._totals[k] **= value
return new
def __rpow__(self, value):
"""Raise a number to the power of all values."""
new = deepcopy(self)
for k in new._totals:
new._totals[k] = value ** new._totals[k]
return new
def __getattr__(self, attr):
"""Convert to an output value.
This is left in for legacy purposes but `__getitem__` is
recommended instead.
"""
if attr in self.Data:
return self.__getitem__(attr)
return super(UnitConvert, self).__getattribute__(attr)
def __getitem__(self, item):
"""Convert to an output value."""
possible_units = set(self.Data.get(item, {})) & self._types
# An invalid unit was chosen
if not possible_units:
raise ValueError('unable to convert to "{}"'.format(item))
# There's too many types to guess
# One example would be "m" to "m"
if len(possible_units) > 1:
raise ValueError('unit type not clear')
unit = possible_units.pop()
original_value = self._totals[unit]
conversion_value = self.Data[item][unit]
return original_value / conversion_value.value + conversion_value.offset
def keys(self):
"""Return a list of all available unit types."""
return [k for k, v in self.Data.items() if self._types & set(v)]
def values(self):
"""Return a list of all available unit values."""
return list(map(self.__getitem__, self.keys()))
def items(self):
"""Return a mapping of unit types to values."""
keys = self.keys()
return dict(zip(keys, map(self.__getitem__, keys)))
def get(self, item, default=None):
"""Get a conversion if available or fallback to the default value."""
try:
return self.__getattr__(item)
except ValueError:
return default