[Checkins] SVN: zc.dict/trunk/ Initial version of zc.dict -- a persistent BTree based "dict".

Albertas Agejevas alga at pov.lt
Tue Jul 3 14:39:29 EDT 2007


Log message for revision 77375:
  Initial version of zc.dict -- a persistent BTree based "dict".
  

Changed:
  A   zc.dict/trunk/README.txt
  A   zc.dict/trunk/bootstrap.py
  A   zc.dict/trunk/buildout.cfg
  A   zc.dict/trunk/setup.py
  A   zc.dict/trunk/src/zc/__init__.py
  A   zc.dict/trunk/src/zc/dict/__init__.py
  A   zc.dict/trunk/src/zc/dict/dict.py
  A   zc.dict/trunk/src/zc/dict/dict.txt
  A   zc.dict/trunk/src/zc/dict/tests.py

-=-
Added: zc.dict/trunk/README.txt
===================================================================
--- zc.dict/trunk/README.txt	                        (rev 0)
+++ zc.dict/trunk/README.txt	2007-07-03 18:39:28 UTC (rev 77375)
@@ -0,0 +1,5 @@
+A BTree-based persistent dict-like object that can be used as a base class.
+
+This is a bit of a heavyweight solution, as every zc.dict.Dict is at
+least 3 persistent objects.  Keep this in mind if you intend to create
+lots and lots of these.


Property changes on: zc.dict/trunk/README.txt
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: zc.dict/trunk/bootstrap.py
===================================================================
--- zc.dict/trunk/bootstrap.py	                        (rev 0)
+++ zc.dict/trunk/bootstrap.py	2007-07-03 18:39:28 UTC (rev 77375)
@@ -0,0 +1,52 @@
+##############################################################################
+#
+# Copyright (c) 2006 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Bootstrap a buildout-based project
+
+Simply run this script in a directory containing a buildout.cfg.
+The script accepts buildout command-line options, so you can
+use the -c option to specify an alternate configuration file.
+
+$Id$
+"""
+
+import os, shutil, sys, tempfile, urllib2
+
+tmpeggs = tempfile.mkdtemp()
+
+ez = {}
+exec urllib2.urlopen('http://peak.telecommunity.com/dist/ez_setup.py'
+                     ).read() in ez
+ez['use_setuptools'](to_dir=tmpeggs, download_delay=0)
+
+import pkg_resources
+
+cmd = 'from setuptools.command.easy_install import main; main()'
+if sys.platform == 'win32':
+    cmd = '"%s"' % cmd # work around spawn lamosity on windows
+
+ws = pkg_resources.working_set
+assert os.spawnle(
+    os.P_WAIT, sys.executable, sys.executable,
+    '-c', cmd, '-mqNxd', tmpeggs, 'zc.buildout',
+    dict(os.environ,
+         PYTHONPATH=
+         ws.find(pkg_resources.Requirement.parse('setuptools')).location
+         ),
+    ) == 0
+
+ws.add_entry(tmpeggs)
+ws.require('zc.buildout')
+import zc.buildout.buildout
+zc.buildout.buildout.main(sys.argv[1:] + ['bootstrap'])
+shutil.rmtree(tmpeggs)


Property changes on: zc.dict/trunk/bootstrap.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: zc.dict/trunk/buildout.cfg
===================================================================
--- zc.dict/trunk/buildout.cfg	                        (rev 0)
+++ zc.dict/trunk/buildout.cfg	2007-07-03 18:39:28 UTC (rev 77375)
@@ -0,0 +1,15 @@
+[buildout]
+develop = .
+parts = test py
+
+find-links = http://download.zope.org/distribution/
+
+[test]
+recipe = zc.recipe.testrunner
+eggs = zc.dict
+
+[py]
+recipe = zc.recipe.egg
+eggs = setuptools
+interpreter = py
+scripts = py


Property changes on: zc.dict/trunk/buildout.cfg
___________________________________________________________________
Name: svn:keywords
   + Id

Added: zc.dict/trunk/setup.py
===================================================================
--- zc.dict/trunk/setup.py	                        (rev 0)
+++ zc.dict/trunk/setup.py	2007-07-03 18:39:28 UTC (rev 77375)
@@ -0,0 +1,20 @@
+from setuptools import setup
+
+setup(
+    name="zc.dict",
+    version="1.0b1",
+    license="ZPL 2.1",
+    author="Zope Corporation",
+    author_email="zope3-dev at zope.org",
+
+    namespace_packages=["zc"],
+    packages=["zc", "zc.dict"],
+    package_dir={"": "src"},
+    include_package_data=True,
+    install_requires=["setuptools", "zope.interface", "ZODB3"],
+    tests_require=["zope.testing"],
+    description=open('README.txt').read(),
+    long_description=open("src/zc/dict/dict.txt").read(),
+    keywords="zope zope3",
+    zip_safe=False
+    )


Property changes on: zc.dict/trunk/setup.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: zc.dict/trunk/src/zc/__init__.py
===================================================================
--- zc.dict/trunk/src/zc/__init__.py	                        (rev 0)
+++ zc.dict/trunk/src/zc/__init__.py	2007-07-03 18:39:28 UTC (rev 77375)
@@ -0,0 +1,7 @@
+# this is a namespace package
+try:
+    import pkg_resources
+    pkg_resources.declare_namespace(__name__)
+except ImportError:
+    import pkgutil
+    __path__ = pkgutil.extend_path(__path__, __name__)


Property changes on: zc.dict/trunk/src/zc/__init__.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: zc.dict/trunk/src/zc/dict/__init__.py
===================================================================
--- zc.dict/trunk/src/zc/dict/__init__.py	                        (rev 0)
+++ zc.dict/trunk/src/zc/dict/__init__.py	2007-07-03 18:39:28 UTC (rev 77375)
@@ -0,0 +1 @@
+from zc.dict.dict import Dict # reexport


Property changes on: zc.dict/trunk/src/zc/dict/__init__.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: zc.dict/trunk/src/zc/dict/dict.py
===================================================================
--- zc.dict/trunk/src/zc/dict/dict.py	                        (rev 0)
+++ zc.dict/trunk/src/zc/dict/dict.py	2007-07-03 18:39:28 UTC (rev 77375)
@@ -0,0 +1,94 @@
+##############################################################################
+#
+# Copyright (c) 2007 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""zc.dict -- A BTree based persistent mapping
+
+$Id$
+"""
+from BTrees.OOBTree import OOBTree
+from persistent import Persistent
+
+
+class Dict(Persistent):
+    """A BTree-based dict-like persistent object that can be safely
+    inherited from.
+    """
+
+    def __init__(self, dict=None, **kwargs):
+        self._data = OOBTree()
+        self.__len = 0
+        if dict is not None:
+            self.update(dict)
+        if len(kwargs):
+            self.update(kwargs)
+
+    def __setitem__(self, key, value):
+        self.__len += 1
+        self._data[key] = value
+
+    def __delitem__(self, key):
+        del self._data[key]
+        self.__len -= 1
+
+    def update(self, other):
+        self._data.update(other)
+        self.__len = len(self._data)
+
+    def clear(self):
+        self._data.clear()
+        self.__len = 0
+
+    def __len__(self):
+        return self.__len
+
+    def keys(self):
+        return list(self._data.keys())
+
+    def values(self):
+        return list(self._data.values())
+
+    def items(self):
+        return list(self._data.items())
+
+    def copy(self):
+        if self.__class__ is Dict:
+            return Dict(OOBTree(self._data))
+        import copy
+        data = self._data
+        try:
+            self._data = OOBTree()
+            c = copy.copy(self)
+        finally:
+            self._data = data
+        c.update(self)
+        return c
+
+    def __getitem__(self, key): return self._data[key]
+    def __iter__(self): return iter(self._data)
+    def iteritems(self): return self._data.iteritems()
+    def iterkeys(self): return self._data.iterkeys()
+    def itervalues(self): return self._data.itervalues()
+    def has_key(self, key): return bool(self._data.has_key(key))
+    def get(self, key, failobj=None): return self._data.get(key, failobj)
+    def setdefault(self, key, failobj=None): self._data.setdefault(key, failobj)
+    def pop(self, key, *args): return self._data.pop(key, *args)
+    def __contains__(self, key): return self._data.__contains__(key)
+
+    def popitem(self):
+        try:
+            k, v = self.iteritems().next()
+        except StopIteration:
+            raise KeyError, 'container is empty'
+        del self[k]
+        return (k, v)
+


Property changes on: zc.dict/trunk/src/zc/dict/dict.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: zc.dict/trunk/src/zc/dict/dict.txt
===================================================================
--- zc.dict/trunk/src/zc/dict/dict.txt	                        (rev 0)
+++ zc.dict/trunk/src/zc/dict/dict.txt	2007-07-03 18:39:28 UTC (rev 77375)
@@ -0,0 +1,112 @@
+An efficient, persistent and subclassable dict
+==============================================
+
+PersistentDict is very inefficient if it contains more than a couple
+of values, and BTrees are not recommended to inherit from.
+
+This class is a simple wrapper over a BTree.  It retains the
+efficiency of BTrees and is safe to use as a base class.  Also, it
+implements the full Python dict interface.
+
+   >>> from zc.dict import Dict
+   >>> d = Dict()
+   >>> d
+   <zc.dict.dict.Dict object at ...>
+
+   >>> d['foo'] = 'bar'
+   >>> len(d)
+   1
+
+   >>> d['bar'] = 'baz'
+   >>> len(d)
+   2
+
+Length is maintained separately, because len on a BTree is
+inefficient, as it has to walk the whole tree.
+
+   >>> d._Dict__len
+   2
+
+As after an update operation we cannot easily adjust the length, it is
+recalculated:
+
+   >>> d.update({'bar': 'moo', 'ding': 'dong', 'beep': 'beep'})
+   >>> len(d)
+   4
+
+keys and values, and items return normal Python lists:
+
+   >>> d.keys()
+   ['bar', 'beep', 'ding', 'foo']
+
+   >>> d.values()
+   ['moo', 'beep', 'dong', 'bar']
+
+   >>> d.items()
+   [('bar', 'moo'), ('beep', 'beep'), ('ding', 'dong'), ('foo', 'bar')]
+
+However, efficient BTree iterators are available via the iter methods:
+
+   >>> iter(d)
+   <OO-iterator object at ...>
+   >>> d.iterkeys()
+   <OO-iterator object at ...>
+
+   >>> d.iteritems()
+   <OO-iterator object at ...>
+
+   >>> d.itervalues()
+   <OO-iterator object at ...>
+
+The rest of the dict methods are delegated to the underlying BTree:
+
+   >>> d.has_key('bar')
+   True
+   >>> 'BAR' in d
+   False
+
+   >>> d.pop('bar')
+   'moo'
+
+   >>> d.get('nonexistent', 'default')
+   'default'
+
+   >>> d.setdefault('newly created', 'value')
+   >>> d['newly created']
+   'value'
+   >>> d.setdefault('newly created', 'other')
+   >>> d['newly created']
+   'value'
+
+popitem removes from the dict and returns a key-value pair:
+
+   >>> len(d)
+   4
+
+   >>> d.popitem()
+   ('beep', 'beep')
+
+   >>> len(d)
+   3
+
+The copy method creates a copy of a Dict:
+
+   >>> c = d.copy()
+   >>> c.items() == d.items()
+   True
+
+However we don't support comparison because of cowardice:
+
+   >>> c == d
+   False
+   >>> Dict() == {}
+   False
+
+clear removes all the keys from the dict:
+
+   >>> d.clear()
+   >>> d.keys()
+   []
+   >>> len(d)
+   0
+


Property changes on: zc.dict/trunk/src/zc/dict/dict.txt
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: zc.dict/trunk/src/zc/dict/tests.py
===================================================================
--- zc.dict/trunk/src/zc/dict/tests.py	                        (rev 0)
+++ zc.dict/trunk/src/zc/dict/tests.py	2007-07-03 18:39:28 UTC (rev 77375)
@@ -0,0 +1,29 @@
+##############################################################################
+#
+# Copyright (c) 2007 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Tests for zc.dict
+
+$Id$
+"""
+import unittest
+from zope.testing import doctest
+
+def test_suite():
+    return unittest.TestSuite([
+        doctest.DocFileSuite('dict.txt',
+                             optionflags=doctest.INTERPRET_FOOTNOTES
+                             |doctest.REPORT_NDIFF|doctest.ELLIPSIS),
+        ])
+
+if __name__ == '__main__':
+    unittest.main(defaultTest='test_suite')


Property changes on: zc.dict/trunk/src/zc/dict/tests.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native



More information about the Checkins mailing list