Project

General

Profile

1
# Dictionaries
2

    
3
import itertools
4

    
5
import util
6

    
7
class IdDict(dict):
8
    '''A dict that stores objects by id()'''
9
    
10
    def add(self, *values):
11
        for value in values: self[id(value)] = value
12
        return self
13
    
14
    def add_vars(self, vars_): return self.add(*vars_.values())
15

    
16
class MergeDict:
17
    '''A dict that checks each of several dicts'''
18
    
19
    def __init__(self, *dicts): self.dicts = dicts
20
    
21
    def __getitem__(self, key):
22
        for dict_ in self.dicts:
23
            try: return dict_[key]
24
            except KeyError: pass
25
        raise # reraise last KeyError
26

    
27

    
28
class AttrsDictView:
29
    '''A dict view of an object's attributes
30
    @pre If you want __iter__() to work, value must have a __dict__
31
    '''
32
    
33
    def __init__(self, value): self.value = value
34
    
35
    def __iter__(self): return iter(self.value.__dict__)
36
    
37
    def __getitem__(self, key): return getattr(self.value, key)
38

    
39
def make_hashable(value):
40
    if isinstance(value, list): value = tuple(value)
41
    elif isinstance(value, dict): value = util.NamedTuple(**value)
42
    return value
43

    
44
def join(dict0, dict1):
45
    '''Joins dict0 (A->B mapping) to dict1 (B->C mapping) SQL-style.
46
    If a value in dict0 has no key in dict1, uses the value itself.
47
    '''
48
    new_dict = {}
49
    for out, in_ in dict0.iteritems():
50
        new_dict[out] = dict1.get(in_, in_) # use in_ if no match found
51
    return new_dict
(11-11/36)