Project

General

Profile

« Previous | Next » 

Revision 2652

util.py: DefaultDict: Use dicts.DictProxy instead of collections.defaultdict so that it provides a view of the given dict instead of copying it

View differences:

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

  
3
import collections
4

  
5
import dicts
3 6
import objects
4 7

  
5
import collections
6

  
7 8
#### Function wrappers for statements
8 9

  
9 10
def noop(*args, **kw_args): pass
......
187 188
def have_same_value(dict_, *keys):
188 189
    return all_equal_ignore_none([dict_.get(k, None) for k in keys])
189 190

  
190
class DefaultDict(collections.defaultdict):
191
class DefaultDict(dicts.DictProxy):
192
    '''Unlike collections.defaultdict, provides a view of the given dict instead
193
    of copying it.'''
191 194
    def __init__(self, dict_, default=None):
192
        collections.defaultdict.__init__(self, lambda: default, dict_)
195
        dicts.DictProxy.__init__(self, dict_)
196
        
197
        self.default = default
198
    
199
    def __getitem__(self, key): return self.inner.get(key, self.default)
193 200

  
194 201
def dict_subset_right_join(dict_, keys):
195 202
    '''Gets a subset of a dict, using None for subset keys that don't exist'''

Also available in: Unified diff