Project

General

Profile

1 1876 aaronmk
# Dictionaries
2
3 1885 aaronmk
import itertools
4 2651 aaronmk
import UserDict
5 1876 aaronmk
6 2657 aaronmk
def is_dict(value): return hasattr(value, 'keys')
7 2649 aaronmk
8 2651 aaronmk
class DictProxy(UserDict.DictMixin):
9
    '''A proxy that forwards all accesses to an inner dict.'''
10
11 3720 aaronmk
    def __init__(self, inner=None):
12
        if inner == None: inner = {}
13
        self.inner = inner
14 2651 aaronmk
15
    def keys(self): return self.inner.keys()
16
17
    def __getitem__(self, key): return self.inner[key]
18
19
    def __setitem__(self, key, value): self.inner[key] = value
20 6405 aaronmk
21
    def __delitem__(self, key): del self.inner[key]
22 2651 aaronmk
23 3717 aaronmk
class KeyExistsError(KeyError):
24
    def __init__(self, key):
25
        KeyError.__init__(self, key)
26
27
        self.key = key
28
29
class OnceOnlyDict(DictProxy):
30
    '''A dict that only allows each key to be assigned once (no overwriting).'''
31
32
    def __setitem__(self, key, value):
33
        if key in self.inner: raise KeyExistsError(key)
34
        DictProxy.__setitem__(self, key, value)
35
36 2670 aaronmk
class IdCompared:
37
    '''A value that's compared using `is` instead of ==.
38
    Do not use this for strings as they are interned, causing `is` to have the
39
    same meaning as ==.
40
    '''
41
42
    def __init__(self, value): self.value = value
43
44
    def __eq__(self, other):
45
        return (other != None and other.__class__ == self.__class__
46
            and other.value is self.value)
47
48
    def __hash__(self): return id(self.value)
49
50 1885 aaronmk
class IdDict(dict):
51
    '''A dict that stores objects by id()'''
52
53
    def add(self, *values):
54
        for value in values: self[id(value)] = value
55
        return self
56
57
    def add_vars(self, vars_): return self.add(*vars_.values())
58
59 1876 aaronmk
class MergeDict:
60
    '''A dict that checks each of several dicts'''
61 1885 aaronmk
62 1876 aaronmk
    def __init__(self, *dicts): self.dicts = dicts
63
64
    def __getitem__(self, key):
65
        for dict_ in self.dicts:
66
            try: return dict_[key]
67
            except KeyError: pass
68
        raise # reraise last KeyError
69 1907 aaronmk
70 1911 aaronmk
class AttrsDictView:
71
    '''A dict view of an object's attributes
72
    @pre If you want __iter__() to work, value must have a __dict__
73
    '''
74
75
    def __init__(self, value): self.value = value
76
77
    def __iter__(self): return iter(self.value.__dict__)
78
79
    def __getitem__(self, key): return getattr(self.value, key)
80
81 2817 aaronmk
import util
82
83 1907 aaronmk
def make_hashable(value):
84
    if isinstance(value, list): value = tuple(value)
85
    elif isinstance(value, dict): value = util.NamedTuple(**value)
86
    return value
87 2384 aaronmk
88
def join(dict0, dict1):
89
    '''Joins dict0 (A->B mapping) to dict1 (B->C mapping) SQL-style.
90
    If a value in dict0 has no key in dict1, uses the value itself.
91
    '''
92
    new_dict = {}
93
    for out, in_ in dict0.iteritems():
94 6224 aaronmk
        try: out_value = dict1.get(in_, in_) # use in_ if no match found
95
        except TypeError: out_value = in_ # unhashable type
96
        new_dict[out] = out_value
97 2384 aaronmk
    return new_dict