예제 #1
0
파일: abstract.py 프로젝트: jyn514/units
 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()
예제 #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()
예제 #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)
예제 #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)
예제 #5
0
파일: quantity.py 프로젝트: jyn514/units
 def __eq__(self, other):
     if not compatible(self.unit, other.unit):
         return False
     else:
         return cmp(self, other) == 0
예제 #6
0
파일: quantity.py 프로젝트: jyn514/units
 def _ensure_same_type(self, other):
     """docstring for ensure_same_type"""
     if not compatible(self.unit, other.unit):
         raise IncompatibleUnitsError()
예제 #7
0
 def __eq__(self, other):
     if not compatible(self.unit, other.unit):
         return False
     else:
         return cmp(self, other) == 0
예제 #8
0
 def _ensure_same_type(self, other):
     """docstring for ensure_same_type"""
     if not compatible(self.unit, other.unit):
         raise IncompatibleUnitsError()