Project

General

Profile

« Previous | Next » 

Revision 1068

util.py: Added DefaultDict to wrap collections.defaultdict with a simple value passed in the constructor, defaulting to None. Added dict_subset_right_join() to fill in None for subset keys that don't exist.

View differences:

util.py
1 1
# Useful functions and classes
2 2

  
3
import collections
4

  
3 5
#### Function wrappers for statements
4 6

  
5 7
def noop(*args, **kw_args): pass
......
89 91
        try: subset[key] = dict_[key]
90 92
        except KeyError: pass
91 93
    return subset
94

  
95
class DefaultDict(collections.defaultdict):
96
    def __init__(self, dict_, default=None):
97
        collections.defaultdict.__init__(self, lambda: default, dict_)
98

  
99
def dict_subset_right_join(dict_, keys):
100
    '''Gets a subset of a dict, using None for subset keys that don't exist'''
101
    return dict_subset(DefaultDict(dict_), keys)

Also available in: Unified diff