def testBigInt(self): big = "123456789012345678901234567890" big = big * 5 i = Mul(Int(big), Int(big)) x = i(-1) # -1 is ignored target = float(big) * float(big) self.assertEquals(target, x)
class TemperatureI(_omero_model.Temperature, UnitBase): UNIT_VALUES = sorted(UnitsTemperature._enumerators.values()) CONVERSIONS = dict() for val in UNIT_VALUES: CONVERSIONS[val] = dict() CONVERSIONS[UnitsTemperature.CELSIUS][UnitsTemperature.FAHRENHEIT] = \ Add(Mul(Rat(Int(9), Int(5)), Sym("c")), Int(32)) # nopep8 CONVERSIONS[UnitsTemperature.CELSIUS][UnitsTemperature.KELVIN] = \ Add(Sym("c"), Rat(Int(5463), Int(20))) # nopep8 CONVERSIONS[UnitsTemperature.CELSIUS][UnitsTemperature.RANKINE] = \ Add(Mul(Rat(Int(9), Int(5)), Sym("c")), Rat(Int(49167), Int(100))) # nopep8 CONVERSIONS[UnitsTemperature.FAHRENHEIT][UnitsTemperature.CELSIUS] = \ Add(Mul(Rat(Int(5), Int(9)), Sym("f")), Rat(Int(-160), Int(9))) # nopep8 CONVERSIONS[UnitsTemperature.FAHRENHEIT][UnitsTemperature.KELVIN] = \ Add(Mul(Rat(Int(5), Int(9)), Sym("f")), Rat(Int(45967), Int(180))) # nopep8 CONVERSIONS[UnitsTemperature.FAHRENHEIT][UnitsTemperature.RANKINE] = \ Add(Sym("f"), Rat(Int(45967), Int(100))) # nopep8 CONVERSIONS[UnitsTemperature.KELVIN][UnitsTemperature.CELSIUS] = \ Add(Sym("k"), Rat(Int(-5463), Int(20))) # nopep8 CONVERSIONS[UnitsTemperature.KELVIN][UnitsTemperature.FAHRENHEIT] = \ Add(Mul(Rat(Int(9), Int(5)), Sym("k")), Rat(Int(-45967), Int(100))) # nopep8 CONVERSIONS[UnitsTemperature.KELVIN][UnitsTemperature.RANKINE] = \ Mul(Rat(Int(9), Int(5)), Sym("k")) # nopep8 CONVERSIONS[UnitsTemperature.RANKINE][UnitsTemperature.CELSIUS] = \ Add(Mul(Rat(Int(5), Int(9)), Sym("r")), Rat(Int(-5463), Int(20))) # nopep8 CONVERSIONS[UnitsTemperature.RANKINE][UnitsTemperature.FAHRENHEIT] = \ Add(Sym("r"), Rat(Int(-45967), Int(100))) # nopep8 CONVERSIONS[UnitsTemperature.RANKINE][UnitsTemperature.KELVIN] = \ Mul(Rat(Int(5), Int(9)), Sym("r")) # nopep8 del val SYMBOLS = dict() SYMBOLS["CELSIUS"] = "°C" SYMBOLS["FAHRENHEIT"] = "°F" SYMBOLS["KELVIN"] = "K" SYMBOLS["RANKINE"] = "°R" def __init__(self, value=None, unit=None): _omero_model.Temperature.__init__(self) if unit is None: target = None elif isinstance(unit, UnitsTemperature): target = unit elif isinstance(unit, (str, unicode)): target = getattr(UnitsTemperature, unit) else: raise Exception("Unknown unit: %s (%s)" % ( unit, type(unit) )) if isinstance(value, _omero_model.TemperatureI): # This is a copy-constructor call. source = value.getUnit() if target is None: raise Exception("Null target unit") if source is None: raise Exception("Null source unit") if target == source: self.setValue(value.getValue()) self.setUnit(source) else: c = self.CONVERSIONS.get(source).get(target) if c is None: t = (value.getValue(), source, target) msg = "%s %s cannot be converted to %s" % t raise Exception(msg) self.setValue(c(value.getValue())) self.setUnit(target) else: self.setValue(value) self.setUnit(target) def getUnit(self, current=None): return self._unit def getValue(self, current=None): return self._value def getSymbol(self, current=None): return self.SYMBOLS.get(str(self.getUnit())) @staticmethod def lookupSymbol(unit): return TemperatureI.SYMBOLS.get(str(unit)) def setUnit(self, unit, current=None): self._unit = unit def setValue(self, value, current=None): self._value = value def __str__(self): return self._base_string(self.getValue(), self.getUnit())
def testDelayedRat(self): r = Rat(Int(1), Int(3)) x = r(-1) # -1 is ignored self.assertEquals(0.333333, x)
def testSimpleInt(self): i = Int(123) assert "123" == str(i) x = i(-1) # -1 is ignored self.assertEquals(123.0, x)
def testSimpleMul(self): mul = Mul(Int(1000000), Sym("megas")) assert "(1000000 * x)" == str(mul) seconds = mul(5.0) self.assertEquals(5000000.0, seconds)