Project

General

Profile

1
# Dictionaries
2

    
3
import itertools
4

    
5
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
class MergeDict:
15
    '''A dict that checks each of several dicts'''
16
    
17
    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
(10-10/33)