[Zodb-checkins] SVN: ZODB/branches/3.4/ Gave PersistentMapping an __iter__ method.

Tim Peters tim.one at comcast.net
Wed Aug 24 15:37:17 EDT 2005


Log message for revision 38076:
  Gave PersistentMapping an __iter__ method.
  
  Also gave it some tests (it was woefully untested).
  

Changed:
  U   ZODB/branches/3.4/NEWS.txt
  U   ZODB/branches/3.4/src/ZODB/tests/testPersistentMapping.py
  U   ZODB/branches/3.4/src/persistent/mapping.py

-=-
Modified: ZODB/branches/3.4/NEWS.txt
===================================================================
--- ZODB/branches/3.4/NEWS.txt	2005-08-24 18:28:41 UTC (rev 38075)
+++ ZODB/branches/3.4/NEWS.txt	2005-08-24 19:37:17 UTC (rev 38076)
@@ -17,7 +17,18 @@
   happen, and stopped happening for subtransaction commits too.  Making a
   savepoint (or doing a subtransaction commit) does invoke cache gc now.
 
+PersistentMapping
+-----------------
 
+- (3.4.2a1) The ``PersistentMapping`` class has an ``__iter__()`` method
+  now, so that objects of this type work well with Python's iteration
+  protocol.  For example, if ``x`` is a ``PersistentMapping`` (or
+  Python dictionary, or BTree, or ``PersistentDict``, ...), then
+  ``for key in x:`` iterates over the keys of ``x``, ``list(x)`` creates
+  a list containing ``x``'s keys, ``iter(x)`` creates an iterator for
+  ``x``'s keys, and so on.
+
+
 What's new in ZODB3 3.4.1?
 ==========================
 Release date: 09-Aug-2005

Modified: ZODB/branches/3.4/src/ZODB/tests/testPersistentMapping.py
===================================================================
--- ZODB/branches/3.4/src/ZODB/tests/testPersistentMapping.py	2005-08-24 18:28:41 UTC (rev 38075)
+++ ZODB/branches/3.4/src/ZODB/tests/testPersistentMapping.py	2005-08-24 19:37:17 UTC (rev 38076)
@@ -95,6 +95,74 @@
 
         self.assert_(oldPath is newPath)
 
+    def checkBasicOps(self):
+        from persistent.mapping import PersistentMapping
+        m = PersistentMapping({'x': 1}, a=2, b=3)
+        m['name'] = 'bob'
+        self.assertEqual(m['name'], "bob")
+        self.assertEqual(m.get('name', 42), "bob")
+        self.assert_('name' in m)
+
+        try:
+            m['fred']
+        except KeyError:
+            pass
+        else:
+            self.fail("expected KeyError")
+        self.assert_('fred' not in m)
+        self.assertEqual(m.get('fred'), None)
+        self.assertEqual(m.get('fred', 42), 42)
+
+        keys = m.keys()
+        keys.sort()
+        self.assertEqual(keys, ['a', 'b', 'name', 'x'])
+
+        values = m.values()
+        values.sort()
+        self.assertEqual(values, [1, 2, 3, 'bob'])
+
+        items = m.items()
+        items.sort()
+        self.assertEqual(items,
+                         [('a', 2), ('b', 3), ('name', 'bob'), ('x', 1)])
+
+        keys = list(m.iterkeys())
+        keys.sort()
+        self.assertEqual(keys, ['a', 'b', 'name', 'x'])
+
+        values = list(m.itervalues())
+        values.sort()
+        self.assertEqual(values, [1, 2, 3, 'bob'])
+
+        items = list(m.iteritems())
+        items.sort()
+        self.assertEqual(items,
+                         [('a', 2), ('b', 3), ('name', 'bob'), ('x', 1)])
+
+    # PersistentMapping didn't have an __iter__ method before ZODB 3.4.2.
+    # Check that it plays well now with the Python iteration protocol.
+    def checkIteration(self):
+        from persistent.mapping import PersistentMapping
+        m = PersistentMapping({'x': 1}, a=2, b=3)
+        m['name'] = 'bob'
+
+        def check(keylist):
+            keylist.sort()
+            self.assertEqual(keylist, ['a', 'b', 'name', 'x'])
+
+        check(list(m))
+        check([key for key in m])
+
+        i = iter(m)
+        keylist = []
+        while 1:
+            try:
+                key = i.next()
+            except StopIteration:
+                break
+            keylist.append(key)
+        check(keylist)
+
 def find_global(modulename, classname):
     """Helper for this test suite to get special PersistentMapping"""
 

Modified: ZODB/branches/3.4/src/persistent/mapping.py
===================================================================
--- ZODB/branches/3.4/src/persistent/mapping.py	2005-08-24 18:28:41 UTC (rev 38075)
+++ ZODB/branches/3.4/src/persistent/mapping.py	2005-08-24 19:37:17 UTC (rev 38076)
@@ -75,6 +75,14 @@
             self._p_changed = 1
             return self.__super_popitem()
 
+    # __iter__ was added in ZODB 3.4.2, but should have been added long
+    # before.  We could inherit from Python's IterableUserDict instead
+    # (which just adds __iter__ to Python's UserDict), but that class isn't
+    # documented, and it would add another level of lookup for all the
+    # other methods.
+    def __iter__(self):
+        return iter(self.data)
+
     # If the internal representation of PersistentMapping changes,
     # it causes compatibility problems for pickles generated by
     # different versions of the code.  Compatibility works in both



More information about the Zodb-checkins mailing list