summaryrefslogtreecommitdiff
path: root/pint/context.py
diff options
context:
space:
mode:
authorGuido Imperiale <crusaderky@gmail.com>2019-12-05 11:22:16 +0000
committerGuido Imperiale <crusaderky@gmail.com>2019-12-05 11:22:16 +0000
commit84d218c6570ba00385b6e388f51f887a003d149b (patch)
treec314027ea877b56fe389b0fcd87a20bdc0136f4a /pint/context.py
parent50285dc969e6859f758ef1ef62ceb276c08c9d0c (diff)
parent616ea8c398ca3dda25842ac0aa3f9e992f6a39ca (diff)
downloadpint-84d218c6570ba00385b6e388f51f887a003d149b.tar.gz
Merge branch 'master' into py27_cleanup
Diffstat (limited to 'pint/context.py')
-rw-r--r--pint/context.py20
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)