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)
Beispiel #2
0
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 testSimpleMul(self):
     mul = Mul(Int(1000000), Sym("megas"))
     assert "(1000000 * x)" == str(mul)
     seconds = mul(5.0)
     self.assertEquals(5000000.0, seconds)