diff options
author | Hernan Grecco <hernan.grecco@gmail.com> | 2013-12-14 23:17:27 -0300 |
---|---|---|
committer | Hernan Grecco <hernan.grecco@gmail.com> | 2013-12-14 23:17:27 -0300 |
commit | 0dfe9716977159c76161403d0407fbbf3edfae12 (patch) | |
tree | f623b48b91a6d6cf705d8ad50e14422e9497871f | |
parent | 85550df2f3d8e864d61d625fd70f7ac26793a106 (diff) | |
download | pint-0dfe9716977159c76161403d0407fbbf3edfae12.tar.gz |
Dimensions are transformed to base upon context parsing
-rw-r--r-- | pint/context.py | 8 | ||||
-rw-r--r-- | pint/testsuite/test_contexts.py | 25 | ||||
-rw-r--r-- | pint/unit.py | 8 |
3 files changed, 28 insertions, 13 deletions
diff --git a/pint/context.py b/pint/context.py index 1ae4525..b4f2cbb 100644 --- a/pint/context.py +++ b/pint/context.py @@ -105,7 +105,7 @@ class Context(object): return context @classmethod - def from_lines(cls, lines): + def from_lines(cls, lines, to_base_func=None): header, lines = lines[0], lines[1:] r = _header_re.search(header) @@ -141,10 +141,16 @@ class Context(object): if '<->' in rel: src, dst = (ParserHelper.from_string(s) for s in rel.split('<->')) + if to_base_func: + src = to_base_func(src) + dst = to_base_func(dst) ctx.add_transformation(src, dst, func) ctx.add_transformation(dst, src, func) elif '->' in rel: src, dst = (ParserHelper.from_string(s) for s in rel.split('->')) + if to_base_func: + src = to_base_func(src) + dst = to_base_func(dst) ctx.add_transformation(src, dst, func) else: raise ValueError('Relationships must be specified with <-> or ->.') diff --git a/pint/testsuite/test_contexts.py b/pint/testsuite/test_contexts.py index 116546b..9c4e4aa 100644 --- a/pint/testsuite/test_contexts.py +++ b/pint/testsuite/test_contexts.py @@ -599,13 +599,18 @@ class TestDefinedContexts(TestCase): with ureg.context('sp'): from pint.util import find_shortest_path for a, b in itertools.product(eq, eq): - 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.assertAlmostEqualRelError(a, b, rel=.01, msg=msg) - - - #for a, b in itertools.product(eq, eq): - # self.assertAlmostEqualRelError(a.to(b.units, ctx='sp'), b, rel=.01, msg=msg) + 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.assertAlmostEqualRelError(b, a, rel=.01, msg=msg) + + + for a, b in itertools.product(eq, eq): + self.assertAlmostEqualRelError(a.to(b.units, 'sp'), b, rel=.01, msg=msg) diff --git a/pint/unit.py b/pint/unit.py index 9f32416..fc34680 100644 --- a/pint/unit.py +++ b/pint/unit.py @@ -633,7 +633,8 @@ class UnitRegistry(object): with open(file, encoding='utf-8') as fp: return self.load_definitions(fp, is_resource) except Exception as e: - raise ValueError('While opening {}\n{}'.format(file, e.message)) + msg = getattr(e, 'message', str(e)) + raise ValueError('While opening {}\n{}'.format(file, msg)) ifile = enumerate(file) for no, line in ifile: @@ -655,7 +656,10 @@ class UnitRegistry(object): for no, line in ifile: line = line.strip() if line.startswith('@end'): - self.add_context(Context.from_lines(context)) + try: + self.add_context(Context.from_lines(context, self.get_dimensionality)) + except KeyError as e: + raise ValueError('Unknown dimension {}'.format(str(e))) break elif line.startswith('@'): raise ValueError('In line {}, cannot nest @ directives:\n{}'.format(no, line)) |