Exemplo n.º 1
0
    def test_zero_and_one(self):
        unitses = [
            Units.scalar,
            Units.parse('GROM'),
        ]

        for u in unitses:
            self.assertIsInstance(u.zero, Value)
            self.assertIsInstance(u.one, Value)
            self.assertEqual(Decimal('0'), u.zero.amount)
            self.assertEqual(Decimal('1'), u.one.amount)
            self.assertEqual(Units.scalar.one, u.one / u.one)
            self.assertEqual(u.zero, u.one * Units.scalar.zero)
            self.assertIs(u**2, (u.zero * u.one).units)
Exemplo n.º 2
0
    def parse(cls, text):
        m = cls._rgx.match(text)
        if m is None:
            raise cls.ParseError("Could not parse Value: {!r}", text)

        dectext = m.group("decimal")
        try:
            decimal = Decimal(dectext)
        except InvalidOperation as e:
            raise cls.ParseError("Could not parse decimal {!r} of Value: {}", dectext, e)

        unitext = m.group("units")
        if unitext is None:
            units = Units.scalar
        else:
            units = Units.parse(unitext)
        return cls(decimal, units)
Exemplo n.º 3
0
    def test_from_string(self):
        inputs = [
            '0',
            '1',
            '1.00',
            '+17e3',
            '-42.07000',
        ]

        unitses = [
            Units.scalar,
            Units.parse('GROM'),
        ]

        for units in unitses:
            for i in inputs:
                self.assertEqual(
                    units.from_string(i),
                    Value(Decimal(i), units),
                )
Exemplo n.º 4
0
 def setUp(self):
     self.meter = Units.parse('meter')