コード例 #1
0
 def __pow__(self, other):
     if self.offset != 0:
         raise TypeError("cannot exponentiate units with non-zero offset")
     if isinstance(other, int):
         return PhysicalUnit(other * self.names, pow(self.factor, other),
                             map(lambda x, p=other: x * p, self.powers))
     if isinstance(other, float):
         inv_exp = 1. / other
         rounded = int(N.floor(inv_exp + 0.5))
         if abs(inv_exp - rounded) < 1.e-10:
             if reduce(lambda a, b: a and b,
                       map(lambda x, e=rounded: x % e == 0, self.powers)):
                 f = pow(self.factor, other)
                 p = map(lambda x, p=rounded: x / p, self.powers)
                 if reduce(
                         lambda a, b: a and b,
                         map(lambda x, e=rounded: x % e == 0,
                             self.names.values())):
                     names = self.names / rounded
                 else:
                     names = NumberDict()
                     if f != 1.:
                         names[str(f)] = 1
                     for i in range(len(p)):
                         names[_base_names[i]] = p[i]
                 return PhysicalUnit(names, f, p)
             else:
                 raise TypeError('Illegal exponent')
     raise TypeError('Only integer and inverse integer exponents allowed')
コード例 #2
0
 def __init__(self, names, factor, powers, offset=0):
     """
     @param names: a dictionary mapping each name component to its
                   associated integer power (e.g. C{{'m': 1, 's': -1}})
                   for M{m/s}). As a shorthand, a string may be passed
                   which is assigned an implicit power 1.
     @type names: C{dict} or C{str}
     @param factor: a scaling factor
     @type factor: C{float}
     @param powers: the integer powers for each of the nine base units
     @type powers: C{list} of C{int}
     @param offset: an additive offset to the base unit (used only for
                    temperatures)
     @type offset: C{float}
     """
     if type(names) == type(''):
         self.names = NumberDict()
         self.names[names] = 1
         pass
     else:
         self.names = names
         pass
     self.factor = factor
     self.offset = offset
     self.powers = powers
コード例 #3
0
 def setName(self, name):
     self.names = NumberDict()
     self.names[name] = 1