Пример #1
0
    def test_anonymous_context(self):
        ureg = UnitRegistry()
        c = Context()
        c.add_transformation("[length]", "[time]",
                             lambda ureg, x: x / ureg("5 cm/s"))
        self.assertRaises(ValueError, ureg.add_context, c)

        x = ureg("10 cm")
        expect = ureg("2 s")
        self.assertQuantityEqual(x.to("s", c), expect)

        with ureg.context(c):
            self.assertQuantityEqual(x.to("s"), expect)

        ureg.enable_contexts(c)
        self.assertQuantityEqual(x.to("s"), expect)
        ureg.disable_contexts(1)
        self.assertRaises(DimensionalityError, x.to, "s")

        # Multiple anonymous contexts
        c2 = Context()
        c2.add_transformation("[length]", "[time]",
                              lambda ureg, x: x / ureg("10 cm/s"))
        c2.add_transformation("[mass]", "[time]",
                              lambda ureg, x: x / ureg("10 kg/s"))
        with ureg.context(c2, c):
            self.assertQuantityEqual(x.to("s"), expect)
            # Transformations only in c2 are still working even if c takes priority
            self.assertQuantityEqual(ureg("100 kg").to("s"), ureg("10 s"))
        with ureg.context(c, c2):
            self.assertQuantityEqual(x.to("s"), ureg("1 s"))
Пример #2
0
def add_arg_ctxs(ureg):
    a, b = UnitsContainer({'[length]': 1}), UnitsContainer({'[time]': -1})
    d = Context('lc')
    d.add_transformation(a, b, lambda ureg, x, n: ureg.speed_of_light / x / n)
    d.add_transformation(b, a, lambda ureg, x, n: ureg.speed_of_light / x / n)

    ureg.add_context(d)

    a, b = UnitsContainer({'[length]': 1}), UnitsContainer({'[current]': 1})
    d = Context('ab')
    d.add_transformation(a, b, lambda ureg, x: ureg.ampere * ureg.meter / x)
    d.add_transformation(b, a, lambda ureg, x: ureg.ampere * ureg.meter / x)

    ureg.add_context(d)
Пример #3
0
def add_ctxs(ureg):
    a, b = UnitsContainer({"[length]": 1}), UnitsContainer({"[time]": -1})
    d = Context("lc")
    d.add_transformation(a, b, lambda ureg, x: ureg.speed_of_light / x)
    d.add_transformation(b, a, lambda ureg, x: ureg.speed_of_light / x)

    ureg.add_context(d)

    a, b = UnitsContainer({"[length]": 1}), UnitsContainer({"[current]": 1})
    d = Context("ab")
    d.add_transformation(a, b, lambda ureg, x: ureg.ampere * ureg.meter / x)
    d.add_transformation(b, a, lambda ureg, x: ureg.ampere * ureg.meter / x)

    ureg.add_context(d)
Пример #4
0
def add_argdef_ctxs(ureg):
    a, b = UnitsContainer({'[length]': 1}), UnitsContainer({'[time]': -1})
    d = Context('lc', defaults=dict(n=1))
    assert d.defaults == dict(n=1)

    d.add_transformation(a, b, lambda ureg, x, n: ureg.speed_of_light / x / n)
    d.add_transformation(b, a, lambda ureg, x, n: ureg.speed_of_light / x / n)

    ureg.add_context(d)

    a, b = UnitsContainer({'[length]': 1}), UnitsContainer({'[current]': -1})
    d = Context('ab')
    d.add_transformation(a, b, lambda ureg, x: 1 / x)
    d.add_transformation(b, a, lambda ureg, x: 1 / x)

    ureg.add_context(d)
Пример #5
0
def add_argdef_ctxs(ureg):
    a, b = UnitsContainer({"[length]": 1}), UnitsContainer({"[time]": -1})
    d = Context("lc", defaults=dict(n=1))
    assert d.defaults == dict(n=1)

    d.add_transformation(a, b, lambda ureg, x, n: ureg.speed_of_light / x / n)
    d.add_transformation(b, a, lambda ureg, x, n: ureg.speed_of_light / x / n)

    ureg.add_context(d)

    a, b = UnitsContainer({"[length]": 1}), UnitsContainer({"[current]": 1})
    d = Context("ab")
    d.add_transformation(a, b, lambda ureg, x: ureg.ampere * ureg.meter / x)
    d.add_transformation(b, a, lambda ureg, x: ureg.ampere * ureg.meter / x)

    ureg.add_context(d)
Пример #6
0
    def test_warnings(self):

        ureg = UnitRegistry()

        with self.capture_log() as buffer:
            add_ctxs(ureg)

            d = Context('ab')
            ureg.add_context(d)

            self.assertEqual(len(buffer), 1)
            self.assertIn("ab", str(buffer[-1]))

            d = Context('ab1', aliases=('ab', ))
            ureg.add_context(d)

            self.assertEqual(len(buffer), 2)
            self.assertIn("ab", str(buffer[-1]))
Пример #7
0
    def test_warnings(self, caplog):

        ureg = UnitRegistry()

        with caplog.at_level(logging.DEBUG, "pint"):
            add_ctxs(ureg)

            d = Context("ab")
            ureg.add_context(d)

            assert len(caplog.records) == 1
            assert "ab" in str(caplog.records[-1].args)

            d = Context("ab1", aliases=("ab", ))
            ureg.add_context(d)

            assert len(caplog.records) == 2
            assert "ab" in str(caplog.records[-1].args)
Пример #8
0
    def test_warnings(self):

        ureg = UnitRegistry()

        th = TestHandler()
        logger.addHandler(th)

        add_ctxs(ureg)

        d = Context('ab')
        ureg.add_context(d)

        self.assertEqual(len(th.buffer), 1)
        self.assertIn("ab", str(th.buffer[-1]['message']))

        d = Context('ab1', aliases=('ab', ))
        ureg.add_context(d)

        self.assertEqual(len(th.buffer), 2)
        self.assertIn("ab", str(th.buffer[-1]['message']))
Пример #9
0
    def define_context(self):
        self.contextName = 'Owis-{}'.format(uuid.uuid4())
        context = Context(self.contextName)

        if not self._pitch.dimensionless:
            context.add_transformation(
                '', '[length]', lambda ureg, x: x /
                (self._pitch * self._microstepResolution))
            context.add_transformation(
                '[length]', '[]',
                lambda ureg, x: x * self._pitch * self._microstepResolution)
            context.add_transformation(
                '1/[time]', '[length]/[time]', lambda ureg, x: x /
                (self._pitch * self._microstepResolution))
            context.add_transformation(
                '[length]/[time]', '1/[time]',
                lambda ureg, x: x * self._pitch * self._microstepResolution)

        ureg.add_context(context)