-
-
Save allenyllee/818c302249b491c057e00ac3dd00bf2b to your computer and use it in GitHub Desktop.
emulation of collections.defaultdict
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
emulation of collections.defaultdict | |
""" | |
class defaultdict(dict): | |
""" | |
emulation of collections.defaultdict | |
to test, run python defaultdict.py -v | |
>>> dd = defaultdict(list) | |
>>> dd[1].append(2) | |
>>> dd | |
defaultdict(<class 'list'>, {1: [2]}) | |
>>> dd = defaultdict(defaultdict) | |
>>> dd['a']={'key1':'value1'} | |
>>> dd | |
defaultdict(<class '__main__.defaultdict'>, {'a': {'key1': 'value1'}}) | |
>>> dd['b'].update({'key2':'value2'}) | |
>>> dd | |
defaultdict(<class '__main__.defaultdict'>, {'a': {'key1': 'value1'}, 'b': defaultdict(None, {'key2': 'value2'})}) | |
>>> dd['c']['key3'] = {'key4':'value4'} | |
>>> dd | |
defaultdict(<class '__main__.defaultdict'>, {'a': {'key1': 'value1'}, 'b': defaultdict(None, {'key2': 'value2'}), 'c': defaultdict(None, {'key3': {'key4': 'value4'}})}) | |
>>> dd2 = dd.copy() | |
>>> dd2 | |
defaultdict(<class '__main__.defaultdict'>, {'a': {'key1': 'value1'}, 'b': defaultdict(None, {'key2': 'value2'}), 'c': defaultdict(None, {'key3': {'key4': 'value4'}})}) | |
""" | |
def __init__(self, default_factory=None): | |
self.default_factory = default_factory | |
dict.__init__(self) | |
def __repr__(self): | |
# print("debug" + str(self.default_factory)) | |
typestr = str(self.default_factory).split("'") | |
if len(typestr) == 1: | |
typestr = typestr[0] | |
else: | |
typestr = str(self.default_factory) | |
return 'defaultdict(%s, %s)' % (typestr, | |
dict.__repr__(self)) | |
def __missing__(self, key): | |
if self.default_factory: | |
self[key] = self.default_factory() | |
return self[key] | |
else: | |
raise KeyError(key) | |
def __getitem__(self, key): | |
try: | |
return dict.__getitem__(self, key) | |
except KeyError: | |
return self.__missing__(key) | |
def copy(self): | |
return self.__copy__() | |
def __copy__(self): | |
new = type(self)(self.default_factory) | |
for key, value in self.items(): | |
new[key] = value | |
return new | |
if __name__ == "__main__": | |
import doctest | |
doctest.testmod() | |
# dd = defaultdict(list) | |
# dd[1].append(2) | |
# print(dd) | |
# dd = defaultdict(defaultdict) | |
# dd['a']={'key1':'value1'} | |
# print(dd) | |
# dd['b'].update({'key2':'value2'}) | |
# print(dd) | |
# dd['c']['key3'] = {'key4':'value4'} | |
# print(dd) | |
# dd2 = dd.copy() | |
# print(dd2) | |
# from collections import defaultdict as defaultdict2 | |
# cdd = defaultdict(list) | |
# cdd[1].append(2) | |
# print(cdd) | |
# cdd = defaultdict2(defaultdict2) | |
# cdd['a']={'key1':'value1'} | |
# print(cdd) | |
# cdd['b'].update({'key2':'value2'}) | |
# print(cdd) | |
# cdd['c']['key3'] = {'key4':'value4'} | |
# print(cdd) | |
# cdd2 = cdd.copy() | |
# print(cdd2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment