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 |
791
|
aaronmk
|
#### Basic types
|
22 |
|
|
|
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 |
976
|
aaronmk
|
#### Strings
|
36 |
|
|
|
37 |
979
|
aaronmk
|
def format_str(format, val):
|
38 |
|
|
return locale.format_string(format, val, grouping=True)
|
39 |
|
|
|
40 |
987
|
aaronmk
|
def int2str(val): return format_str('%d', val)
|
41 |
|
|
|
42 |
976
|
aaronmk
|
def to_percent(val, sig_figs=2):
|
43 |
|
|
if val >= 1: sig_figs += 1
|
44 |
979
|
aaronmk
|
return format_str('%#.'+str(sig_figs)+'g', val*100)+'%'
|
45 |
976
|
aaronmk
|
|
46 |
980
|
aaronmk
|
def to_si(val, sig_figs=3):
|
47 |
|
|
'''Adds SI prefix to value'''
|
48 |
|
|
prefix = ''
|
49 |
|
|
if val < 1: prefix = 'm'; val *= 1000
|
50 |
|
|
return format_str('%#.'+str(sig_figs)+'g', val)+' '+prefix
|
51 |
|
|
|
52 |
791
|
aaronmk
|
#### Iterables
|
53 |
|
|
|
54 |
135
|
aaronmk
|
def first(iter_): return iter_.next()
|
55 |
|
|
|
56 |
133
|
aaronmk
|
def skip(iter_, func):
|
57 |
|
|
# Advance iter while func is True
|
58 |
|
|
try:
|
59 |
|
|
while func(iter_.curr()): iter_.next()
|
60 |
|
|
except StopIteration: pass # nothing after the matching elements
|
61 |
|
|
|
62 |
934
|
aaronmk
|
def list_subset(list_, idxs):
|
63 |
|
|
subset = []
|
64 |
|
|
for idx in idxs:
|
65 |
|
|
try: subset.append(list_[idx])
|
66 |
|
|
except IndexError: pass
|
67 |
|
|
return subset
|
68 |
|
|
|
69 |
791
|
aaronmk
|
class CheckedIter:
|
70 |
|
|
def __init__(self, check_func, iterable):
|
71 |
|
|
self.check_func = check_func
|
72 |
|
|
self.iter_ = iterable.__iter__()
|
73 |
|
|
|
74 |
|
|
def __iter__(self): return self
|
75 |
|
|
|
76 |
|
|
def next(self):
|
77 |
|
|
entry = self.iter_.next()
|
78 |
|
|
if self.check_func(entry): return entry
|
79 |
|
|
else: raise ConstraintError(self.check_func, entry)
|
80 |
|
|
|
81 |
|
|
#### Dicts
|
82 |
|
|
|
83 |
330
|
aaronmk
|
def rename_key(dict_, orig, new): dict_[new] = dict_.pop(orig)
|
84 |
466
|
aaronmk
|
|
85 |
467
|
aaronmk
|
def dict_subset(dict_, keys):
|
86 |
|
|
subset = dict()
|
87 |
|
|
for key in keys:
|
88 |
|
|
try: subset[key] = dict_[key]
|
89 |
|
|
except KeyError: pass
|
90 |
|
|
return subset
|