-
-
Notifications
You must be signed in to change notification settings - Fork 220
Expand file tree
/
Copy pathcollections.py
More file actions
28 lines (22 loc) · 958 Bytes
/
collections.py
File metadata and controls
28 lines (22 loc) · 958 Bytes
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
"""Python's collections module -- for Transcrypt."""
class defaultdict(dict):
"""Dictionary that takes a factory parameter and always returns a value."""
def __init__(self, default_factory=None, *args, **kwargs): # noqa
if not callable(default_factory) and default_factory is not None:
raise TypeError("first argument must be callable or None")
super().__init__(*args, **kwargs)
self.default_factory = default_factory
def __repr__(self):
return "defaultdict({}, {})".format(
self.default_factory, super().__repr__(self)
)
def __missing__(self, key: str):
if self.default_factory is None:
raise KeyError(key)
self[key] = self.default_factory()
return super().__getitem__(key)
def __getitem__(self, key: str):
try:
return super().__getitem__(key)
except KeyError:
return self.__missing__(key)