mirror of https://github.com/m-labs/artiq.git
units: implement operators
This commit is contained in:
parent
c769bdab9b
commit
732412e4c9
|
@ -32,9 +32,55 @@ class Quantity:
|
||||||
return NotImplemented
|
return NotImplemented
|
||||||
return Quantity(self.amount*other, self.unit)
|
return Quantity(self.amount*other, self.unit)
|
||||||
|
|
||||||
|
def __neg__(self):
|
||||||
|
return Quantity(-self.amount, self.unit)
|
||||||
|
|
||||||
|
def __add__(self, other):
|
||||||
|
if self.unit != other.unit:
|
||||||
|
raise DimensionError
|
||||||
|
return Quantity(self.amount + other.amount, self.unit)
|
||||||
|
def __radd__(self, other):
|
||||||
|
if self.unit != other.unit:
|
||||||
|
raise DimensionError
|
||||||
|
return Quantity(other.amount + self.amount, self.unit)
|
||||||
|
def __sub__(self, other):
|
||||||
|
if self.unit != other.unit:
|
||||||
|
raise DimensionError
|
||||||
|
return Quantity(self.amount - other.amount, self.unit)
|
||||||
|
def __rsub__(self, other):
|
||||||
|
if self.unit != other.unit:
|
||||||
|
raise DimensionError
|
||||||
|
return Quantity(other.amount - self.amount, self.unit)
|
||||||
|
|
||||||
|
def __lt__(self, other):
|
||||||
|
if self.unit != other.unit:
|
||||||
|
raise DimensionError
|
||||||
|
return self.amount < other.amount
|
||||||
|
def __le__(self, other):
|
||||||
|
if self.unit != other.unit:
|
||||||
|
raise DimensionError
|
||||||
|
return self.amount <= other.amount
|
||||||
|
def __eq__(self, other):
|
||||||
|
if self.unit != other.unit:
|
||||||
|
raise DimensionError
|
||||||
|
return self.amount == other.amount
|
||||||
|
def __ne__(self, other):
|
||||||
|
if self.unit != other.unit:
|
||||||
|
raise DimensionError
|
||||||
|
return self.amount != other.amount
|
||||||
|
def __gt__(self, other):
|
||||||
|
if self.unit != other.unit:
|
||||||
|
raise DimensionError
|
||||||
|
return self.amount > other.amount
|
||||||
|
def __ge__(self, other):
|
||||||
|
if self.unit != other.unit:
|
||||||
|
raise DimensionError
|
||||||
|
return self.amount >= other.amount
|
||||||
|
|
||||||
def check_unit(value, unit):
|
def check_unit(value, unit):
|
||||||
if not isinstance(value, Quantity) or value.unit != unit:
|
if not isinstance(value, Quantity) or value.unit != unit:
|
||||||
raise DimensionError
|
raise DimensionError
|
||||||
|
return value.amount
|
||||||
|
|
||||||
def _register_unit(base_prefix, name, prefixes):
|
def _register_unit(base_prefix, name, prefixes):
|
||||||
base_prefix_exp = _prefixes_str.index(base_prefix)
|
base_prefix_exp = _prefixes_str.index(base_prefix)
|
||||||
|
|
Loading…
Reference in New Issue