def __call__(self, quantity): """Overload the function call operator to convert units.""" if not hasattr(quantity, 'unit'): return Quantity(quantity, self) elif compatible(self, quantity.unit): return Quantity( quantity.num * quantity.unit.squeeze() / self.squeeze(), self) else: raise IncompatibleUnitsError()
def __call__(self, quantity): """Overload the function call operator to convert units.""" if not hasattr(quantity, 'unit'): return Quantity(quantity, self) elif compatible(self, quantity.unit): return Quantity(quantity.num * quantity.unit.squeeze() / self.squeeze(), self) else: raise IncompatibleUnitsError()
def cancel(numer, denom): """Cancel out compatible units in the given numerator and denominator. Return a triple of the implied quantity multiplier that has been squeezed out, the new numerator and the new denominator.""" multiplier = 1 simple_numer = numer[:] simple_denom = denom[:] for nleaf in numer: remaining_denom = simple_denom[:] for dleaf in remaining_denom: if compatible(nleaf, dleaf): multiplier *= nleaf.squeeze() / dleaf.squeeze() simple_numer.remove(nleaf) simple_denom.remove(dleaf) break return (simple_numer, simple_denom, multiplier)
def cancel(numer, denom): """Cancel out compatible units in the given numerator and denominator. Return a triple of the new numerator, the new denominator, the implied quantity multiplier that has been squeezed out.""" multiplier = 1 simple_numer = numer[:] simple_denom = denom[:] for nleaf in numer: remaining_denom = simple_denom[:] for dleaf in remaining_denom: if compatible(nleaf, dleaf): multiplier *= nleaf.squeeze() / dleaf.squeeze() simple_numer.remove(nleaf) simple_denom.remove(dleaf) break return (simple_numer, simple_denom, multiplier)
def __eq__(self, other): if not compatible(self.unit, other.unit): return False else: return cmp(self, other) == 0
def _ensure_same_type(self, other): """docstring for ensure_same_type""" if not compatible(self.unit, other.unit): raise IncompatibleUnitsError()