[Python-checkins] python/dist/src/Lib UserDict.py,1.20,1.21

rhettinger@users.sourceforge.net rhettinger@users.sourceforge.net
Wed, 27 Nov 2002 00:29:13 -0800


Update of /cvsroot/python/python/dist/src/Lib
In directory sc8-pr-cvs1:/tmp/cvs-serv10075

Modified Files:
	UserDict.py 
Log Message:
Bring UserDict in-sync with changes to dict.
  
Constructor accepts optional keyword arguments after a optional items list.
Add fromkeys() as an alternate constructor from an iterable over keys.
Expand related unittests.


Index: UserDict.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/UserDict.py,v
retrieving revision 1.20
retrieving revision 1.21
diff -C2 -d -r1.20 -r1.21
*** UserDict.py	22 Nov 2002 00:07:39 -0000	1.20
--- UserDict.py	27 Nov 2002 08:29:10 -0000	1.21
***************
*** 3,13 ****
  class UserDict:
      def __init__(self, dict=None, **kwargs):
!         self.data = kwargs              # defaults to {}
          if dict is not None:
!             if hasattr(dict,'keys'):    # handle mapping (possibly a UserDict)
!                 self.update(dict)
!             else:                       # handle sequence
!                 DICT = type({})  # because builtin dict is locally overridden
!                 self.data.update(DICT(dict))
      def __repr__(self): return repr(self.data)
      def __cmp__(self, dict):
--- 3,13 ----
  class UserDict:
      def __init__(self, dict=None, **kwargs):
!         self.data = {}
          if dict is not None:
!             if not hasattr(dict,'keys'):
!                 dict = type({})(dict)   # make mapping from a sequence
!             self.update(dict)
!         if len(kwargs):
!             self.update(kwargs)
      def __repr__(self): return repr(self.data)
      def __cmp__(self, dict):
***************
*** 62,65 ****
--- 62,71 ----
      def __contains__(self, key):
          return key in self.data
+     def fromkeys(cls, iterable, value=None):
+         d = cls()
+         for key in iterable:
+             d[key] = value
+         return d
+     fromkeys = classmethod(fromkeys)
  
  class IterableUserDict(UserDict):