Project

General

Profile

1 11 aaronmk
# Useful functions and classes
2
3 2652 aaronmk
import collections
4
5
import dicts
6 2375 aaronmk
import objects
7
8 931 aaronmk
#### Function wrappers for statements
9
10
def noop(*args, **kw_args): pass
11
12
def and_(a, b): return a and b
13
14 791 aaronmk
#### Object metadata
15
16 341 aaronmk
def type_name(value): return type(value).__name__
17
18 2299 aaronmk
def class_name(value): return value.__class__.__name__
19
20 135 aaronmk
def module(value): return type(value).__module__.split('.')
21
22
def root_module(value): return module(value)[0]
23
24 1233 aaronmk
#### Type checking and conversion
25 791 aaronmk
26
class ConstraintError(ValueError):
27
    def __init__(self, check_func, value):
28 795 aaronmk
        ValueError.__init__(self, str(value)+' must satisfy constraint '
29
            +check_func.__name__)
30 791 aaronmk
31 836 aaronmk
def cast(type_, val):
32 1046 aaronmk
    '''Passes None through. Does not cast a subclass to a superclass (which
33
    doesn't make sense in a dynamically-typed language).'''
34
    if val != None and not isinstance(val, type_): val = type_(val)
35 836 aaronmk
    return val
36
37 1233 aaronmk
def all_not_none(vals): return reduce(and_, map(lambda v: v != None, vals))
38
39 791 aaronmk
def is_str(val): return isinstance(val, basestring)
40
41 1007 aaronmk
def is_list(val): return isinstance(val, list)
42
43 1233 aaronmk
def bool2str(val):
44
    if val: return '1'
45
    else: return ''
46
47 1046 aaronmk
#### Basic types
48
49 1359 aaronmk
def do_ignore_none(do, val):
50
    if val == None: return val
51
    else: return do(val)
52
53 1483 aaronmk
def coalesce(*vals):
54
    for val in vals:
55
        if val != None: return val
56
    return None
57
58 1373 aaronmk
def none_if(val, *none_vals):
59
    for none_val in none_vals:
60
        if cast(type(none_val), val) == none_val: return None
61
    return val
62 1046 aaronmk
63 1445 aaronmk
#### Classes
64
65
def classes_eq(val0, val1): return val0() == val1() # compare instances
66
67 791 aaronmk
#### Iterables
68
69 135 aaronmk
def first(iter_): return iter_.next()
70
71 133 aaronmk
def skip(iter_, func):
72
    # Advance iter while func is True
73
    try:
74
        while func(iter_.curr()): iter_.next()
75
    except StopIteration: pass # nothing after the matching elements
76
77 1147 aaronmk
class WrapIter:
78
    def __init__(self, wrap_func, iterable):
79
        self.wrap_func = wrap_func
80
        self.iter_ = iterable.__iter__()
81
82
    def __iter__(self): return self
83
84
    def next(self): return self.wrap_func(self.iter_.next())
85 934 aaronmk
86 791 aaronmk
class CheckedIter:
87
    def __init__(self, check_func, iterable):
88
        self.check_func = check_func
89
        self.iter_ = iterable.__iter__()
90
91
    def __iter__(self): return self
92
93
    def next(self):
94
        entry = self.iter_.next()
95
        if self.check_func(entry): return entry
96
        else: raise ConstraintError(self.check_func, entry)
97
98 1008 aaronmk
#### Lists
99
100
def list_get(list_, idx, default=None):
101
    try: return list_[idx]
102
    except IndexError: return default
103
104 1414 aaronmk
def list_set_length(list_, len_, fill=None, expand_only=False):
105 1415 aaronmk
    '''@param list_ Must be a list, not a tuple'''
106 1414 aaronmk
    extra_needed = len_ - len(list_)
107
    if extra_needed > 0: list_ += extra_needed*[fill]
108
    elif not expand_only: del list_[len_:]
109
110 1415 aaronmk
def list_as_length(list_, len_, fill=None):
111
    list_ = list(list_) # will also convert a tuple to a list
112
    list_set_length(list_, len_, fill)
113
    return list_
114
115 1281 aaronmk
def list_set(list_, idx, value, default=None):
116 1414 aaronmk
    list_set_length(list_, idx+1, default, expand_only=True)
117 1281 aaronmk
    list_[idx] = value
118
119
def list_setdefault(list_, idx, default=None):
120
    try: return list_[idx]
121
    except IndexError:
122
        list_set(list_, idx, default, default)
123
        return default
124
125 2020 aaronmk
subset_skip = object() # tells list_subset() to leave out invalid indexes
126
127
def list_subset(list_, idxs, default=subset_skip):
128
    '''
129
    @param idxs list|None If None, returns entire list
130
    @param default The value to use for invalid indexes, or subset_skip to leave
131
        them out of the returned list
132
    '''
133 1494 aaronmk
    if idxs == None: return list_
134 1147 aaronmk
    subset = []
135
    for idx in idxs:
136 2020 aaronmk
        value = list_get(list_, idx, default)
137 2254 aaronmk
        if value is not subset_skip: subset.append(value)
138 1147 aaronmk
    return subset
139
140 1012 aaronmk
def list_eq_is(list0, list1):
141
    '''Compares two lists using is'''
142
    if len(list0) != len(list1): return False
143
    for i in xrange(len(list0)):
144
        if list0[i] is not list1[i]: return False
145
    return True
146
147 1344 aaronmk
def list_replace(list_, repl_elem, with_slice):
148
    list_ = list_[:] # don't modify input!
149
    while True:
150
        try: idx = list_.index(repl_elem)
151
        except ValueError: break
152
        list_[idx:idx+1] = with_slice
153
    return list_
154
155 1145 aaronmk
def list_flip(list_): return dict((v, i) for i, v in enumerate(list_))
156
157 1767 aaronmk
def sort_by_len(list_of_lists): list_of_lists.sort(lambda *a: cmp(*map(len, a)))
158
159
def shortest(*lists):
160
    lists = list(lists)
161
    sort_by_len(lists)
162
    return lists[0]
163
164
def longest(*lists):
165
    lists = list(lists)
166
    sort_by_len(lists)
167
    return lists[-1]
168
169 1785 aaronmk
def all_equal(vals, default=False):
170
    '''@return default if len(vals) < 2'''
171
    if len(vals) < 2: return default
172
    return reduce(and_, map(lambda v: v == vals[0], vals), True)
173
174
def all_equal_ignore_none(vals):
175
    return all_equal(filter(lambda v: v != None, vals), default=False)
176
177 791 aaronmk
#### Dicts
178
179 330 aaronmk
def rename_key(dict_, orig, new): dict_[new] = dict_.pop(orig)
180 466 aaronmk
181 467 aaronmk
def dict_subset(dict_, keys):
182
    subset = dict()
183
    for key in keys:
184
        try: subset[key] = dict_[key]
185
        except KeyError: pass
186
    return subset
187 1068 aaronmk
188 1785 aaronmk
def have_same_value(dict_, *keys):
189
    return all_equal_ignore_none([dict_.get(k, None) for k in keys])
190
191 2652 aaronmk
class DefaultDict(dicts.DictProxy):
192
    '''Unlike collections.defaultdict, provides a view of the given dict instead
193
    of copying it.'''
194 1068 aaronmk
    def __init__(self, dict_, default=None):
195 2652 aaronmk
        dicts.DictProxy.__init__(self, dict_)
196
197
        self.default = default
198
199
    def __getitem__(self, key): return self.inner.get(key, self.default)
200 1068 aaronmk
201
def dict_subset_right_join(dict_, keys):
202
    '''Gets a subset of a dict, using None for subset keys that don't exist'''
203
    return dict_subset(DefaultDict(dict_), keys)
204 1147 aaronmk
205
class ListDict:
206 1415 aaronmk
    '''Views a list as a dict, given a key->index mapping
207
    @pre len(list_) == len(keys)'''
208 1147 aaronmk
    def __init__(self, list_, keys, key_idxs=None):
209 1415 aaronmk
        assert len(list_) == len(keys)
210 1147 aaronmk
        if key_idxs == None: key_idxs = list_flip(keys)
211
        self.list = list_
212
        self.keys = keys
213
        self.key_idxs = key_idxs
214
215
    def __getitem__(self, key): return self.list[self.key_idxs[key]]
216
217 1178 aaronmk
    def __str__(self): return '{\n'+(''.join(str(k)+': '+str(v)+'\n'
218
        for k, v in zip(self.keys, self.list)))+'}\n'
219 1443 aaronmk
220
#### Named tuples
221
222 2375 aaronmk
class NamedTuple(objects.BasicObject):
223 1443 aaronmk
    def __init__(self, **attrs): self.__dict__ = attrs
224 1908 aaronmk
225 1910 aaronmk
    def __iter__(self): return iter(self.__dict__)
226 1908 aaronmk
227
    def __getitem__(self, key): return getattr(self, key)