Project

General

Profile

1
# Useful functions and classes
2

    
3
def type_name(value): return type(value).__name__
4

    
5
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
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
def rename_key(dict_, orig, new): dict_[new] = dict_.pop(orig)
18

    
19
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
(11-11/14)