示例#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()