Instantly share code, notes, and snippets.

Embed
What would you like to do?
POC distance class with unit conversion
import math
factors = {
'mm': 0.001,
'cm': 0.01,
'in': 0.0254,
'dm': 0.1,
'ft': 0.3048,
'yd': 0.9144,
'm': 1.0,
'km': 1000.0,
'mi': 1609.344,
'nm': 1852.0,
'gm': 1855.3248,
'ls': 299792458.0,
'au': 149597870700.0,
'ly': 9460730472580800.0,
'pc': (648000.0 / math.pi) * 149597870700.0,
}
available = set(factors.keys())
def convert(value, from_unit, to_unit):
assert {from_unit, to_unit} <= available
return value * factors[from_unit] / factors[to_unit]
class Distance(object):
_dist = 0.0
def __init__(self, distance=0.0, unit='m'):
self.__set(float(distance), unit)
for i in available:
self.__add_property(i, convert(distance, unit, i))
def __set(self, value, unit):
self._dist = convert(value, unit, 'm')
def __add_property(self, name, value, doc=None):
setattr(
self.__class__, 'in_' + name, property(
fget=lambda self: convert(self._dist, 'm', name),
fset=lambda self, value: self.__set(value, name),
doc=doc
)
)
def __repr__(self):
return str(self._dist)
def __float__(self):
return self._dist
def __int__(self):
return int(self._dist)
def __bool__(self):
return bool(self._dist)
def __complex__(self):
return complex(self._dist)
def __round__(self, n=None):
return round(self._dist, n)
def __hash__(self):
return hash(self._dist)
def __abs__(self):
return abs(self._dist)
def __pos__(self):
return +self._dist
def __neg__(self):
return -self._dist
def __invert__(self):
return ~int(self._dist)
def __lt__(self, other):
return self._dist < float(other)
def __le__(self, other):
return self._dist <= float(other)
def __eq__(self, other):
return self._dist == float(other)
def __ne__(self, other):
return self._dist != float(other)
def __gt__(self, other):
return self._dist > float(other)
def __ge__(self, other):
return self._dist >= float(other)
def __add__(self, other):
return Distance(self._dist + float(other))
def __radd__(self, other):
return Distance(self._dist + float(other))
def __iadd__(self, other):
self._dist += float(other)
return self
def __sub__(self, other):
return Distance(self._dist - float(other))
def __rsub__(self, other):
return Distance(float(other) - self._dist)
def __isub__(self, other):
self._dist -= float(other)
return self
def __mul__(self, other):
return Distance(self._dist * float(other))
def __rmul__(self, other):
return Distance(self._dist * float(other))
def __imul__(self, other):
self._dist *= float(other)
return self
def __truediv__(self, other):
return Distance(self._dist / float(other))
def __rtruediv__(self, other):
return Distance(float(other) / self._dist)
def __itruediv__(self, other):
self._dist /= float(other)
return self
def __floordiv__(self, other):
return Distance(self._dist // float(other))
def __rfloordiv__(self, other):
return Distance(float(other) // self._dist)
def __ifloordiv__(self, other):
self._dist //= float(other)
return self
def __mod__(self, other):
return Distance(self._dist % float(other))
def __rmod__(self, other):
return Distance(float(other) % self._dist)
def __imod__(self, other):
self._dist %= float(other)
return self
def __divmod__(self, other):
return Distance(self._dist // float(other)), Distance(self._dist % float(other))
def __rdivmod__(self, other):
return Distance(float(other) // self._dist), Distance(float(other) % self._dist)
def __pow__(self, power, modulo=None):
if modulo and isinstance(power, int):
return Distance(pow(int(self._dist), int(power), modulo))
return Distance(self._dist ** float(power))
def __rpow__(self, other):
return Distance(float(other) ** self._dist)
def __ipow__(self, other, modulo=None):
if modulo:
self._dist = float(pow(int(self._dist), int(other), modulo))
else:
self._dist **= float(other)
return self
def __lshift__(self, other):
return Distance(int(self._dist) << int(other))
def __rlshift__(self, other):
return Distance(int(other) << int(self._dist))
def __ilshift__(self, other):
self._dist = float(int(self._dist) << int(other))
return self
def __rshift__(self, other):
return Distance(int(self._dist) >> int(other))
def __rrshift__(self, other):
return Distance(int(other) >> int(self._dist))
def __irshift__(self, other):
self._dist = float(int(self._dist) >> int(other))
return self
def __and__(self, other):
return Distance(int(self._dist) & int(other))
def __rand__(self, other):
return Distance(int(other) & int(self._dist))
def __iand__(self, other):
self._dist = float(int(self._dist) & int(other))
return self
def __or__(self, other):
return Distance(int(self._dist) | int(other))
def __ror__(self, other):
return Distance(int(other) | int(self._dist))
def __ior__(self, other):
self._dist = float(int(self._dist) | int(other))
return self
def __xor__(self, other):
return Distance(int(self._dist) ^ int(other))
def __rxor__(self, other):
return Distance(int(other) ^ int(self._dist))
def __ixor__(self, other):
self._dist = float(int(self._dist) ^ int(other))
return self
@property
def raw(self):
return self._dist
@raw.setter
def raw(self, value):
self._dist = float(value)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment