1 |
11
|
aaronmk
|
# Useful functions and classes
|
2 |
|
|
|
3 |
979
|
aaronmk
|
import locale
|
4 |
|
|
|
5 |
|
|
locale.setlocale(locale.LC_ALL, '') # needed to initialize locale
|
6 |
|
|
|
7 |
931
|
aaronmk
|
#### Function wrappers for statements
|
8 |
|
|
|
9 |
|
|
def noop(*args, **kw_args): pass
|
10 |
|
|
|
11 |
|
|
def and_(a, b): return a and b
|
12 |
|
|
|
13 |
791
|
aaronmk
|
#### Object metadata
|
14 |
|
|
|
15 |
341
|
aaronmk
|
def type_name(value): return type(value).__name__
|
16 |
|
|
|
17 |
135
|
aaronmk
|
def module(value): return type(value).__module__.split('.')
|
18 |
|
|
|
19 |
|
|
def root_module(value): return module(value)[0]
|
20 |
|
|
|
21 |
1007
|
aaronmk
|
#### Type checking
|
22 |
791
|
aaronmk
|
|
23 |
|
|
class ConstraintError(ValueError):
|
24 |
|
|
def __init__(self, check_func, value):
|
25 |
795
|
aaronmk
|
ValueError.__init__(self, str(value)+' must satisfy constraint '
|
26 |
|
|
+check_func.__name__)
|
27 |
791
|
aaronmk
|
|
28 |
836
|
aaronmk
|
def cast(type_, val):
|
29 |
|
|
'''Passes None through'''
|
30 |
|
|
if val != None: val = type_(val)
|
31 |
|
|
return val
|
32 |
|
|
|
33 |
791
|
aaronmk
|
def is_str(val): return isinstance(val, basestring)
|
34 |
|
|
|
35 |
1007
|
aaronmk
|
def is_list(val): return isinstance(val, list)
|
36 |
|
|
|
37 |
791
|
aaronmk
|
#### Iterables
|
38 |
|
|
|
39 |
135
|
aaronmk
|
def first(iter_): return iter_.next()
|
40 |
|
|
|
41 |
133
|
aaronmk
|
def skip(iter_, func):
|
42 |
|
|
# Advance iter while func is True
|
43 |
|
|
try:
|
44 |
|
|
while func(iter_.curr()): iter_.next()
|
45 |
|
|
except StopIteration: pass # nothing after the matching elements
|
46 |
|
|
|
47 |
934
|
aaronmk
|
def list_subset(list_, idxs):
|
48 |
|
|
subset = []
|
49 |
|
|
for idx in idxs:
|
50 |
|
|
try: subset.append(list_[idx])
|
51 |
|
|
except IndexError: pass
|
52 |
|
|
return subset
|
53 |
|
|
|
54 |
791
|
aaronmk
|
class CheckedIter:
|
55 |
|
|
def __init__(self, check_func, iterable):
|
56 |
|
|
self.check_func = check_func
|
57 |
|
|
self.iter_ = iterable.__iter__()
|
58 |
|
|
|
59 |
|
|
def __iter__(self): return self
|
60 |
|
|
|
61 |
|
|
def next(self):
|
62 |
|
|
entry = self.iter_.next()
|
63 |
|
|
if self.check_func(entry): return entry
|
64 |
|
|
else: raise ConstraintError(self.check_func, entry)
|
65 |
|
|
|
66 |
1008
|
aaronmk
|
#### Lists
|
67 |
|
|
|
68 |
|
|
def list_get(list_, idx, default=None):
|
69 |
|
|
try: return list_[idx]
|
70 |
|
|
except IndexError: return default
|
71 |
|
|
|
72 |
1012
|
aaronmk
|
def list_eq_is(list0, list1):
|
73 |
|
|
'''Compares two lists using is'''
|
74 |
|
|
if len(list0) != len(list1): return False
|
75 |
|
|
for i in xrange(len(list0)):
|
76 |
|
|
if list0[i] is not list1[i]: return False
|
77 |
|
|
return True
|
78 |
|
|
|
79 |
791
|
aaronmk
|
#### Dicts
|
80 |
|
|
|
81 |
330
|
aaronmk
|
def rename_key(dict_, orig, new): dict_[new] = dict_.pop(orig)
|
82 |
466
|
aaronmk
|
|
83 |
467
|
aaronmk
|
def dict_subset(dict_, keys):
|
84 |
|
|
subset = dict()
|
85 |
|
|
for key in keys:
|
86 |
|
|
try: subset[key] = dict_[key]
|
87 |
|
|
except KeyError: pass
|
88 |
|
|
return subset
|