8000 Put an updated UserDict class in the collections module and · python/cpython@48b8b66 · GitHub
[go: up one dir, main page]

Skip to content

Commit 48b8b66

Browse files
committed
Put an updated UserDict class in the collections module and
register it as a compliant Mutable Mapping. Todo: Convert the UserDict dependent tests to the new API and then remove the old UserDict module. Move the UserDict docs to collections.rst.
1 parent 7ac6095 commit 48b8b66

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

Lib/collections.py

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__all__ = ['deque', 'defaultdict', 'namedtuple']
1+
__all__ = ['deque', 'defaultdict', 'namedtuple', 'UserDict']
22
# For bootstrapping reasons, the collection ABCs are defined in _abcoll.py.
33
# They should however be considered an integral part of collections.py.
44
from _abcoll import *
@@ -10,6 +10,10 @@
1010
from keyword import iskeyword as _iskeyword
1111
import sys as _sys
1212

13+
################################################################################
14+
### namedtuple
15+
################################################################################
16+
1317
def namedtuple(typename, field_names, verbose=False):
1418
"""Returns a new subclass of tuple with named fields.
1519
@@ -106,7 +110,60 @@ def _replace(self, **kwds):
106110

107111

108112

109-
113+
################################################################################
114+
### UserDict
115+
################################################################################
116+
117+
class UserDict(MutableMapping):
118+
119+
# Start by filling-out the abstract methods
120+
def __init__(self, dict=None, **kwargs):
121+
self.data = {}
122+
if dict is not None:
123+
self.update(dict)
124+
if len(kwargs):
125+
self.update(kwargs)
126+
def __len__(self): return len(self.data)
127+
def __getitem__(self, key):
128+
if key in self.data:
129+
return self.data[key]
130+
if hasattr(self.__class__, "__missing__"):
131+
return self.__class__.__missing__(self, key)
132+
raise KeyError(key)
133+
def __setitem__(self, key, item): self.data[key] = item
134+
def __delitem__(self, key): del self.data[key]
135+
def __iter__(self):
136+
return iter(self.data)
137+
138+
139+
# Now, add the methods in dicts but not in MutableMapping
140+
def __repr__(self): return repr(self.data)
141+
def copy(self):
142+
if self.__class__ is UserDict:
143+
return 10000 UserDict(self.data.copy())
144+
import copy
145+
data = self.data
146+
try:
147+
self.data = {}
148+
c = copy.copy(self)
149+
finally:
150+
self.data = data
151+
c.update(self)
152+
return c
153+
@classmethod
154+
def fromkeys(cls, iterable, value=None):
155+
d = cls()
156+
for key in iterable:
157+
d[key] = value
158+
return d
159+
160+
MutableMapping.register(UserDict)
161+
162+
163+
164+
################################################################################
165+
### Simple tests
166+
################################################################################
110167

111168
if __name__ == '__main__':
112169
# verify that instances can be pickled

Misc/NEWS

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ Extension Modules
7070
Library
7171
-------
7272

73+
- Weakref dictionaries now inherit from MutableMapping.
74+
XXX their API still needs to be modernized (i.e. eliminate the iter methods).
75+
76+
- Created new UserDict class in collections module. This one inherits from and
77+
complies with the MutableMapping ABC.
78+
XXX still need to covert old UserDict based tests and eliminate the old UserDict module.
79+
7380
- Removed UserDict.DictMixin. Replaced all its uses with collections.MutableMapping.
7481

7582
- Issue #1703: getpass() should flush after writing prompt.

0 commit comments

Comments
 (0)
0