예제 #1
0
    def test_anonymous_context(self, func_registry):
        ureg = UnitRegistry()
        c = Context()
        c.add_transformation("[length]", "[time]",
                             lambda ureg, x: x / ureg("5 cm/s"))
        with pytest.raises(ValueError):
            ureg.add_context(c)

        x = ureg("10 cm")
        expect = ureg("2 s")
        helpers.assert_quantity_equal(x.to("s", c), expect)

        with ureg.context(c):
            helpers.assert_quantity_equal(x.to("s"), expect)

        ureg.enable_contexts(c)
        helpers.assert_quantity_equal(x.to("s"), expect)
        ureg.disable_contexts(1)
        with pytest.raises(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):
            helpers.assert_quantity_equal(x.to("s"), expect)
            # Transformations only in c2 are still working even if c takes priority
            helpers.assert_quantity_equal(ureg("100 kg").to("s"), ureg("10 s"))
        with ureg.context(c, c2):
            helpers.assert_quantity_equal(x.to("s"), ureg("1 s"))
예제 #2
0
 def _test_ctx(self, ctx):
     ureg = UnitRegistry()
     q = 500 * ureg.meter
     s = (ureg.speed_of_light / q).to('Hz')
     ureg.add_context(ctx)
     with ureg.context(ctx.name):
         self.assertEqual(q.to('Hz'), s)
         self.assertEqual(s.to('meter'), q)
예제 #3
0
 def _test_ctx(self, ctx):
     ureg = UnitRegistry()
     q = 500 * ureg.meter
     s = (ureg.speed_of_light / q).to('Hz')
     ureg.add_context(ctx)
     with ureg.context(ctx.name):
         self.assertEqual(q.to('Hz'), s)
         self.assertEqual(s.to('meter'), q)
예제 #4
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]))
예제 #5
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]))
예제 #6
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)
예제 #7
0
    def _test_ctx(self, ctx):
        ureg = UnitRegistry()
        q = 500 * ureg.meter
        s = (ureg.speed_of_light / q).to('Hz')

        nctx = len(ureg._contexts)

        self.assertNotIn(ctx.name, ureg._contexts)
        ureg.add_context(ctx)

        self.assertIn(ctx.name, ureg._contexts)
        self.assertEqual(len(ureg._contexts), nctx + 1 + len(ctx.aliases))

        with ureg.context(ctx.name):
            self.assertEqual(q.to('Hz'), s)
            self.assertEqual(s.to('meter'), q)

        ureg.remove_context(ctx.name)
        self.assertNotIn(ctx.name, ureg._contexts)
        self.assertEqual(len(ureg._contexts), nctx)
예제 #8
0
    def _test_ctx(self, ctx):
        ureg = UnitRegistry()
        q = 500 * ureg.meter
        s = (ureg.speed_of_light / q).to("Hz")

        nctx = len(ureg._contexts)

        self.assertNotIn(ctx.name, ureg._contexts)
        ureg.add_context(ctx)

        self.assertIn(ctx.name, ureg._contexts)
        self.assertEqual(len(ureg._contexts), nctx + 1 + len(ctx.aliases))

        with ureg.context(ctx.name):
            self.assertEqual(q.to("Hz"), s)
            self.assertEqual(s.to("meter"), q)

        ureg.remove_context(ctx.name)
        self.assertNotIn(ctx.name, ureg._contexts)
        self.assertEqual(len(ureg._contexts), nctx)
예제 #9
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']))
예제 #10
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']))
예제 #11
0
    def _test_ctx(self, ctx):
        ureg = UnitRegistry()
        q = 500 * ureg.meter
        s = (ureg.speed_of_light / q).to("Hz")

        nctx = len(ureg._contexts)

        assert ctx.name not in ureg._contexts
        ureg.add_context(ctx)

        assert ctx.name in ureg._contexts
        assert len(ureg._contexts) == nctx + 1 + len(ctx.aliases)

        with ureg.context(ctx.name):
            assert q.to("Hz") == s
            assert s.to("meter") == q

        ureg.remove_context(ctx.name)
        assert ctx.name not in ureg._contexts
        assert len(ureg._contexts) == nctx
예제 #12
0
파일: units.py 프로젝트: olemke/typhon
# -*- coding: utf-8 -*-

"""Various units-related things

This module has a soft dependency on the pint units library.  Please
import this module only conditionally or only if you can accept a pint
dependency.
"""

from pint import (UnitRegistry, Context)
ureg = UnitRegistry()
ureg.define("micro- = 1e-6 = µ-")

# aid conversion between different radiance units
sp2 = Context("radiance")
sp2.add_transformation(
    "[length] * [mass] / [time] ** 3",
    "[mass] / [time] ** 2",
    lambda ureg, x: x / ureg.speed_of_light)
sp2.add_transformation(
    "[mass] / [time] ** 2",
    "[length] * [mass] / [time] ** 3",
    lambda ureg, x: x * ureg.speed_of_light)
ureg.add_context(sp2)

radiance_units = {
    "si": ureg.W / (ureg.m**2 * ureg.sr * ureg.Hz),
    "ir": ureg.mW / (ureg.m**2 * ureg.sr * (1 / ureg.cm))}
예제 #13
0
#    "[mass] / ([length] * [time] ** 3)",
#    "[length] * [mass] / [time] ** 3",
#    lambda ureg, x, **kwargs: )
def _R_to_bt(ureg, R, srf):
    """For use by pint, do not call directly, use q.to or SRF class."""
    if srf.lookup_table is None:
        srf.make_lookup_table()
    return ureg.Quantity(srf.L_to_T(R), 'K')
def _bt_to_R(ureg, T, srf):
    return ureg.Quantity(srf.blackbody_radiance(T, spectral=True))
_bt_to_R.__doc__ = _R_to_bt.__doc__
sp2.add_transformation(
    "[mass] / [time] ** 2",
    "[temperature]",
    _R_to_bt)
sp2.add_transformation(
    "[temperature]",
    "[mass] / [time] ** 2",
    _bt_to_R)
ureg.add_context(sp2)

radiance_units = {
    "si": ureg.W / (ureg.m**2 * ureg.sr * ureg.Hz),
    "ir": ureg.mW / (ureg.m**2 * ureg.sr * ureg.cm**-1)}

# add wavenumber/kayser to spectroscopy contexts for use with pint<0.8
ureg._contexts["spectroscopy"].add_transformation("[length]", "1/[length]",
    lambda ureg, x, **kwargs: 1/x)
ureg._contexts["spectroscopy"].add_transformation("1/[length]", "[length]",
    lambda ureg, x, **kwargs: 1/x)
예제 #14
0
파일: units.py 프로젝트: znicholls/openscm
unit_registry.define("ppm = 1000 * ppb")

# Contexts:

_c = Context("AR4GWP12")
_c.add_transformation(
    "[carbon]",
    "[nitrogen]",
    lambda unit_registry, x: 20 * unit_registry.N * x / unit_registry.C,
)
_c.add_transformation(
    "[nitrogen]",
    "[carbon]",
    lambda unit_registry, x: x * unit_registry.C / unit_registry.N / 20,
)
unit_registry.add_context(_c)

_ch4_context = Context("CH4_conversions")
_ch4_context.add_transformation(
    "[carbon]",
    "[methane]",
    lambda unit_registry, x: 16 / 12 * unit_registry.CH4 * x / unit_registry.C,
)
_ch4_context.add_transformation(
    "[methane]",
    "[carbon]",
    lambda unit_registry, x: x * unit_registry.C / unit_registry.CH4 /
    (16 / 12),
)
unit_registry.add_context(_ch4_context)