diff options
Diffstat (limited to 'pint/context.py')
-rw-r--r-- | pint/context.py | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/pint/context.py b/pint/context.py index 071e89e..78e763f 100644 --- a/pint/context.py +++ b/pint/context.py @@ -18,10 +18,10 @@ from .util import (ParserHelper, UnitsContainer, from .errors import DefinitionSyntaxError #: Regex to match the header parts of a context. -_header_re = re.compile('@context\s*(?P<defaults>\(.*\))?\s+(?P<name>\w+)\s*(=(?P<aliases>.*))*') +_header_re = re.compile(r'@context\s*(?P<defaults>\(.*\))?\s+(?P<name>\w+)\s*(=(?P<aliases>.*))*') #: Regex to match variable names in an equation. -_varname_re = re.compile('[A-Za-z_][A-Za-z0-9_]*') +_varname_re = re.compile(r'[A-Za-z_][A-Za-z0-9_]*') def _expression_to_function(eq): @@ -85,7 +85,7 @@ class Context: newdef = dict(context.defaults, **defaults) c = cls(context.name, context.aliases, newdef) c.funcs = context.funcs - for edge in context.funcs.keys(): + for edge in context.funcs: c.relation_to_context[edge] = c return c return context @@ -194,8 +194,8 @@ class ContextChain(ChainMap): to transform from one dimension to another. """ - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) + def __init__(self): + super().__init__() self._graph = None self._contexts = [] @@ -220,7 +220,7 @@ class ContextChain(ChainMap): @property def defaults(self): if self: - return list(self.maps[0].values())[0].defaults + return next(iter(self.maps[0].values())).defaults return {} @property @@ -240,3 +240,11 @@ class ContextChain(ChainMap): :raises: KeyError if the rule is not found. """ return self[(src, dst)].transform(src, dst, registry, value) + + def context_ids(self): + """Hashable unique identifier of the current contents of the context chain. This + is not implemented as ``__hash__`` as doing so on a mutable object can provoke + unpredictable behaviour, as interpreter-level optimizations can cache the output + of ``__hash__``. + """ + return tuple(id(ctx) for ctx in self._contexts) |