[Checkins] SVN: zc.dict/trunk/ docs, fixes and tests for intended subclassing story

Gary Poster gary at zope.com
Wed Mar 5 21:33:30 EST 2008


Log message for revision 84493:
  docs, fixes and tests for intended subclassing story

Changed:
  U   zc.dict/trunk/CHANGES.txt
  U   zc.dict/trunk/buildout.cfg
  U   zc.dict/trunk/setup.py
  U   zc.dict/trunk/src/zc/dict/dict.py
  U   zc.dict/trunk/src/zc/dict/dict.txt
  U   zc.dict/trunk/src/zc/dict/ordered.txt

-=-
Modified: zc.dict/trunk/CHANGES.txt
===================================================================
--- zc.dict/trunk/CHANGES.txt	2008-03-06 00:12:00 UTC (rev 84492)
+++ zc.dict/trunk/CHANGES.txt	2008-03-06 02:33:29 UTC (rev 84493)
@@ -1,5 +1,12 @@
+1.2.1 (2007-3-5)
+----------------
+
+* documented, fixed, and tested intended subclassing story
+
+* documented and tested support for old DBs with instances of OrderedDict
+
 1.2 (2007-3-5)
------------------
+--------------
 
 * bug fix: ``pop`` and ``setdefault`` did not correctly adjust _len
 
@@ -23,7 +30,3 @@
 ----------------
 
 * Added OrderedDict class that maintains the order in which items are added.
-
-
-1.1b
-----

Modified: zc.dict/trunk/buildout.cfg
===================================================================
--- zc.dict/trunk/buildout.cfg	2008-03-06 00:12:00 UTC (rev 84492)
+++ zc.dict/trunk/buildout.cfg	2008-03-06 02:33:29 UTC (rev 84493)
@@ -10,6 +10,7 @@
 
 [py]
 recipe = zc.recipe.egg
-eggs = setuptools
+eggs = zc.dict
+       setuptools
 interpreter = py
 scripts = py

Modified: zc.dict/trunk/setup.py
===================================================================
--- zc.dict/trunk/setup.py	2008-03-06 00:12:00 UTC (rev 84492)
+++ zc.dict/trunk/setup.py	2008-03-06 02:33:29 UTC (rev 84493)
@@ -5,7 +5,7 @@
                     open("src/zc/dict/ordered.txt").read())
 setup(
     name="zc.dict",
-    version="1.2",
+    version="1.2.1",
     license="ZPL 2.1",
     author="Zope Corporation",
     author_email="zope3-dev at zope.org",

Modified: zc.dict/trunk/src/zc/dict/dict.py
===================================================================
--- zc.dict/trunk/src/zc/dict/dict.py	2008-03-06 00:12:00 UTC (rev 84492)
+++ zc.dict/trunk/src/zc/dict/dict.py	2008-03-06 02:33:29 UTC (rev 84493)
@@ -31,7 +31,8 @@
     def __init__(self, *args, **kwargs):
         self._data = BTrees.OOBTree.OOBTree()
         self._len = BTrees.Length.Length()
-        self.update(*args, **kwargs)
+        if args or kwargs:
+            self.update(*args, **kwargs)
 
     def __setitem__(self, key, value):
         delta = 1
@@ -120,11 +121,10 @@
 
     def popitem(self):
         try:
-            k, v = self.iteritems().next()
-        except StopIteration:
+            key = self._data.minKey()
+        except ValueError:
             raise KeyError, 'container is empty'
-        del self[k]
-        return (k, v)
+        return (key, self.pop(key))
 
 class OrderedDict(Dict):
     """A Ordered BTree-based dict-like persistent object that can be safely

Modified: zc.dict/trunk/src/zc/dict/dict.txt
===================================================================
--- zc.dict/trunk/src/zc/dict/dict.txt	2008-03-06 00:12:00 UTC (rev 84492)
+++ zc.dict/trunk/src/zc/dict/dict.txt	2008-03-06 02:33:29 UTC (rev 84493)
@@ -167,7 +167,54 @@
     >>> c.get('nonexistent', 'default')
     'default'
 
+Subclassing
+-----------
 
+For easy subclassing, the dict is intended to have three important
+characteristics:
+
+- All addition is done with __setitem__ so overriding it will control
+  addition.
+
+- All removal is done with either ``pop`` or ``clear`` so overriding these
+  methods will control removal.
+
+- Calling __init__ without passing an argument will not try to access the
+  ``update`` method.
+
+Let's demonstrate these with a quick subclass.
+
+    >>> class Demo(Dict):
+    ...     def __setitem__(self, key, value):
+    ...         print '__setitem__', key, value
+    ...         super(Demo, self).__setitem__(key, value)
+    ...     def pop(self, key, *args):
+    ...         print 'pop', key, args and arg[0] or '---'
+    ...         return super(Demo, self).pop(key, *args)
+    ...     def update(self, *args, **kwargs):
+    ...         print 'update'
+    ...         super(Demo, self).update(*args, **kwargs)
+    ...     def clear(self):
+    ...         print 'clear'
+    ...         super(Demo, self).clear()
+    ...
+    
+    >>> demo1 = Demo()
+    >>> demo2 = Demo([['foo', 'bar'], ['bing', 'baz']], sha='zam')
+    update
+    __setitem__ foo bar
+    __setitem__ bing baz
+    __setitem__ sha zam
+    >>> demo2.setdefault('babble')
+    __setitem__ babble None
+    >>> del demo2['bing']
+    pop bing ---
+    >>> demo2.popitem()
+    pop babble ---
+    ('babble', None)
+    >>> demo2.clear()
+    clear
+
 Regression tests
 ----------------
 

Modified: zc.dict/trunk/src/zc/dict/ordered.txt
===================================================================
--- zc.dict/trunk/src/zc/dict/ordered.txt	2008-03-06 00:12:00 UTC (rev 84492)
+++ zc.dict/trunk/src/zc/dict/ordered.txt	2008-03-06 02:33:29 UTC (rev 84493)
@@ -226,7 +226,55 @@
     >>> c.get('nonexistent', 'default')
     'default'
 
+Subclassing
+-----------
 
+For easy subclassing, the ordered dict is intended to have three important
+characteristics:
+
+- All addition is done with __setitem__ so overriding it will control
+  addition.
+
+- All removal is done with either ``pop`` or ``clear`` so overriding these
+  methods will control removal.
+
+- Calling __init__ without passing an argument will not try to access the
+  ``update`` method.
+
+Let's demonstrate these with a quick subclass.
+
+    >>> class Demo(OrderedDict):
+    ...     def __setitem__(self, key, value):
+    ...         print '__setitem__', key, value
+    ...         super(Demo, self).__setitem__(key, value)
+    ...     def pop(self, key, *args):
+    ...         print 'pop', key, args and arg[0] or '---'
+    ...         return super(Demo, self).pop(key, *args)
+    ...     def update(self, *args, **kwargs):
+    ...         print 'update'
+    ...         super(Demo, self).update(*args, **kwargs)
+    ...     def clear(self):
+    ...         print 'clear'
+    ...         super(Demo, self).clear()
+    ...
+    
+    >>> demo1 = Demo()
+    >>> demo2 = Demo([['foo', 'bar'], ['bing', 'baz']], sha='zam')
+    update
+    __setitem__ foo bar
+    __setitem__ bing baz
+    __setitem__ sha zam
+    >>> demo2.setdefault('babble')
+    __setitem__ babble None
+    >>> del demo2['bing']
+    pop bing ---
+    >>> demo2.popitem()
+    pop babble ---
+    ('babble', None)
+    >>> demo2.clear()
+    clear
+
+
 Regression tests
 ----------------
 
@@ -239,4 +287,11 @@
     >>> len(d)
     1
 
+Legacy tests
+------------
 
+Old databases may need to find something importable from zc.dict.ordered.
+
+    >>> from zc.dict.ordered import OrderedDict as Olde
+    >>> Olde is OrderedDict
+    True



More information about the Checkins mailing list