1
|
|
|
from collections import MutableMapping |
2
|
|
|
|
3
|
|
|
__all__ = 'ChainMap', |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
class ChainMap(MutableMapping): |
7
|
|
|
''' A ChainMap groups multiple dicts (or other mappings) together |
8
|
|
|
to create a single, updateable view. |
9
|
|
|
|
10
|
|
|
The underlying mappings are stored in a list. That list is public and can |
11
|
|
|
accessed or updated using the *maps* attribute. There is no other state. |
12
|
|
|
|
13
|
|
|
Lookups search the underlying mappings successively until a key is found. |
14
|
|
|
In contrast, writes, updates, and deletions only operate on the first |
15
|
|
|
mapping. |
16
|
|
|
|
17
|
|
|
''' |
18
|
|
|
|
19
|
|
|
def __init__(self, *maps): |
20
|
|
|
'''Initialize a ChainMap by setting *maps* to the given mappings. |
21
|
|
|
If no mappings are provided, a single empty dictionary is used. |
22
|
|
|
|
23
|
|
|
''' |
24
|
|
|
self.maps = list(maps) or [{}] # always at least one map |
25
|
|
|
|
26
|
|
|
def __missing__(self, key): |
27
|
|
|
raise KeyError(key) |
28
|
|
|
|
29
|
|
|
def __getitem__(self, key): |
30
|
|
|
for mapping in self.maps: |
31
|
|
|
try: |
32
|
|
|
return mapping[key] # can't use 'key in mapping' with defaultdict |
33
|
|
|
except KeyError: |
34
|
|
|
pass |
35
|
|
|
return self.__missing__(key) # support subclasses that define __missing__ |
36
|
|
|
|
37
|
|
|
def get(self, key, default=None): |
38
|
|
|
return self[key] if key in self else default |
39
|
|
|
|
40
|
|
|
def __len__(self): |
41
|
|
|
return len(set().union(*self.maps)) # reuses stored hash values if possible |
42
|
|
|
|
43
|
|
|
def __iter__(self): |
44
|
|
|
return iter(set().union(*self.maps)) |
45
|
|
|
|
46
|
|
|
def __contains__(self, key): |
47
|
|
|
return any(key in m for m in self.maps) |
48
|
|
|
|
49
|
|
|
def __bool__(self): |
50
|
|
|
return any(self.maps) |
51
|
|
|
|
52
|
|
|
def __repr__(self): |
53
|
|
|
return '{0.__class__.__name__}({1})'.format( |
54
|
|
|
self, ', '.join(map(repr, self.maps))) |
55
|
|
|
|
56
|
|
|
@classmethod |
57
|
|
|
def fromkeys(cls, iterable, *args): |
58
|
|
|
'Create a ChainMap with a single dict created from the iterable.' |
59
|
|
|
return cls(dict.fromkeys(iterable, *args)) |
60
|
|
|
|
61
|
|
|
def copy(self): |
62
|
|
|
'New ChainMap or subclass with a new copy of maps[0] and refs to maps[1:]' |
63
|
|
|
return self.__class__(self.maps[0].copy(), *self.maps[1:]) |
64
|
|
|
|
65
|
|
|
__copy__ = copy |
66
|
|
|
|
67
|
|
|
def new_child(self, m=None): # like Django's Context.push() |
68
|
|
|
''' |
69
|
|
|
New ChainMap with a new map followed by all previous maps. If no |
70
|
|
|
map is provided, an empty dict is used. |
71
|
|
|
''' |
72
|
|
|
if m is None: |
73
|
|
|
m = {} |
74
|
|
|
return self.__class__(m, *self.maps) |
75
|
|
|
|
76
|
|
|
@property |
77
|
|
|
def parents(self): # like Django's Context.pop() |
78
|
|
|
'New ChainMap from maps[1:].' |
79
|
|
|
return self.__class__(*self.maps[1:]) |
80
|
|
|
|
81
|
|
|
def __setitem__(self, key, value): |
82
|
|
|
self.maps[0][key] = value |
83
|
|
|
|
84
|
|
|
def __delitem__(self, key): |
85
|
|
|
try: |
86
|
|
|
del self.maps[0][key] |
87
|
|
|
except KeyError: |
88
|
|
|
raise KeyError('Key not found in the first mapping: {!r}'.format(key)) |
89
|
|
|
|
90
|
|
|
def popitem(self): |
91
|
|
|
'Remove and return an item pair from maps[0]. Raise KeyError is maps[0] is empty.' |
92
|
|
|
try: |
93
|
|
|
return self.maps[0].popitem() |
94
|
|
|
except KeyError: |
95
|
|
|
raise KeyError('No keys found in the first mapping.') |
96
|
|
|
|
97
|
|
|
def pop(self, key, *args): |
98
|
|
|
'Remove *key* from maps[0] and return its value. Raise KeyError if *key* not in maps[0].' |
99
|
|
|
try: |
100
|
|
|
return self.maps[0].pop(key, *args) |
101
|
|
|
except KeyError: |
102
|
|
|
raise KeyError('Key not found in the first mapping: {!r}'.format(key)) |
103
|
|
|
|
104
|
|
|
def clear(self): |
105
|
|
|
'Clear maps[0], leaving maps[1:] intact.' |
106
|
|
|
self.maps[0].clear() |
107
|
|
|
|