Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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 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: 1 / x)
d.add_transformation(b, a, lambda ureg, x: 1 / 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_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']))
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]))
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_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 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 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)