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