def test_parse_simple(self): a = Context.__keytransform__(UnitsContainer({'[time]': -1}), UnitsContainer({'[length]': 1})) b = Context.__keytransform__(UnitsContainer({'[length]': 1}), UnitsContainer({'[time]': -1})) s = ['@context longcontextname', '[length] -> 1 / [time]: c / value', '1 / [time] -> [length]: c / value'] c = Context.from_lines(s) self.assertEqual(c.name, 'longcontextname') self.assertEqual(c.aliases, ()) self.assertEqual(c.defaults, {}) self.assertEqual(set(c.funcs.keys()), set((a, b))) self._test_ctx(c) s = ['@context longcontextname = lc', '[length] <-> 1 / [time]: c / value'] c = Context.from_lines(s) self.assertEqual(c.name, 'longcontextname') self.assertEqual(c.aliases, ('lc', )) self.assertEqual(c.defaults, {}) self.assertEqual(set(c.funcs.keys()), set((a, b))) self._test_ctx(c) s = ['@context longcontextname = lc = lcn', '[length] <-> 1 / [time]: c / value'] c = Context.from_lines(s) self.assertEqual(c.name, 'longcontextname') self.assertEqual(c.aliases, ('lc', 'lcn', )) self.assertEqual(c.defaults, {}) self.assertEqual(set(c.funcs.keys()), set((a, b))) self._test_ctx(c)
def test_parse_simple(self): a = Context.__keytransform__(UnitsContainer({"[time]": -1}), UnitsContainer({"[length]": 1})) b = Context.__keytransform__(UnitsContainer({"[length]": 1}), UnitsContainer({"[time]": -1})) s = ["@context longcontextname", "[length] -> 1 / [time]: c / value", "1 / [time] -> [length]: c / value"] c = Context.from_lines(s) self.assertEqual(c.name, "longcontextname") self.assertEqual(c.aliases, ()) self.assertEqual(c.defaults, {}) self.assertEqual(set(c.funcs.keys()), set((a, b))) self._test_ctx(c) s = ["@context longcontextname = lc", "[length] <-> 1 / [time]: c / value"] c = Context.from_lines(s) self.assertEqual(c.name, "longcontextname") self.assertEqual(c.aliases, ("lc",)) self.assertEqual(c.defaults, {}) self.assertEqual(set(c.funcs.keys()), set((a, b))) self._test_ctx(c) s = ["@context longcontextname = lc = lcn", "[length] <-> 1 / [time]: c / value"] c = Context.from_lines(s) self.assertEqual(c.name, "longcontextname") self.assertEqual(c.aliases, ("lc", "lcn")) self.assertEqual(c.defaults, {}) self.assertEqual(set(c.funcs.keys()), set((a, b))) self._test_ctx(c)
def test_parse_define(self): a = Context.__keytransform__(UnitsContainer({"[time]": -1}), UnitsContainer({"[length]": 1.0})) b = Context.__keytransform__(UnitsContainer({"[length]": 1}), UnitsContainer({"[time]": -1.0})) s = ["@context longcontextname", "[length] <-> 1 / [time]: c / value"] c = Context.from_lines(s) self.assertEqual(c.defaults, {}) self.assertEqual(set(c.funcs.keys()), set((a, b))) self._test_ctx(c)
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)
def test_defined(self): ureg = self.ureg with ureg.context("sp"): pass a = Context.__keytransform__(UnitsContainer({"[time]": -1.0}), UnitsContainer({"[length]": 1.0})) b = Context.__keytransform__(UnitsContainer({"[length]": 1.0}), UnitsContainer({"[time]": -1.0})) 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)
def test_parse_auto_inverse(self): a = Context.__keytransform__(UnitsContainer({'[time]': -1.}), UnitsContainer({'[length]': 1.})) b = Context.__keytransform__(UnitsContainer({'[length]': 1.}), UnitsContainer({'[time]': -1.})) s = ['@context longcontextname', '[length] <-> 1 / [time]: c / value'] c = Context.from_lines(s) self.assertEqual(c.defaults, {}) self.assertEqual(set(c.funcs.keys()), set((a, b))) self._test_ctx(c)
def test_parse_parameterized(self): a = Context.__keytransform__(UnitsContainer({"[time]": -1.0}), UnitsContainer({"[length]": 1.0})) b = Context.__keytransform__(UnitsContainer({"[length]": 1.0}), UnitsContainer({"[time]": -1.0})) s = ["@context(n=1) longcontextname", "[length] <-> 1 / [time]: n * c / value"] c = Context.from_lines(s) self.assertEqual(c.defaults, {"n": 1}) self.assertEqual(set(c.funcs.keys()), set((a, b))) self._test_ctx(c) # If the variable is not present in the definition, then raise an error s = ["@context(n=1) longcontextname", "[length] <-> 1 / [time]: c / value"] self.assertRaises(ValueError, Context.from_lines, s)
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)
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: 1 / x) d.add_transformation(b, a, lambda ureg, x: 1 / x) ureg.add_context(d)
def test_parse_parameterized(self): a = Context.__keytransform__(UnitsContainer({"[time]": -1.0}), UnitsContainer({"[length]": 1.0})) b = Context.__keytransform__(UnitsContainer({"[length]": 1.0}), UnitsContainer({"[time]": -1.0})) s = [ "@context(n=1) longcontextname", "[length] <-> 1 / [time]: n * c / value" ] c = Context.from_lines(s) assert c.defaults == {"n": 1} assert c.funcs.keys() == {a, b} self._test_ctx(c) s = [ "@context(n=1, bla=2) longcontextname", "[length] <-> 1 / [time]: n * c / value / bla", ] c = Context.from_lines(s) assert c.defaults == {"n": 1, "bla": 2} assert c.funcs.keys() == {a, b} # If the variable is not present in the definition, then raise an error s = [ "@context(n=1) longcontextname", "[length] <-> 1 / [time]: c / value" ] with pytest.raises(DefinitionSyntaxError): Context.from_lines(s)
def test_parse_parameterized(self): a = Context.__keytransform__(UnitsContainer({'[time]': -1.}), UnitsContainer({'[length]': 1.})) b = Context.__keytransform__(UnitsContainer({'[length]': 1.}), UnitsContainer({'[time]': -1.})) s = [ '@context(n=1) longcontextname', '[length] <-> 1 / [time]: n * c / value' ] c = Context.from_lines(s) self.assertEqual(c.defaults, {'n': 1}) self.assertEqual(set(c.funcs.keys()), set((a, b))) self._test_ctx(c) # If the variable is not present in the definition, then raise an error s = [ '@context(n=1) longcontextname', '[length] <-> 1 / [time]: c / value' ] self.assertRaises(ValueError, Context.from_lines, s)
def test_parse_parameterized(self): a = Context.__keytransform__(UnitsContainer({'[time]': -1.}), UnitsContainer({'[length]': 1.})) b = Context.__keytransform__(UnitsContainer({'[length]': 1.}), UnitsContainer({'[time]': -1.})) s = ['@context(n=1) longcontextname', '[length] <-> 1 / [time]: n * c / value'] c = Context.from_lines(s) self.assertEqual(c.defaults, {'n': 1}) self.assertEqual(set(c.funcs.keys()), set((a, b))) self._test_ctx(c) s = ['@context(n=1, bla=2) longcontextname', '[length] <-> 1 / [time]: n * c / value / bla'] c = Context.from_lines(s) self.assertEqual(c.defaults, {'n': 1, 'bla': 2}) self.assertEqual(set(c.funcs.keys()), set((a, b))) # If the variable is not present in the definition, then raise an error s = ['@context(n=1) longcontextname', '[length] <-> 1 / [time]: c / value'] self.assertRaises(ValueError, Context.from_lines, s)
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_textile(self): ureg = self.ureg qty_direct = 1.331 * ureg.tex with self.assertRaises(errors.DimensionalityError): qty_indirect = qty_direct.to('Nm') with ureg.context('textile'): from pint.util import find_shortest_path qty_indirect = qty_direct.to('Nm') a = qty_direct.to_base_units() b = qty_indirect.to_base_units() da, db = Context.__keytransform__(a.dimensionality, b.dimensionality) p = find_shortest_path(ureg._active_ctx.graph, da, db) self.assertTrue(p) msg = '{} <-> {}'.format(a, b) self.assertQuantityAlmostEqual(b, a, rtol=0.01, msg=msg)
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: 1 / x) d.add_transformation(b, a, lambda ureg, x: 1 / x) ureg.add_context(d)
def test_spectroscopy(self): ureg = self.ureg eq = (532. * ureg.nm, 563.5 * ureg.terahertz, 2.33053 * ureg.eV) with ureg.context('sp'): from pint.util import find_shortest_path for a, b in itertools.product(eq, eq): for x in range(2): if x == 1: a = a.to_base_units() b = b.to_base_units() da, db = Context.__keytransform__(a.dimensionality, b.dimensionality) p = find_shortest_path(ureg._active_ctx.graph, da, db) self.assertTrue(p) msg = '{} <-> {}'.format(a, b) # assertAlmostEqualRelError converts second to first self.assertQuantityAlmostEqual(b, a, rtol=0.01, msg=msg) for a, b in itertools.product(eq, eq): self.assertQuantityAlmostEqual(a.to(b.units, 'sp'), b, rtol=0.01)
def test_spectroscopy(self): ureg = self.ureg eq = (532.0 * ureg.nm, 563.5 * ureg.terahertz, 2.33053 * ureg.eV) with ureg.context("sp"): from pint.util import find_shortest_path for a, b in itertools.product(eq, eq): for x in range(2): if x == 1: a = a.to_base_units() b = b.to_base_units() da, db = Context.__keytransform__(a.dimensionality, b.dimensionality) p = find_shortest_path(ureg._active_ctx.graph, da, db) self.assertTrue(p) msg = "{0} <-> {1}".format(a, b) # assertAlmostEqualRelError converts second to first self.assertQuantityAlmostEqual(b, a, rtol=0.01, msg=msg) for a, b in itertools.product(eq, eq): self.assertQuantityAlmostEqual(a.to(b.units, "sp"), b, rtol=0.01)
def add_sharedargdef_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", defaults=dict(n=0)) d.add_transformation(a, b, lambda ureg, x, n: ureg.ampere * ureg.meter * n / x) d.add_transformation(b, a, lambda ureg, x, n: ureg.ampere * ureg.meter * n / x) ureg.add_context(d)
def test_parse_invalid(self, badrow): with pytest.raises(DefinitionSyntaxError): Context.from_lines(["@context c", badrow])
def test_err_to_base_unit(): expected = "Can't define base units within a context" with pytest.raises(DefinitionSyntaxError, match=expected): Context.from_lines(["@context c", "x = [d]"])
def test_err_prefix_redefinition(): expected = re.escape( "Expected <unit> = <converter>; got [d1] = [d2] * [d3]") with pytest.raises(DefinitionSyntaxError, match=expected): Context.from_lines(["@context c", "[d1] = [d2] * [d3]"])
def test_err_redefine_alias(subtests): expected = "Can't change a unit's symbol or aliases within a context" for s in ("foo = bar = f", "foo = bar = _ = baz"): with subtests.test(s): with pytest.raises(DefinitionSyntaxError, match=expected): Context.from_lines(["@context c", s])
def test_err_to_base_unit(self): with self.assertRaises(DefinitionSyntaxError) as e: Context.from_lines(["@context c", "x = [d]"]) self.assertEqual(str(e.exception), "Can't define base units within a context")
def test_err_prefix_redefinition(self): with self.assertRaises(DefinitionSyntaxError) as e: Context.from_lines(["@context c", "[d1] = [d2] * [d3]"]) self.assertEqual( str(e.exception), "Expected <unit> = <converter>; got [d1] = [d2] * [d3]")