[Zope3-checkins] SVN: Zope3/trunk/src/zope/bforest/bforest. Make doctest have more standard indentation.

Gary Poster gary at zope.com
Tue Feb 8 21:31:17 EST 2005


Log message for revision 29089:
  Make doctest have more standard indentation.
  
  Make comparisons more forgiving.  Add tests.
  
  

Changed:
  U   Zope3/trunk/src/zope/bforest/bforest.py
  U   Zope3/trunk/src/zope/bforest/bforest.txt

-=-
Modified: Zope3/trunk/src/zope/bforest/bforest.py
===================================================================
--- Zope3/trunk/src/zope/bforest/bforest.py	2005-02-09 01:00:34 UTC (rev 29088)
+++ Zope3/trunk/src/zope/bforest/bforest.py	2005-02-09 02:31:17 UTC (rev 29089)
@@ -123,13 +123,49 @@
         else:
             return True
     
-    def __cmp__(self, d):
-        # this is probably not the most efficient approach, but I'm not sure
-        # what is, and this is certainly among the simplest.  Don't do 
-        # comparisons with large bforests unless you are willing to pay the
-        # price.  Improvements welcome.
-        return cmp(dict(self), dict(d))
+    def __eq__(self, other):
+        if not isinstance(other, dict):
+            if (isinstance(other, AbstractBForest) and 
+                self._treemodule is not other._treemodule):
+                return False
+            try:
+                other = dict(other)
+            except TypeError:
+                return False
+        return dict(self)==other # :-/
     
+    def __gt__(self, other):
+        if not isinstance(other, dict):
+            try:
+                other = dict(other)
+            except TypeError:
+                return id(self) > id(other)
+        return dict(self) > other
+
+    def __lt__(self, other):
+        if not isinstance(other, dict):
+            try:
+                other = dict(other)
+            except TypeError:
+                return id(self) < id(other)
+        return dict(self) < other
+        
+    def __ge__(self, other):
+        if not isinstance(other, dict):
+            try:
+                other = dict(other)
+            except TypeError:
+                return id(self) >= id(other)
+        return dict(self) >= other
+        
+    def __le__(self, other):
+        if not isinstance(other, dict):
+            try:
+                other = dict(other)
+            except TypeError:
+                return id(self) <= id(other)
+        return dict(self) <= other
+
     def __len__(self):
         return len(self.tree())
     

Modified: Zope3/trunk/src/zope/bforest/bforest.txt
===================================================================
--- Zope3/trunk/src/zope/bforest/bforest.txt	2005-02-09 01:00:34 UTC (rev 29088)
+++ Zope3/trunk/src/zope/bforest/bforest.txt	2005-02-09 02:31:17 UTC (rev 29089)
@@ -15,187 +15,241 @@
 First let's instantiate a bforest and look at an empty example.  By default,
 a new bforest creates two composite btree buckets.
 
->>> d = BForest()
->>> list(d.keys())
-[]
->>> list(d.values())
-[]
->>> len(d.buckets)
-2
->>> dummy_key = KeyGenerator()
->>> d.get(dummy_key)
->>> d.get(dummy_key, 42)
-42
+    >>> d = BForest()
+    >>> list(d.keys())
+    []
+    >>> list(d.values())
+    []
+    >>> len(d.buckets)
+    2
+    >>> dummy_key = KeyGenerator()
+    >>> d.get(dummy_key)
+    >>> d.get(dummy_key, 42)
+    42
 
 Now we'll populate it.  We'll first create a dictionary we'll use to compare.
 
->>> original = {}
->>> for i in range(10):
-...     original[KeyGenerator()] = ValueGenerator()
-... 
->>> d.update(original)
->>> d == original
-True
->>> d_keys = list(d.keys())
->>> d_keys.sort()
->>> o_keys = original.keys()
->>> o_keys.sort()
->>> d_keys == o_keys
-True
->>> d_values = list(d.values())
->>> d_values.sort()
->>> o_values = original.values()
->>> o_values.sort()
->>> o_values == d_values
-True
->>> d_items = list(d.items())
->>> d_items.sort()
->>> o_items = original.items()
->>> o_items.sort()
->>> o_items == d_items
-True
->>> key, value = d.popitem()
->>> value == original.pop(key)
-True
->>> key, value = original.popitem()
->>> value == d.pop(key)
-True
->>> len(d) == len(original)
-True
+    >>> original = {}
+    >>> for i in range(10):
+    ...     original[KeyGenerator()] = ValueGenerator()
+    ... 
+    >>> d.update(original)
+    >>> d == original
+    True
+    >>> d_keys = list(d.keys())
+    >>> d_keys.sort()
+    >>> o_keys = original.keys()
+    >>> o_keys.sort()
+    >>> d_keys == o_keys
+    True
+    >>> d_values = list(d.values())
+    >>> d_values.sort()
+    >>> o_values = original.values()
+    >>> o_values.sort()
+    >>> o_values == d_values
+    True
+    >>> d_items = list(d.items())
+    >>> d_items.sort()
+    >>> o_items = original.items()
+    >>> o_items.sort()
+    >>> o_items == d_items
+    True
+    >>> key, value = d.popitem()
+    >>> value == original.pop(key)
+    True
+    >>> key, value = original.popitem()
+    >>> value == d.pop(key)
+    True
+    >>> len(d) == len(original)
+    True
 
 Now let's rotate the buckets.
 
->>> d.rotateBucket()
+    >>> d.rotateBucket()
 
 ...and we'll do the exact same test as above, first.
 
->>> d == original
-True
->>> d_keys = list(d.keys())
->>> d_keys.sort()
->>> o_keys = original.keys()
->>> o_keys.sort()
->>> d_keys == o_keys
-True
->>> d_values = list(d.values())
->>> d_values.sort()
->>> o_values = original.values()
->>> o_values.sort()
->>> o_values == d_values
-True
->>> d_items = list(d.items())
->>> d_items.sort()
->>> o_items = original.items()
->>> o_items.sort()
->>> o_items == d_items
-True
->>> key, value = d.popitem()
->>> value == original.pop(key)
-True
->>> key, value = original.popitem()
->>> value == d.pop(key)
-True
->>> len(d) == len(original)
-True
+    >>> d == original
+    True
+    >>> d_keys = list(d.keys())
+    >>> d_keys.sort()
+    >>> o_keys = original.keys()
+    >>> o_keys.sort()
+    >>> d_keys == o_keys
+    True
+    >>> d_values = list(d.values())
+    >>> d_values.sort()
+    >>> o_values = original.values()
+    >>> o_values.sort()
+    >>> o_values == d_values
+    True
+    >>> d_items = list(d.items())
+    >>> d_items.sort()
+    >>> o_items = original.items()
+    >>> o_items.sort()
+    >>> o_items == d_items
+    True
+    >>> key, value = d.popitem()
+    >>> value == original.pop(key)
+    True
+    >>> key, value = original.popitem()
+    >>> value == d.pop(key)
+    True
+    >>> len(d) == len(original)
+    True
 
 Now we'll make a new dictionary to represent changes made after the bucket
 rotation.
 
->>> second = {}
->>> for i in range(10):
-...     key = KeyGenerator()
-...     value = ValueGenerator()
-...     second[key] = value
-...     d[key] = value
-... 
->>> original.update(second)
+    >>> second = {}
+    >>> for i in range(10):
+    ...     key = KeyGenerator()
+    ...     value = ValueGenerator()
+    ...     second[key] = value
+    ...     d[key] = value
+    ... 
+    >>> original.update(second)
 
 ...and we'll do almost the exact same test as above, first.
 
->>> d == original
-True
->>> d_keys = list(d.keys())
->>> d_keys.sort()
->>> o_keys = original.keys()
->>> o_keys.sort()
->>> d_keys == o_keys
-True
->>> d_values = list(d.values())
->>> d_values.sort()
->>> o_values = original.values()
->>> o_values.sort()
->>> o_values == d_values
-True
->>> d_items = list(d.items())
->>> d_items.sort()
->>> o_items = original.items()
->>> o_items.sort()
->>> o_items == d_items
-True
->>> key, value = d.popitem()
->>> ignore = second.pop(key, None) # keep second up-to-date
->>> value == original.pop(key)
-True
->>> key, value = original.popitem()
->>> ignore = second.pop(key, None) # keep second up-to-date
->>> value == d.pop(key)
-True
->>> len(d) == len(original)
-True
+    >>> d == original
+    True
+    >>> d_keys = list(d.keys())
+    >>> d_keys.sort()
+    >>> o_keys = original.keys()
+    >>> o_keys.sort()
+    >>> d_keys == o_keys
+    True
+    >>> d_values = list(d.values())
+    >>> d_values.sort()
+    >>> o_values = original.values()
+    >>> o_values.sort()
+    >>> o_values == d_values
+    True
+    >>> d_items = list(d.items())
+    >>> d_items.sort()
+    >>> o_items = original.items()
+    >>> o_items.sort()
+    >>> o_items == d_items
+    True
+    >>> key, value = d.popitem()
+    >>> ignore = second.pop(key, None) # keep second up-to-date
+    >>> value == original.pop(key)
+    True
+    >>> key, value = original.popitem()
+    >>> ignore = second.pop(key, None) # keep second up-to-date
+    >>> value == d.pop(key)
+    True
+    >>> len(d) == len(original)
+    True
 
 Now if we rotate the buckets, the first set of items will be gone, but the 
 second will remain.
 
->>> d.rotateBucket()
->>> d == original
-False
->>> d == second
-True
+    >>> d.rotateBucket()
+    >>> d == original
+    False
+    >>> d == second
+    True
 
 Let's set a value, check the copy behavior,  and then rotate it one more time.
 
->>> third = {KeyGenerator(): ValueGenerator()}
->>> d.update(third)
->>> copy = d.copy()
->>> copy == d
-True
->>> copy != second # because second doesn't have the values of third
-True
->>> list(copy.buckets[0].items()) == list(d.buckets[0].items())
-True
->>> list(copy.buckets[1].items()) == list(d.buckets[1].items())
-True
->>> copy[KeyGenerator()] = ValueGenerator()
->>> copy == d
-False
->>> d.rotateBucket()
->>> d == third
-True
->>> d.clear()
->>> d == BForest() == {}
-True
+    >>> third = {KeyGenerator(): ValueGenerator()}
+    >>> d.update(third)
+    >>> copy = d.copy()
+    >>> copy == d
+    True
+    >>> copy != second # because second doesn't have the values of third
+    True
+    >>> list(copy.buckets[0].items()) == list(d.buckets[0].items())
+    True
+    >>> list(copy.buckets[1].items()) == list(d.buckets[1].items())
+    True
+    >>> copy[KeyGenerator()] = ValueGenerator()
+    >>> copy == d
+    False
+    >>> d.rotateBucket()
+    >>> d == third
+    True
+    >>> d.clear()
+    >>> d == BForest() == {}
+    True
+    
+    >>> d.update(second)
 
->>> d.update(second)
-
 We'll make a value in one bucket that we'll override in another.
 
->>> d[third.keys()[0]] = ValueGenerator()
->>> d.rotateBucket()
->>> d.update(third)
->>> second.update(third)
->>> d == second
-True
->>> second == d
-True
+    >>> d[third.keys()[0]] = ValueGenerator()
+    >>> d.rotateBucket()
+    >>> d.update(third)
+    >>> second.update(third)
+    >>> d == second
+    True
+    >>> second == d
+    True
 
 The tree method converts the bforest to a btree as efficiently as I know how
 for a common case of more items in buckets than buckets.
 
->>> tree = d.tree()
->>> d_items = list(d.items())
->>> d_items.sort()
->>> t_items = list(tree.items())
->>> t_items.sort()
->>> t_items == d_items
-True
+    >>> tree = d.tree()
+    >>> d_items = list(d.items())
+    >>> d_items.sort()
+    >>> t_items = list(tree.items())
+    >>> t_items.sort()
+    >>> t_items == d_items
+    True
 
+Finally, comparisons work similarly to dicts but in a simpleminded 
+way--improvements welcome!  We've already looked at a lot of examples above,
+but here are some additional cases
+
+    >>> d == None
+    False
+    >>> d != None
+    True
+    >>> None == d
+    False
+    >>> d != None
+    True
+    >>> d > None
+    True
+    >>> d < None
+    False
+    >>> d >= None
+    True
+    >>> d <= None
+    False
+    >>> d >= second
+    True
+    >>> d >= dict(second)
+    True
+    >>> d <= second
+    True
+    >>> d <= dict(second)
+    True
+    >>> d > second
+    False
+    >>> d > dict(second)
+    False
+    >>> d < second
+    False
+    >>> d > dict(second)
+    False
+    >>> second.popitem()[0] in d
+    True
+    >>> d > second
+    True
+    >>> d < second
+    False
+    >>> d >= second
+    True
+    >>> d <= second
+    False
+    >>> second < d
+    True
+    >>> second > d
+    False
+    >>> second <= d
+    True
+    >>> second >= d
+    False



More information about the Zope3-Checkins mailing list