Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_deepcopy(self):
x = self.U_(UnitsContainer(meter=1))
self.assertEqual(x, copy.deepcopy(x))
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)
def test_graph(self):
ureg = UnitRegistry()
add_ctxs(ureg)
l = UnitsContainer({'[length]': 1.})
t = UnitsContainer({'[time]': -1.})
c = UnitsContainer({'[current]': 1.})
g_sp = defaultdict(set)
g_sp.update({l: set((t,)),
t: set((l,))})
g_ab = defaultdict(set)
g_ab.update({l: set((c,)),
c: set((l,))})
g = defaultdict(set)
g.update({l: set((t, c)),
t: set((l,)),
c: set((l,))})
def test_convert_inplace(self):
ureg = self.ureg
# Conversions with single units take a different codepath than
# Conversions with more than one unit.
src_dst1 = UnitsContainer(meter=1), UnitsContainer(inch=1)
src_dst2 = UnitsContainer(meter=1, second=-1), UnitsContainer(inch=1, minute=-1)
for src, dst in (src_dst1, src_dst2):
v = ureg.convert(1, src, dst),
a = np.ones((3, 1))
ac = np.ones((3, 1))
r1 = ureg.convert(a, src, dst)
np.testing.assert_allclose(r1, v * ac)
self.assertIsNot(r1, a)
r2 = ureg.convert(a, src, dst, inplace=True)
np.testing.assert_allclose(r2, v * ac)
self.assertIs(r2, a)
def test_graph_enable(self):
ureg = UnitRegistry()
add_ctxs(ureg)
l = UnitsContainer({"[length]": 1.0}) # noqa: E741
t = UnitsContainer({"[time]": -1.0})
c = UnitsContainer({"[current]": 1.0})
g_sp = defaultdict(set)
g_sp.update({l: {t}, t: {l}})
g_ab = defaultdict(set)
g_ab.update({l: {c}, c: {l}})
g = defaultdict(set)
g.update({l: {t, c}, t: {l}, c: {l}})
ureg.enable_contexts("lc")
self.assertEqual(ureg._active_ctx.graph, g_sp)
ureg.disable_contexts(1)
def test_unitcontainer_bool(self):
self.assertTrue(UnitsContainer(meter=1, second=2))
self.assertFalse(UnitsContainer())
def test_parse_factor(self):
self.assertEqual(self.ureg.parse_expression('42*meter'), self.Q_(42, UnitsContainer(meter=1.)))
self.assertEqual(self.ureg.parse_expression('meter*42'), self.Q_(42, UnitsContainer(meter=1.)))
def test_unit_formatting(self):
x = self.U_(UnitsContainer(meter=2, kilogram=1, second=-1))
for spec, result in (
("{}", str(x)),
("{!s}", str(x)),
("{!r}", repr(x)),
(
"{:L}",
r"\frac{\mathrm{kilogram} \cdot \mathrm{meter}^{2}}{\mathrm{second}}",
),
("{:P}", "kilogram·meter²/second"),
("{:H}", r"\[kilogram\ {meter}^{2}/second\]"),
("{:C}", "kilogram*meter**2/second"),
("{:Lx}", r"\si[]{\kilo\gram\meter\squared\per\second}"),
("{:~}", "kg * m ** 2 / s"),
("{:L~}", r"\frac{\mathrm{kg} \cdot \mathrm{m}^{2}}{\mathrm{s}}"),
("{:P~}", "kg·m²/s"),
("{:H~}", r"\[kg\ {m}^{2}/s\]"),
def format_babel(self, spec='', **kwspec):
spec = spec or self.default_format
if '~' in spec:
if self.dimensionless:
return ''
units = UnitsContainer(dict((self._REGISTRY._get_symbol(key),
value)
for key, value in self._units.items()))
spec = spec.replace('~', '')
else:
units = self._units
return '%s' % (units.format_babel(spec, **kwspec))
def remove(self, keys):
""" Create a new UnitsContainer purged from given keys.
"""
d = udict(self._d)
return UnitsContainer(((key, d[key]) for key in d if key not in keys))