def __mul__(cls, other): if isinstance(other, Number): return ValueWithUnits(other, cls) elif isinstance(other, Unit): return CompositeUnit({cls: 1, other: 1}) else: return NotImplemented
def __rmul__(self, other): if isinstance(other, Number): return ValueWithUnits(self.coefficient * other, CompositeUnit(copy(self.composed))) if isinstance(other, Unit): new_composed = copy(self.composed) new_composed[other] += 1 return CompositeUnit(new_composed, coeff=self.coefficient) else: # pragma: no cover return NotImplemented
def __rdiv__(self, other): new_composed = defaultdict(lambda: 0) for unit, exp in self.composed.iteritems(): new_composed[unit] = -exp if isinstance(other, Unit): new_composed[other] += 1 return CompositeUnit(new_composed, coeff=1.0 / self.coefficient) elif isinstance(other, Number): return ValueWithUnits(other / self.coefficient, CompositeUnit(new_composed)) else: # pragma: no cover return NotImplemented
def __div__(self, other): new_composed = copy(self.composed) if isinstance(other, CompositeUnit): for unit, exp in other.composed.iteritems(): new_composed[unit] -= exp new_coeff = self.coefficient / other.coefficient return CompositeUnit(new_composed, coeff=new_coeff) elif isinstance(other, Unit): new_composed[other] -= 1 return CompositeUnit(new_composed, coeff=self.coefficient) elif isinstance(other, Number): return ValueWithUnits(self.coefficient / other, CompositeUnit(new_composed)) else: # pragma: no cover return NotImplemented
def __rdiv__(cls, other): if isinstance(other, Number): return ValueWithUnits(other, CompositeUnit({cls: -1})) else: # pragma: no cover return NotImplemented
def __rmul__(cls, other): if isinstance(other, Number): return ValueWithUnits(other, cls) else: return NotImplemented