[Zope-Checkins] CVS: Zope3/lib/python/Persistence/BTrees/tests - testBTrees.py:1.16

Tim Peters tim.one@comcast.net
Sat, 22 Jun 2002 03:18:12 -0400


Update of /cvs-repository/Zope3/lib/python/Persistence/BTrees/tests
In directory cvs.zope.org:/tmp/cvs-serv11837/tests

Modified Files:
	testBTrees.py 
Log Message:
Sets, TreeSets, Buckets, and BTrees in Zope3 now participate in the
new-in-Python-2.2 iteration protocol.  The short course is that
anywhere an "iterable object" can be used, you can now use an object
of one of the Bucket/BTree-based data types.

For example, where x is a Set, TreeSet, Bucket, or BTree:

    for key in x:

is the same as

    for key in x.keys():

However, the former is more efficient for TreeSets and BTrees, while for
Sets and Buckets it doesn't need to materialize the whole set of keys in
one gulp.  Note that results are undefined if you mutate x *during* the
iteration.  If the iterator implementation detects (not likely), that
you've done so, it will raise a RuntimeError saying so.

Lots of other contexts in 2.2 accept iterable objects too, including
contexts that in pre-2.2 accepted only sequences.  For example,

    list(x)

is the same as list(x.keys()).  Perhaps subtler (note that this isn't
a special case -- it just falls out of playing along with the iteration
protocol):

    IIBucket(some_IIBTree)

sucks the keys out of some_IIBTree and puts them in a bucket.

On the ridiculous end of the scale, unpacking notation also accepts
iterable objects, so, for example,

>>> s = IISet([7, 4, 2])
>>> x, y, z = s
>>> print x
2
>>> print y
4
>>> print z
7
>>>

Even more ridiculous, min(x) is now a very expensive way to spell
x.minKey().  Stick to things that really need iteration <wink>.


=== Zope3/lib/python/Persistence/BTrees/tests/testBTrees.py 1.15 => 1.16 ===
         v = self.t.values()
         for i in range(100):
-            self.assertEqual(v[i],i*i , (i*i,i))
+            self.assertEqual(v[i], i*i)
 
     def testValuesWorks1(self):
         for x in range(100):
@@ -187,11 +187,9 @@
 
         for x in range(40):
             lst = self.t.keys(0+x,99-x)
-            self.assertEqual(list(lst),range(0+x,99-x+1))
+            self.assertEqual(list(lst), range(0+x, 99-x+1))
 
-        # BTree items must lie about their lengths, so we convert to list
-        self.assertEqual(len(v) , 100, len(v))
-        #self.assertEqual(len(v) , 100, len(v))
+        self.assertEqual(len(v), 100)
 
     def testItemsWorks(self):
         for x in range(100):
@@ -220,12 +218,12 @@
         self.t[4] = 150
         del self.t[7]
         t = self.t
-        self.assertEqual(t.maxKey() , 10)
-        self.assertEqual(t.maxKey(6) , 6)
-        self.assertEqual(t.maxKey(9) , 8)
-        self.assertEqual(t.minKey() , 1)
-        self.assertEqual(t.minKey(3) , 3)
-        self.assertEqual(t.minKey(9) , 10)
+        self.assertEqual(t.maxKey(), 10)
+        self.assertEqual(t.maxKey(6), 6)
+        self.assertEqual(t.maxKey(9), 8)
+        self.assertEqual(t.minKey(), 1)
+        self.assertEqual(t.minKey(3), 3)
+        self.assertEqual(t.minKey(9), 10)
 
     def testClear(self):
         r = range(100)
@@ -234,7 +232,7 @@
             self.t[rnd] = 0
         self.t.clear()
         diff = lsubtract(list(self.t.keys()), [])
-        self.assertEqual(diff , [], diff)
+        self.assertEqual(diff, [])
 
     def testUpdate(self):
         d={}
@@ -248,13 +246,13 @@
         items.sort()
 
         self.t.update(d)
-        self.assertEqual(list(self.t.items()) , items)
+        self.assertEqual(list(self.t.items()), items)
 
         self.t.clear()
-        self.assertEqual(list(self.t.items()) , [])
+        self.assertEqual(list(self.t.items()), [])
 
         self.t.update(l)
-        self.assertEqual(list(self.t.items()) , items)
+        self.assertEqual(list(self.t.items()), items)
 
     def testEmptyRangeSearches(self):
         t = self.t
@@ -349,6 +347,30 @@
         self.assertEqual(len(tslice), 60)
         self.assertEqual(list(tslice), zip(range(20, 80), [1]*60))
 
+    def testIterator(self):
+        t = self.t
+
+        for keys in [], [-2], [1, 4], range(-170, 2000, 6):
+            t.clear()
+            for k in keys:
+                t[k] = -3 * k
+
+            self.assertEqual(list(t), keys)
+
+            x = []
+            for k in t:
+                x.append(k)
+            self.assertEqual(x, keys)
+
+            it = iter(t)
+            self.assert_(it is iter(it))
+            x = []
+            try:
+                while 1:
+                    x.append(it.next())
+            except StopIteration:
+                pass
+            self.assertEqual(x, keys)
 
 class NormalSetTests(Base):
     """ Test common to all set types """
@@ -498,6 +520,29 @@
                     x = kslice[lo:hi]
                     self.assertEqual(list(x), keys[lo:hi])
 
+    def testIterator(self):
+        t = self.t
+
+        for keys in [], [-2], [1, 4], range(-170, 2000, 6):
+            t.clear()
+            t.update(keys)
+
+            self.assertEqual(list(t), keys)
+
+            x = []
+            for k in t:
+                x.append(k)
+            self.assertEqual(x, keys)
+
+            it = iter(t)
+            self.assert_(it is iter(it))
+            x = []
+            try:
+                while 1:
+                    x.append(it.next())
+            except StopIteration:
+                pass
+            self.assertEqual(x, keys)
 
 class ExtendedSetTests(NormalSetTests):
     def testLen(self):