1 |
11
|
aaronmk
|
# Useful functions and classes
|
2 |
|
|
|
3 |
1068
|
aaronmk
|
import collections
|
4 |
|
|
|
5 |
931
|
aaronmk
|
#### Function wrappers for statements
|
6 |
|
|
|
7 |
|
|
def noop(*args, **kw_args): pass
|
8 |
|
|
|
9 |
|
|
def and_(a, b): return a and b
|
10 |
|
|
|
11 |
791
|
aaronmk
|
#### Object metadata
|
12 |
|
|
|
13 |
341
|
aaronmk
|
def type_name(value): return type(value).__name__
|
14 |
|
|
|
15 |
135
|
aaronmk
|
def module(value): return type(value).__module__.split('.')
|
16 |
|
|
|
17 |
|
|
def root_module(value): return module(value)[0]
|
18 |
|
|
|
19 |
1007
|
aaronmk
|
#### Type checking
|
20 |
791
|
aaronmk
|
|
21 |
|
|
class ConstraintError(ValueError):
|
22 |
|
|
def __init__(self, check_func, value):
|
23 |
795
|
aaronmk
|
ValueError.__init__(self, str(value)+' must satisfy constraint '
|
24 |
|
|
+check_func.__name__)
|
25 |
791
|
aaronmk
|
|
26 |
836
|
aaronmk
|
def cast(type_, val):
|
27 |
1046
|
aaronmk
|
'''Passes None through. Does not cast a subclass to a superclass (which
|
28 |
|
|
doesn't make sense in a dynamically-typed language).'''
|
29 |
|
|
if val != None and not isinstance(val, type_): val = type_(val)
|
30 |
836
|
aaronmk
|
return val
|
31 |
|
|
|
32 |
791
|
aaronmk
|
def is_str(val): return isinstance(val, basestring)
|
33 |
|
|
|
34 |
1007
|
aaronmk
|
def is_list(val): return isinstance(val, list)
|
35 |
|
|
|
36 |
1046
|
aaronmk
|
#### Basic types
|
37 |
|
|
|
38 |
|
|
def none_if(val, none_val):
|
39 |
|
|
if cast(type(none_val), val) == none_val: return None
|
40 |
|
|
else: return val
|
41 |
|
|
|
42 |
791
|
aaronmk
|
#### Iterables
|
43 |
|
|
|
44 |
135
|
aaronmk
|
def first(iter_): return iter_.next()
|
45 |
|
|
|
46 |
133
|
aaronmk
|
def skip(iter_, func):
|
47 |
|
|
# Advance iter while func is True
|
48 |
|
|
try:
|
49 |
|
|
while func(iter_.curr()): iter_.next()
|
50 |
|
|
except StopIteration: pass # nothing after the matching elements
|
51 |
|
|
|
52 |
1147
|
aaronmk
|
class WrapIter:
|
53 |
|
|
def __init__(self, wrap_func, iterable):
|
54 |
|
|
self.wrap_func = wrap_func
|
55 |
|
|
self.iter_ = iterable.__iter__()
|
56 |
|
|
|
57 |
|
|
def __iter__(self): return self
|
58 |
|
|
|
59 |
|
|
def next(self): return self.wrap_func(self.iter_.next())
|
60 |
934
|
aaronmk
|
|
61 |
791
|
aaronmk
|
class CheckedIter:
|
62 |
|
|
def __init__(self, check_func, iterable):
|
63 |
|
|
self.check_func = check_func
|
64 |
|
|
self.iter_ = iterable.__iter__()
|
65 |
|
|
|
66 |
|
|
def __iter__(self): return self
|
67 |
|
|
|
68 |
|
|
def next(self):
|
69 |
|
|
entry = self.iter_.next()
|
70 |
|
|
if self.check_func(entry): return entry
|
71 |
|
|
else: raise ConstraintError(self.check_func, entry)
|
72 |
|
|
|
73 |
1008
|
aaronmk
|
#### Lists
|
74 |
|
|
|
75 |
|
|
def list_get(list_, idx, default=None):
|
76 |
|
|
try: return list_[idx]
|
77 |
|
|
except IndexError: return default
|
78 |
|
|
|
79 |
1147
|
aaronmk
|
def list_subset(list_, idxs):
|
80 |
|
|
subset = []
|
81 |
|
|
for idx in idxs:
|
82 |
|
|
try: subset.append(list_[idx])
|
83 |
|
|
except IndexError: pass
|
84 |
|
|
return subset
|
85 |
|
|
|
86 |
1012
|
aaronmk
|
def list_eq_is(list0, list1):
|
87 |
|
|
'''Compares two lists using is'''
|
88 |
|
|
if len(list0) != len(list1): return False
|
89 |
|
|
for i in xrange(len(list0)):
|
90 |
|
|
if list0[i] is not list1[i]: return False
|
91 |
|
|
return True
|
92 |
|
|
|
93 |
1145
|
aaronmk
|
def list_flip(list_): return dict((v, i) for i, v in enumerate(list_))
|
94 |
|
|
|
95 |
791
|
aaronmk
|
#### Dicts
|
96 |
|
|
|
97 |
330
|
aaronmk
|
def rename_key(dict_, orig, new): dict_[new] = dict_.pop(orig)
|
98 |
466
|
aaronmk
|
|
99 |
467
|
aaronmk
|
def dict_subset(dict_, keys):
|
100 |
|
|
subset = dict()
|
101 |
|
|
for key in keys:
|
102 |
|
|
try: subset[key] = dict_[key]
|
103 |
|
|
except KeyError: pass
|
104 |
|
|
return subset
|
105 |
1068
|
aaronmk
|
|
106 |
|
|
class DefaultDict(collections.defaultdict):
|
107 |
|
|
def __init__(self, dict_, default=None):
|
108 |
|
|
collections.defaultdict.__init__(self, lambda: default, dict_)
|
109 |
|
|
|
110 |
|
|
def dict_subset_right_join(dict_, keys):
|
111 |
|
|
'''Gets a subset of a dict, using None for subset keys that don't exist'''
|
112 |
|
|
return dict_subset(DefaultDict(dict_), keys)
|
113 |
1147
|
aaronmk
|
|
114 |
|
|
class ListDict:
|
115 |
|
|
'''Views a list as a dict, given a key->index mapping'''
|
116 |
|
|
def __init__(self, list_, keys, key_idxs=None):
|
117 |
|
|
assert len(keys) == len(list_)
|
118 |
|
|
if key_idxs == None: key_idxs = list_flip(keys)
|
119 |
|
|
self.list = list_
|
120 |
|
|
self.keys = keys
|
121 |
|
|
self.key_idxs = key_idxs
|
122 |
|
|
|
123 |
|
|
def __getitem__(self, key): return self.list[self.key_idxs[key]]
|
124 |
|
|
|
125 |
|
|
def __str__(self): return str(dict(zip(self.keys, self.list)))
|