diff options
| author | Anderson Bravalheri <andersonbravalheri@gmail.com> | 2022-01-06 17:05:16 +0000 |
|---|---|---|
| committer | Anderson Bravalheri <andersonbravalheri@gmail.com> | 2022-01-06 17:05:16 +0000 |
| commit | 71554cb5562ac6f0dfda81f87a4fc55e400aed97 (patch) | |
| tree | 57629342dd041877f38a5062f84d994b6d7446da /setuptools/_distutils/_collections.py | |
| parent | e773611d2d88d46872d3f5b1a84290955034c215 (diff) | |
| parent | 732731afd1e16145f9bf197b28b1e199b3f1fe9e (diff) | |
| download | python-setuptools-git-71554cb5562ac6f0dfda81f87a4fc55e400aed97.tar.gz | |
Merge remote-tracking branch 'upstream/main' into patch-1
Diffstat (limited to 'setuptools/_distutils/_collections.py')
| -rw-r--r-- | setuptools/_distutils/_collections.py | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/setuptools/_distutils/_collections.py b/setuptools/_distutils/_collections.py new file mode 100644 index 00000000..98fce800 --- /dev/null +++ b/setuptools/_distutils/_collections.py @@ -0,0 +1,56 @@ +import collections +import itertools + + +# from jaraco.collections 3.5.1 +class DictStack(list, collections.abc.Mapping): + """ + A stack of dictionaries that behaves as a view on those dictionaries, + giving preference to the last. + + >>> stack = DictStack([dict(a=1, c=2), dict(b=2, a=2)]) + >>> stack['a'] + 2 + >>> stack['b'] + 2 + >>> stack['c'] + 2 + >>> len(stack) + 3 + >>> stack.push(dict(a=3)) + >>> stack['a'] + 3 + >>> set(stack.keys()) == set(['a', 'b', 'c']) + True + >>> set(stack.items()) == set([('a', 3), ('b', 2), ('c', 2)]) + True + >>> dict(**stack) == dict(stack) == dict(a=3, c=2, b=2) + True + >>> d = stack.pop() + >>> stack['a'] + 2 + >>> d = stack.pop() + >>> stack['a'] + 1 + >>> stack.get('b', None) + >>> 'c' in stack + True + """ + + def __iter__(self): + dicts = list.__iter__(self) + return iter(set(itertools.chain.from_iterable(c.keys() for c in dicts))) + + def __getitem__(self, key): + for scope in reversed(tuple(list.__iter__(self))): + if key in scope: + return scope[key] + raise KeyError(key) + + push = list.append + + def __contains__(self, other): + return collections.abc.Mapping.__contains__(self, other) + + def __len__(self): + return len(list(iter(self))) |
