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 testFahrenheitCelsius(self): ftoc = Add(Mul(Rat(5, 9), Sym("f")), Rat(-160, 9)) assert "(((5 / 9) * x) + (-160 / 9))" == str(ftoc) self.assertEquals(0.0, ftoc(32.0)) self.assertEquals(100.0, ftoc(212.0)) self.assertEquals(-40.0, ftoc(-40.0))
def testSimpleRat(self): r = Rat(1, 3) assert "(1 / 3)" == str(r) x = r(-1) # -1 is ignored self.assertEquals(0.333333, x)
def testDelayedRat(self): r = Rat(Int(1), Int(3)) x = r(-1) # -1 is ignored self.assertEquals(0.333333, x)
def testSimpleAdd(self): add = Add(Rat(1, 2), Rat(1, 2)) assert "(1 + 2)", str(add) whole = add(-1) # -1 is ignored self.assertEquals(1.0, whole)