Add defaultdict to collections

This commit is contained in:
coolreader18 committed 2019-07-10 22:16:37 -05:00
1 parent 8869130509
commit 5e5d726a5d
2 files changed
+26

No files matched your search

+6
View File
@@ -1277,3 +1277,9 @@ class UserString(_collections_abc.Sequence):
return self.__class__(self.data.translate(*args))
def upper(self): return self.__class__(self.data.upper())
def zfill(self, width): return self.__class__(self.data.zfill(width))
# FIXME: try to implement defaultdict in collections.rs rather than in Python
# I (coolreader18) couldn't figure out some class stuff with __new__ and
# __init__ and __missing__ and subclassing built-in types from Rust, so I went
# with this instead.
from ._defaultdict import *
+20
View File
@@ -0,0 +1,20 @@
class defaultdict(dict):
def __new__(cls, *args, **kwargs):
if len(args) >= 1:
default_factory = args[0]
args = args[1:]
else:
default_factory = None
self = dict.__new__(cls, *args, **kwargs)
self.default_factory = default_factory
return self
def __missing__(self, key):
if self.default_factory:
return self.default_factory()
else:
raise KeyError(key)
def __repr__(self):
return f"defaultdict({self.default_factory}, {dict.__repr__(self)})"