can now import and use sfa.server.sfaapi withough having to worry about registry...
[sfa.git] / sfa / util / defaultdict.py
1 # from http://code.activestate.com/recipes/523034/
2 try:
3     from collections import defaultdict
4 except:
5     class defaultdict(dict):
6         def __init__(self, default_factory=None, *a, **kw):
7             if (default_factory is not None and
8                 not hasattr(default_factory, '__call__')):
9                 raise TypeError('first argument must be callable')
10             dict.__init__(self, *a, **kw)
11             self.default_factory = default_factory
12         def __getitem__(self, key):
13             try:
14                 return dict.__getitem__(self, key)
15             except KeyError:
16                 return self.__missing__(key)
17         def __missing__(self, key):
18             if self.default_factory is None:
19                 raise KeyError(key)
20             self[key] = value = self.default_factory()
21             return value
22         def __reduce__(self):
23             if self.default_factory is None:
24                 args = tuple()
25             else:
26                 args = self.default_factory,
27             return type(self), args, None, None, self.items()
28         def copy(self):
29             return self.__copy__()
30         def __copy__(self):
31             return type(self)(self.default_factory, self)
32         def __deepcopy__(self, memo):
33             import copy
34             return type(self)(self.default_factory,
35                               copy.deepcopy(self.items()))
36         def __repr__(self):
37             return 'defaultdict(%s, %s)' % (self.default_factory,
38                                             dict.__repr__(self))