summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2008-02-05 01:53:00 (GMT)
committerRaymond Hettinger <python@rcn.com>2008-02-05 01:53:00 (GMT)
commit48b8b665d7a1adab46b70fc536569c16fc8092d3 (patch)
treebbb73562721f6aafcf714eaa51dbe81b9ad92ab7
parent7ac609559da71b247bf6c2d37e8af9b2eadea2a7 (diff)
downloadcpython-48b8b665d7a1adab46b70fc536569c16fc8092d3.zip
cpython-48b8b665d7a1adab46b70fc536569c16fc8092d3.tar.gz
cpython-48b8b665d7a1adab46b70fc536569c16fc8092d3.tar.bz2
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.
-rw-r--r--Lib/collections.py61
-rw-r--r--Misc/NEWS7
2 files changed, 66 insertions, 2 deletions
diff --git a/Lib/collections.py b/Lib/collections.py
index 78f92ce..b883832 100644
--- a/Lib/collections.py
+++ b/Lib/collections.py
@@ -1,4 +1,4 @@
-__all__ = ['deque', 'defaultdict', 'namedtuple']
+__all__ = ['deque', 'defaultdict', 'namedtuple', 'UserDict']
# For bootstrapping reasons, the collection ABCs are defined in _abcoll.py.
# They should however be considered an integral part of collections.py.
from _abcoll import *
@@ -10,6 +10,10 @@ from operator import itemgetter as _itemgetter
from keyword import iskeyword as _iskeyword
import sys as _sys
+################################################################################
+### namedtuple
+################################################################################
+
def namedtuple(typename, field_names, verbose=False):
"""Returns a new subclass of tuple with named fields.
@@ -106,7 +110,60 @@ def namedtuple(typename, field_names, verbose=False):
-
+################################################################################
+### UserDict
+################################################################################
+
+class UserDict(MutableMapping):
+
+ # Start by filling-out the abstract methods
+ def __init__(self, dict=None, **kwargs):
+ self.data = {}
+ if dict is not None:
+ self.update(dict)
+ if len(kwargs):
+ self.update(kwargs)
+ def __len__(self): return len(self.data)
+ def __getitem__(self, key):
+ if key in self.data:
+ return self.data[key]
+ if hasattr(self.__class__, "__missing__"):
+ return self.__class__.__missing__(self, key)
+ raise KeyError(key)
+ def __setitem__(self, key, item): self.data[key] = item
+ def __delitem__(self, key): del self.data[key]
+ def __iter__(self):
+ return iter(self.data)
+
+
+ # Now, add the methods in dicts but not in MutableMapping
+ def __repr__(self): return repr(self.data)
+ def copy(self):
+ if self.__class__ is UserDict:
+ return UserDict(self.data.copy())
+ import copy
+ data = self.data
+ try:
+ self.data = {}
+ c = copy.copy(self)
+ finally:
+ self.data = data
+ c.update(self)
+ return c
+ @classmethod
+ def fromkeys(cls, iterable, value=None):
+ d = cls()
+ for key in iterable:
+ d[key] = value
+ return d
+
+MutableMapping.register(UserDict)
+
+
+
+################################################################################
+### Simple tests
+################################################################################
if __name__ == '__main__':
# verify that instances can be pickled
diff --git a/Misc/NEWS b/Misc/NEWS
index 921b2a9..5e90be4 100644
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -70,6 +70,13 @@ Extension Modules
Library
-------
+- Weakref dictionaries now inherit from MutableMapping.
+ XXX their API still needs to be modernized (i.e. eliminate the iter methods).
+
+- Created new UserDict class in collections module. This one inherits from and
+ complies with the MutableMapping ABC.
+ XXX still need to covert old UserDict based tests and eliminate the old UserDict module.
+
- Removed UserDict.DictMixin. Replaced all its uses with collections.MutableMapping.
- Issue #1703: getpass() should flush after writing prompt.