-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathddict.py
36 lines (30 loc) · 1.02 KB
/
ddict.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# https://github.com/micropython/micropython-lib/
class ddict:
#@staticmethod
#def __new__(cls, default_factory=None, **kwargs):
# # Some code (e.g. urllib.urlparse) expects that basic defaultdict
# # functionality will be available to subclasses without them
# # calling __init__().
# self = super(defaultdict, cls).__new__(cls)
# self.d = {}
# return self
def __init__(self, default_factory=None, d = {}):
self.d = d
self.default_factory = default_factory
def __getitem__(self, key):
try:
return self.d[key]
except KeyError:
v = self.__missing__(key)
self.d[key] = v
return v
def __setitem__(self, key, v):
self.d[key] = v
def __delitem__(self, key):
del self.d[key]
def __contains__(self, key):
return key in self.d
def __missing__(self, key):
if self.default_factory is None:
raise KeyError(key)
return self.default_factory()