def testCombinedDivision(self): # Do we collapse the units? assert Units.volt / Units.ohm is Units.ampere # This should make a temporary unit, as division is not commutative x = Units.ohm / Units.volt assert not Unit.hasUnit(x)
def __findUnit(self): """ Take a string like kV and work out prefix and units, obviously there is scope for collision between units and prefixes... :return: :mod:`Unit` object """ if not self.unit: return NO_UNIT if isinstance(self.unit, Unit.Unit): return self.unit u = self.unit if Unit.hasUnit(u): return Unit.getUnit(u) # Work backwards through the list trying to find a unit that matches unit = self.unit = None ul = list(u) u = "" while ul: u = (ul.pop() + u) if Unit.hasUnit(u): unit = Unit.getUnit(u) break # Nothing left to look at if not ul: if not unit: # Make a temporary unit, since we don't know what this is unit = Unit.Unit(u, u, True) return unit # We have left overs... this should be a prefix... ul = ''.join(ul) if Prefix.hasPrefix(ul): self.amount *= Prefix.getPrefix(ul) return unit
def testSimpleUnit(self): t = Unit.Unit('ZZZ', 'TestUnit', True) assert not Unit.hasUnit('ZZZ') t = Unit.Unit('ZZZ', 'TestUnit', False) assert Unit.hasUnit('ZZZ')