1 |
1876
|
aaronmk
|
# Dictionaries
|
2 |
|
|
|
3 |
1885
|
aaronmk
|
import itertools
|
4 |
1876
|
aaronmk
|
|
5 |
1885
|
aaronmk
|
class IdDict(dict):
|
6 |
|
|
'''A dict that stores objects by id()'''
|
7 |
|
|
|
8 |
|
|
def add(self, *values):
|
9 |
|
|
for value in values: self[id(value)] = value
|
10 |
|
|
return self
|
11 |
|
|
|
12 |
|
|
def add_vars(self, vars_): return self.add(*vars_.values())
|
13 |
|
|
|
14 |
1876
|
aaronmk
|
class MergeDict:
|
15 |
|
|
'''A dict that checks each of several dicts'''
|
16 |
1885
|
aaronmk
|
|
17 |
1876
|
aaronmk
|
def __init__(self, *dicts): self.dicts = dicts
|
18 |
|
|
|
19 |
|
|
def __getitem__(self, key):
|
20 |
|
|
for dict_ in self.dicts:
|
21 |
|
|
try: return dict_[key]
|
22 |
|
|
except KeyError: pass
|
23 |
|
|
raise # reraise last KeyError
|