Beispiel #1
0
    def test_get_base_units_different_exponent(self):
        sysname = "mysys3"
        ureg = UnitRegistry()

        g = ureg.get_group("test-imperial")
        g.add_units("inch", "yard", "pint")
        ureg.get_compatible_units("meter", "test-imperial")

        lines = ["@system %s using test-imperial" % sysname, "pint:meter"]

        s = ureg.System.from_lines(lines, ureg.get_base_units)
        ureg._systems[s.name] = s

        # base_factor, destination_units
        c = ureg.get_base_units("inch", system=sysname)
        assert round(abs(c[0] - 0.326), 3) == 0
        assert c[1] == {"pint": 1.0 / 3}

        c = ureg.get_base_units("cm", system=sysname)
        assert round(abs(c[0] - 0.1283), 3) == 0
        assert c[1] == {"pint": 1.0 / 3}

        c = ureg.get_base_units("inch**2", system=sysname)
        assert round(abs(c[0] - 0.326**2), 3) == 0
        assert c[1] == {"pint": 2.0 / 3}

        c = ureg.get_base_units("cm**2", system=sysname)
        assert round(abs(c[0] - 0.1283**2), 3) == 0
        assert c[1] == {"pint": 2.0 / 3}
Beispiel #2
0
    def test_get_base_units_different_exponent(self):
        sysname = "mysys3"
        ureg = UnitRegistry()

        g = ureg.get_group("test-imperial")
        g.add_units("inch", "yard", "pint")
        ureg.get_compatible_units("meter", "test-imperial")

        lines = ["@system %s using test-imperial" % sysname, "pint:meter"]

        s = ureg.System.from_lines(lines, ureg.get_base_units)
        ureg._systems[s.name] = s

        # base_factor, destination_units
        c = ureg.get_base_units("inch", system=sysname)
        self.assertAlmostEqual(c[0], 0.326, places=3)
        self.assertEqual(c[1], {"pint": 1.0 / 3})

        c = ureg.get_base_units("cm", system=sysname)
        self.assertAlmostEqual(c[0], 0.1283, places=3)
        self.assertEqual(c[1], {"pint": 1.0 / 3})

        c = ureg.get_base_units("inch**2", system=sysname)
        self.assertAlmostEqual(c[0], 0.326 ** 2, places=3)
        self.assertEqual(c[1], {"pint": 2.0 / 3})

        c = ureg.get_base_units("cm**2", system=sysname)
        self.assertAlmostEqual(c[0], 0.1283 ** 2, places=3)
        self.assertEqual(c[1], {"pint": 2.0 / 3})
Beispiel #3
0
    def test_get_compatible_units(self):
        sysname = "mysys1"
        ureg = UnitRegistry()

        g = ureg.get_group("test-imperial")

        g.add_units("inch", "yard", "pint")
        c = ureg.get_compatible_units("meter", "test-imperial")
        assert c == frozenset([ureg.inch, ureg.yard])

        lines = ["@system %s using test-imperial" % sysname, "inch"]

        ureg.System.from_lines(lines, lambda x: x)
        c = ureg.get_compatible_units("meter", sysname)
        assert c == frozenset([ureg.inch, ureg.yard])
Beispiel #4
0
    def test_get_compatible_units(self):
        sysname = "mysys1"
        ureg = UnitRegistry()

        g = ureg.get_group("test-imperial")

        g.add_units("inch", "yard", "pint")
        c = ureg.get_compatible_units("meter", "test-imperial")
        self.assertEqual(c, frozenset([ureg.inch, ureg.yard]))

        lines = ["@system %s using test-imperial" % sysname, "inch"]

        s = ureg.System.from_lines(lines, lambda x: x)
        c = ureg.get_compatible_units("meter", sysname)
        self.assertEqual(c, frozenset([ureg.inch, ureg.yard]))
Beispiel #5
0
    def test_get_compatible_units(self):
        ureg = UnitRegistry()

        g = ureg.get_group("test-imperial")
        g.add_units("inch", "yard", "pint")
        c = ureg.get_compatible_units("meter", "test-imperial")
        assert c == frozenset([ureg.inch, ureg.yard])
Beispiel #6
0
    def test_get_base_units_different_exponent(self):
        sysname = 'mysys3'

        ureg = UnitRegistry()

        g = ureg.get_group('test-imperial')
        g.add_units('inch', 'yard', 'pint')
        c = ureg.get_compatible_units('meter', 'test-imperial')

        lines = [
            '@system %s using test-imperial' % sysname,
            'pint:meter',
        ]

        s = System.from_lines(lines, ureg.get_base_units, g._groups_systems)

        # base_factor, destination_units
        c = ureg.get_base_units('inch', system=sysname)
        self.assertAlmostEqual(c[0], 0.326, places=3)
        self.assertEqual(c[1], {'pint': 1. / 3})

        c = ureg.get_base_units('cm', system=sysname)
        self.assertAlmostEqual(c[0], 0.1283, places=3)
        self.assertEqual(c[1], {'pint': 1. / 3})

        c = ureg.get_base_units('inch**2', system=sysname)
        self.assertAlmostEqual(c[0], 0.326**2, places=3)
        self.assertEqual(c[1], {'pint': 2. / 3})

        c = ureg.get_base_units('cm**2', system=sysname)
        self.assertAlmostEqual(c[0], 0.1283**2, places=3)
        self.assertEqual(c[1], {'pint': 2. / 3})
Beispiel #7
0
    def test_get_compatible_units(self):
        ureg = UnitRegistry()

        g = ureg.get_group('test-imperial')
        g.add_units('inch', 'yard', 'pint')
        c = ureg.get_compatible_units('meter', 'test-imperial')
        self.assertEqual(c, frozenset([ureg.inch, ureg.yard]))
Beispiel #8
0
    def test_get_compatible_units(self):
        ureg = UnitRegistry()

        g = ureg.get_group("test-imperial")
        g.add_units("inch", "yard", "pint")
        c = ureg.get_compatible_units("meter", "test-imperial")
        self.assertEqual(c, frozenset([ureg.inch, ureg.yard]))
Beispiel #9
0
    def test_get_base_units_different_exponent(self):
        sysname = 'mysys3'

        ureg = UnitRegistry()

        g = ureg.get_group('test-imperial')
        g.add_units('inch', 'yard', 'pint')
        c = ureg.get_compatible_units('meter', 'test-imperial')

        lines = ['@system %s using test-imperial' % sysname,
                 'pint:meter',
                 ]

        s = System.from_lines(lines, ureg.get_base_units, g._groups_systems)

        # base_factor, destination_units
        c = ureg.get_base_units('inch', system=sysname)
        self.assertAlmostEqual(c[0], 0.326, places=3)
        self.assertEqual(c[1], {'pint': 1./3})

        c = ureg.get_base_units('cm', system=sysname)
        self.assertAlmostEqual(c[0], 0.1283, places=3)
        self.assertEqual(c[1], {'pint': 1./3})

        c = ureg.get_base_units('inch**2', system=sysname)
        self.assertAlmostEqual(c[0], 0.326**2, places=3)
        self.assertEqual(c[1], {'pint': 2./3})

        c = ureg.get_base_units('cm**2', system=sysname)
        self.assertAlmostEqual(c[0], 0.1283**2, places=3)
        self.assertEqual(c[1], {'pint': 2./3})
Beispiel #10
0
    def test_get_compatible_units(self):
        ureg = UnitRegistry()

        g = ureg.get_group('test-imperial')
        g.add_units('inch', 'yard', 'pint')
        c = ureg.get_compatible_units('meter', 'test-imperial')
        self.assertEqual(c, frozenset([ureg.inch, ureg.yard]))
Beispiel #11
0
    def test_get_base_units_different_exponent(self):
        sysname = "mysys3"

        ureg = UnitRegistry()

        g = ureg.get_group("test-imperial")
        g.add_units("inch", "yard", "pint")
        c = ureg.get_compatible_units("meter", "test-imperial")

        lines = ["@system %s using test-imperial" % sysname, "pint:meter"]

        s = ureg.System.from_lines(lines, ureg.get_base_units)
        ureg._systems[s.name] = s

        # base_factor, destination_units
        c = ureg.get_base_units("inch", system=sysname)
        self.assertAlmostEqual(c[0], 0.326, places=3)
        self.assertEqual(c[1], {"pint": 1.0 / 3})

        c = ureg.get_base_units("cm", system=sysname)
        self.assertAlmostEqual(c[0], 0.1283, places=3)
        self.assertEqual(c[1], {"pint": 1.0 / 3})

        c = ureg.get_base_units("inch**2", system=sysname)
        self.assertAlmostEqual(c[0], 0.326 ** 2, places=3)
        self.assertEqual(c[1], {"pint": 2.0 / 3})

        c = ureg.get_base_units("cm**2", system=sysname)
        self.assertAlmostEqual(c[0], 0.1283 ** 2, places=3)
        self.assertEqual(c[1], {"pint": 2.0 / 3})
Beispiel #12
0
    def test_get_compatible_units(self):
        sysname = 'mysys1'
        ureg = UnitRegistry()

        g = ureg.get_group('test-imperial')

        g.add_units('inch', 'yard', 'pint')
        c = ureg.get_compatible_units('meter', 'test-imperial')
        self.assertEqual(c, frozenset([ureg.inch, ureg.yard]))

        lines = ['@system %s using test-imperial' % sysname,
                 'inch',
                 ]

        s = System.from_lines(lines, lambda x: x, g._groups_systems)
        c = ureg.get_compatible_units('meter', sysname)
        self.assertEqual(c, frozenset([ureg.inch, ureg.yard]))
Beispiel #13
0
    def test_one_context(self):
        ureg = UnitRegistry()

        add_ctxs(ureg)

        q = 500 * ureg.meter
        s = (ureg.speed_of_light / q).to('Hz')

        meter_units = ureg.get_compatible_units(ureg.meter)
        hertz_units = ureg.get_compatible_units(ureg.hertz)

        self.assertRaises(ValueError, q.to, 'Hz')
        with ureg.context('lc'):
            self.assertEqual(q.to('Hz'), s)
            self.assertEqual(ureg.get_compatible_units(q), meter_units | hertz_units)
        self.assertRaises(ValueError, q.to, 'Hz')
        self.assertEqual(ureg.get_compatible_units(q), meter_units)
Beispiel #14
0
    def test_get_compatible_units(self):
        sysname = 'mysys1'
        ureg = UnitRegistry()

        g = ureg.get_group('test-imperial')

        g.add_units('inch', 'yard', 'pint')
        c = ureg.get_compatible_units('meter', 'test-imperial')
        self.assertEqual(c, frozenset([ureg.inch, ureg.yard]))

        lines = [
            '@system %s using test-imperial' % sysname,
            'inch',
        ]

        s = System.from_lines(lines, lambda x: x, g._groups_systems)
        c = ureg.get_compatible_units('meter', sysname)
        self.assertEqual(c, frozenset([ureg.inch, ureg.yard]))
Beispiel #15
0
 def test_issue912(self):
     """pprint.pformat() invokes sorted() on large sets and frozensets and graciously
     handles TypeError, but not generic Exceptions. This test will fail if
     pint.DimensionalityError stops being a subclass of TypeError.
     """
     ureg = UnitRegistry()
     meter_units = ureg.get_compatible_units(ureg.meter)
     hertz_units = ureg.get_compatible_units(ureg.hertz)
     pprint.pformat(meter_units | hertz_units)
Beispiel #16
0
    def test_one_context(self):
        ureg = UnitRegistry()

        add_ctxs(ureg)

        q = 500 * ureg.meter
        s = (ureg.speed_of_light / q).to("Hz")

        meter_units = ureg.get_compatible_units(ureg.meter)
        hertz_units = ureg.get_compatible_units(ureg.hertz)

        self.assertRaises(DimensionalityError, q.to, "Hz")
        with ureg.context("lc"):
            self.assertEqual(q.to("Hz"), s)
            self.assertEqual(ureg.get_compatible_units(q),
                             meter_units | hertz_units)
        self.assertRaises(DimensionalityError, q.to, "Hz")
        self.assertEqual(ureg.get_compatible_units(q), meter_units)
Beispiel #17
0
    def test_one_context(self):
        ureg = UnitRegistry()

        add_ctxs(ureg)

        q = 500 * ureg.meter
        s = (ureg.speed_of_light / q).to('Hz')

        meter_units = ureg.get_compatible_units(ureg.meter)
        hertz_units = ureg.get_compatible_units(ureg.hertz)

        self.assertRaises(ValueError, q.to, 'Hz')
        with ureg.context('lc'):
            self.assertEqual(q.to('Hz'), s)
            self.assertEqual(ureg.get_compatible_units(q),
                             meter_units | hertz_units)
        self.assertRaises(ValueError, q.to, 'Hz')
        self.assertEqual(ureg.get_compatible_units(q), meter_units)
Beispiel #18
0
    def test_one_context(self, func_registry):
        ureg = UnitRegistry()

        add_ctxs(ureg)

        q = 500 * ureg.meter
        s = (ureg.speed_of_light / q).to("Hz")

        meter_units = ureg.get_compatible_units(ureg.meter)
        hertz_units = ureg.get_compatible_units(ureg.hertz)

        with pytest.raises(DimensionalityError):
            q.to("Hz")
        with ureg.context("lc"):
            assert q.to("Hz") == s
            assert ureg.get_compatible_units(q) == meter_units | hertz_units
        with pytest.raises(DimensionalityError):
            q.to("Hz")
        assert ureg.get_compatible_units(q) == meter_units