1 |
11
|
aaronmk
|
# Useful functions and classes
|
2 |
|
|
|
3 |
341
|
aaronmk
|
def type_name(value): return type(value).__name__
|
4 |
|
|
|
5 |
135
|
aaronmk
|
def module(value): return type(value).__module__.split('.')
|
6 |
|
|
|
7 |
|
|
def root_module(value): return module(value)[0]
|
8 |
|
|
|
9 |
|
|
def first(iter_): return iter_.next()
|
10 |
|
|
|
11 |
133
|
aaronmk
|
def skip(iter_, func):
|
12 |
|
|
# Advance iter while func is True
|
13 |
|
|
try:
|
14 |
|
|
while func(iter_.curr()): iter_.next()
|
15 |
|
|
except StopIteration: pass # nothing after the matching elements
|
16 |
|
|
|
17 |
330
|
aaronmk
|
def rename_key(dict_, orig, new): dict_[new] = dict_.pop(orig)
|
18 |
466
|
aaronmk
|
|
19 |
467
|
aaronmk
|
def dict_subset(dict_, keys):
|
20 |
|
|
subset = dict()
|
21 |
|
|
for key in keys:
|
22 |
|
|
try: subset[key] = dict_[key]
|
23 |
|
|
except KeyError: pass
|
24 |
|
|
return subset
|