Esempio n. 1
0
 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()
Esempio n. 2
0
 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()
Esempio n. 3
0
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)
Esempio n. 4
0
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)
Esempio n. 5
0
 def __eq__(self, other):
     if not compatible(self.unit, other.unit):
         return False
     else:
         return cmp(self, other) == 0
Esempio n. 6
0
 def _ensure_same_type(self, other):
     """docstring for ensure_same_type"""
     if not compatible(self.unit, other.unit):
         raise IncompatibleUnitsError()
Esempio n. 7
0
 def __eq__(self, other):
     if not compatible(self.unit, other.unit):
         return False
     else:
         return cmp(self, other) == 0
Esempio n. 8
0
 def _ensure_same_type(self, other):
     """docstring for ensure_same_type"""
     if not compatible(self.unit, other.unit):
         raise IncompatibleUnitsError()