How to use the pint.unit.UnitsContainer function in Pint

To help you get started, we’ve selected a few Pint examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github hgrecco / pint / pint / testsuite / test_non_int.py View on Github external
value = self.NON_INT_TYPE("4.2")

        for args in (
            (value, "meter"),
            (value, UnitsContainer(meter=1)),
            (value, self.ureg.meter),
            ("4.2*meter",),
            ("4.2/meter**(-1)",),
            (self.Q_(value, "meter"),),
        ):
            x = self.Q_(*args)
            self.assertEqual(x.magnitude, value)
            self.assertEqual(x.units, self.ureg.UnitsContainer(meter=1))

        x = self.Q_(value, UnitsContainer(length=1))
        y = self.Q_(x)
        self.assertEqual(x.magnitude, y.magnitude)
        self.assertEqual(x.units, y.units)
        self.assertIsNot(x, y)

        x = self.Q_(value, None)
        self.assertEqual(x.magnitude, value)
        self.assertEqual(x.units, UnitsContainer())

        with self.capture_log() as buffer:
            self.assertEqual(
                value * self.ureg.meter,
                self.Q_(value, self.NON_INT_TYPE("2") * self.ureg.meter),
            )
            self.assertEqual(len(buffer), 1)
github suavecode / SUAVE / trunk / SUAVE / Plugins / pint / testsuite / test_quantity.py View on Github external
def test_get_dimensionality(self):
        get = self.ureg.get_dimensionality
        self.assertEqual(get('[time]'), UnitsContainer({'[time]': 1}))
        self.assertEqual(get(UnitsContainer({'[time]': 1})), UnitsContainer({'[time]': 1}))
        self.assertEqual(get('seconds'), UnitsContainer({'[time]': 1}))
        self.assertEqual(get(UnitsContainer({'seconds': 1})), UnitsContainer({'[time]': 1}))
        self.assertEqual(get('[speed]'), UnitsContainer({'[length]': 1, '[time]': -1}))
        self.assertEqual(get('[acceleration]'), UnitsContainer({'[length]': 1, '[time]': -2}))
github suavecode / SUAVE / trunk / SUAVE / Plugins / pint / testsuite / test_unit.py View on Github external
def test_unitcontainer_comp(self):
        x = UnitsContainer(meter=1, second=2)
        y = UnitsContainer(meter=1., second=2)
        z = UnitsContainer(meter=1, second=3)
        self.assertTrue(x == y)
        self.assertFalse(x != y)
        self.assertFalse(x == z)
        self.assertTrue(x != z)
github pymedusa / Medusa / ext / pint / testsuite / test_quantity.py View on Github external
def test_exponent_formatting(self):
        ureg = UnitRegistry()
        x = ureg.Quantity(1e20, UnitsContainer(meter=1))
        self.assertEqual("{:~H}".format(x), "1×10<sup>20</sup> m")
        self.assertEqual("{:~L}".format(x), r"1\times 10^{20}\ \mathrm{m}")
        x /= 1e40
        self.assertEqual("{:~H}".format(x), "1×10<sup>-20</sup> m")
        self.assertEqual("{:~L}".format(x), r"1\times 10^{-20}\ \mathrm{m}")
github suavecode / SUAVE / trunk / SUAVE / Plugins / pint / testsuite / test_contexts.py View on Github external
def test_defined(self):
        ureg = self.ureg
        with ureg.context('sp'):
            pass

        a = Context.__keytransform__(UnitsContainer({'[time]': -1.}), UnitsContainer({'[length]': 1.}))
        b = Context.__keytransform__(UnitsContainer({'[length]': 1.}), UnitsContainer({'[time]': -1.}))
        self.assertIn(a, ureg._contexts['sp'].funcs)
        self.assertIn(b, ureg._contexts['sp'].funcs)
        with ureg.context('sp'):
            self.assertIn(a, ureg._active_ctx)
            self.assertIn(b, ureg._active_ctx)
github pymedusa / Medusa / ext / pint / testsuite / test_issues.py View on Github external
def test_issue523(self):
        ureg = UnitRegistry()
        src, dst = UnitsContainer({'meter': 1}), UnitsContainer({'degF': 1})
        value = 10.
        convert = self.ureg.convert
        self.assertRaises(DimensionalityError, convert, value, src, dst)
        self.assertRaises(DimensionalityError, convert, value, dst, src)
github suavecode / SUAVE / trunk / SUAVE / Plugins / pint / testsuite / test_unit.py View on Github external
def test_unitcontainer_arithmetic(self):
        x = UnitsContainer(meter=1)
        y = UnitsContainer(second=1)
        z = UnitsContainer(meter=1, second=-2)

        self._test_not_inplace(op.mul, x, y, UnitsContainer(meter=1, second=1))
        self._test_not_inplace(op.truediv, x, y, UnitsContainer(meter=1, second=-1))
        self._test_not_inplace(op.pow, z, 2, UnitsContainer(meter=2, second=-4))
        self._test_not_inplace(op.pow, z, -2, UnitsContainer(meter=-2, second=4))

        self._test_inplace(op.imul, x, y, UnitsContainer(meter=1, second=1))
        self._test_inplace(op.itruediv, x, y, UnitsContainer(meter=1, second=-1))
        self._test_inplace(op.ipow, z, 2, UnitsContainer(meter=2, second=-4))
        self._test_inplace(op.ipow, z, -2, UnitsContainer(meter=-2, second=4))
github suavecode / SUAVE / trunk / SUAVE / Plugins / pint / testsuite / test_quantity.py View on Github external
def test_dimensionality(self):
        x = self.Q_(42, 'centimeter')
        x.to_base_units()
        x = self.Q_(42, 'meter*second')
        self.assertEqual(x.dimensionality, UnitsContainer({'[length]': 1., '[time]': 1.}))
        x = self.Q_(42, 'meter*second*second')
        self.assertEqual(x.dimensionality, UnitsContainer({'[length]': 1., '[time]': 2.}))
        x = self.Q_(42, 'inch*second*second')
        self.assertEqual(x.dimensionality, UnitsContainer({'[length]': 1., '[time]': 2.}))
        self.assertTrue(self.Q_(42, None).dimensionless)
        self.assertFalse(self.Q_(42, 'meter').dimensionless)
        self.assertTrue((self.Q_(42, 'meter') / self.Q_(1, 'meter')).dimensionless)
        self.assertFalse((self.Q_(42, 'meter') / self.Q_(1, 'second')).dimensionless)
        self.assertTrue((self.Q_(42, 'meter') / self.Q_(1, 'inch')).dimensionless)
github hgrecco / pint / pint / quantity.py View on Github external
if self.__used:
            if not _only_multiplicative_units(self):
                new_self = self.to_base_units()

        if _check(self, other):
            if not _only_multiplicative_units(other):
                other = other.to_base_units()
            magnitude = magnitude_op(new_self._magnitude, other._magnitude)
            units = units_op(new_self._units, other._units)
        else:
            try:
                other_magnitude = _to_magnitude(other, self.force_ndarray)
            except TypeError:
                return NotImplemented
            magnitude = magnitude_op(new_self._magnitude, other_magnitude)
            units = units_op(new_self._units, UnitsContainer())

        ret = self.__class__(magnitude, units)
        ret.__used = True
        return ret
github hgrecco / pint / pint / quantity.py View on Github external
def to(self, other=None, *contexts, **ctx_kwargs):
        """Return Quantity rescaled to different units.

        :param other: destination units.
        :type other: Quantity, str or dict
        """
        if isinstance(other, string_types):
            other = self._REGISTRY.parse_units(other)
        elif isinstance(other, self.__class__):
            other = copy.copy(other.units)
        elif isinstance(other, UnitsContainer):
            pass
        else:
            other = UnitsContainer(other)

        magnitude = self._convert_magnitude_not_inplace(other, *contexts, **ctx_kwargs)

        return self.__class__(magnitude, other)