SVN: Sandbox/chrism/zodb/3.6.0/weakset/ zope_weakset egg module.
Chris McDonough <[email protected]> Fri, 3 Mar 2006 13:12:44 -0500 (EST)
| Newsgroups | gmane.comp.web.zope.public-cvs |
|---|---|
| Message-ID | <[email protected]> |
Log message for revision 65769: zope_weakset egg module. Changed: A Sandbox/chrism/zodb/3.6.0/weakset/ A Sandbox/chrism/zodb/3.6.0/weakset/setup.py A Sandbox/chrism/zodb/3.6.0/weakset/src/ A Sandbox/chrism/zodb/3.6.0/weakset/src/zope/ A Sandbox/chrism/zodb/3.6.0/weakset/src/zope/__init__.py A Sandbox/chrism/zodb/3.6.0/weakset/src/zope/weakset/ A Sandbox/chrism/zodb/3.6.0/weakset/src/zope/weakset/__init__.py A Sandbox/chrism/zodb/3.6.0/weakset/src/zope/weakset/tests.py -=- Added: Sandbox/chrism/zodb/3.6.0/weakset/setup.py =================================================================== --- Sandbox/chrism/zodb/3.6.0/weakset/setup.py 2006-03-03 15:16:45 UTC (rev 65768) +++ Sandbox/chrism/zodb/3.6.0/weakset/setup.py 2006-03-03 18:12:44 UTC (rev 65769) @@ -0,0 +1,66 @@ +############################################################################## +# +# 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. +# +############################################################################## +"""Zope Object Database: object database and persistence + +The Zope Object Database provides an object-oriented database for +Python that provides a high-degree of transparency. Applications can +take advantage of object database features with few, if any, changes +to application logic. ZODB includes features such as a plugable storage +interface, rich transaction support, and undo. + +This distribution includes the weakset module from ZODB. +""" + +# The (non-obvious!) choices for the Trove Development Status line: +# Development Status :: 5 - Production/Stable +# Development Status :: 4 - Beta +# Development Status :: 3 - Alpha + +import os +from setuptools import setup + +classifiers = """\ +Development Status :: 5 - Production/Stable +Intended Audience :: Developers +License :: OSI Approved :: Zope Public License +Programming Language :: Python +Topic :: Database +Topic :: Software Development :: Libraries :: Python Modules +Operating System :: Microsoft :: Windows +Operating System :: Unix +""" + +setup(name='zope_weakset', + version='3.6.0', + url='http://svn.zope.org/ZODB', + download_url = "http://www.zope.org/Products/ZODB3.6", + license='ZPL 2.1', + description='ZODB weakset implementation', + author='Zope Corporation and Contributors', + maintainer='Zope Corporation and Contributors', + author_email='[email protected]', + maintainer_email='[email protected]', + platforms = ['any'], + classifiers = filter(None, classifiers.split("\n")), + long_description = __doc__, + packages=['zope', 'zope.weakset'], + package_dir = {'': 'src'}, + namespace_packages = ['zope'], + tests_require = [], + install_requires=[], + include_package_data = True, + zip_safe = True, + ) + + Property changes on: Sandbox/chrism/zodb/3.6.0/weakset/setup.py ___________________________________________________________________ Name: svn:eol-style + native Added: Sandbox/chrism/zodb/3.6.0/weakset/src/zope/__init__.py =================================================================== --- Sandbox/chrism/zodb/3.6.0/weakset/src/zope/__init__.py 2006-03-03 15:16:45 UTC (rev 65768) +++ Sandbox/chrism/zodb/3.6.0/weakset/src/zope/__init__.py 2006-03-03 18:12:44 UTC (rev 65769) @@ -0,0 +1 @@ +# Package Property changes on: Sandbox/chrism/zodb/3.6.0/weakset/src/zope/__init__.py ___________________________________________________________________ Name: svn:eol-style + native Added: Sandbox/chrism/zodb/3.6.0/weakset/src/zope/weakset/__init__.py =================================================================== --- Sandbox/chrism/zodb/3.6.0/weakset/src/zope/weakset/__init__.py 2006-03-03 15:16:45 UTC (rev 65768) +++ Sandbox/chrism/zodb/3.6.0/weakset/src/zope/weakset/__init__.py 2006-03-03 18:12:44 UTC (rev 65769) @@ -0,0 +1,81 @@ +############################################################################## +# +# Copyright (c) 2001, 2002 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 +# +############################################################################## + +import weakref + +# A simple implementation of weak sets, supplying just enough of Python's +# sets.Set interface for our needs. + +class WeakSet(object): + """A set of objects that doesn't keep its elements alive. + + The objects in the set must be weakly referencable. + The objects need not be hashable, and need not support comparison. + Two objects are considered to be the same iff their id()s are equal. + + When the only references to an object are weak references (including + those from WeakSets), the object can be garbage-collected, and + will vanish from any WeakSets it may be a member of at that time. + """ + + def __init__(self): + # Map id(obj) to obj. By using ids as keys, we avoid requiring + # that the elements be hashable or comparable. + self.data = weakref.WeakValueDictionary() + + def __len__(self): + return len(self.data) + + def __contains__(self, obj): + return id(obj) in self.data + + # Same as a Set, add obj to the collection. + def add(self, obj): + self.data[id(obj)] = obj + + # Same as a Set, remove obj from the collection, and raise + # KeyError if obj not in the collection. + def remove(self, obj): + del self.data[id(obj)] + + # f is a one-argument function. Execute f(elt) for each elt in the + # set. f's return value is ignored. + def map(self, f): + for wr in self.as_weakref_list(): + elt = wr() + if elt is not None: + f(elt) + + # Return a list of weakrefs to all the objects in the collection. + # Because a weak dict is used internally, iteration is dicey (the + # underlying dict may change size during iteration, due to gc or + # activity from other threads). as_weakef_list() is safe. + # + # Something like this should really be a method of Python's weak dicts. + # If we invoke self.data.values() instead, we get back a list of live + # objects instead of weakrefs. If gc occurs while this list is alive, + # all the objects move to an older generation (because they're strongly + # referenced by the list!). They can't get collected then, until a + # less frequent collection of the older generation. Before then, if we + # invoke self.data.values() again, they're still alive, and if gc occurs + # while that list is alive they're all moved to yet an older generation. + # And so on. Stress tests showed that it was easy to get into a state + # where a WeakSet grows without bounds, despite that almost all its + # elements are actually trash. By returning a list of weakrefs instead, + # we avoid that, although the decision to use weakrefs is now# very + # visible to our clients. + def as_weakref_list(self): + # We're cheating by breaking into the internals of Python's + # WeakValueDictionary here (accessing its .data attribute). + return self.data.data.values() Property changes on: Sandbox/chrism/zodb/3.6.0/weakset/src/zope/weakset/__init__.py ___________________________________________________________________ Name: svn:eol-style + native Added: Sandbox/chrism/zodb/3.6.0/weakset/src/zope/weakset/tests.py =================================================================== --- Sandbox/chrism/zodb/3.6.0/weakset/src/zope/weakset/tests.py 2006-03-03 15:16:45 UTC (rev 65768) +++ Sandbox/chrism/zodb/3.6.0/weakset/src/zope/weakset/tests.py 2006-03-03 18:12:44 UTC (rev 65769) @@ -0,0 +1,80 @@ +############################################################################## +# +# 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 +# +############################################################################## + +import unittest +from __init__ import WeakSet + +class Dummy: + pass + +class WeakSetTests(unittest.TestCase): + def test_contains(self): + w = WeakSet() + dummy = Dummy() + w.add(dummy) + self.assertEqual(dummy in w, True) + dummy2 = Dummy() + self.assertEqual(dummy2 in w, False) + + def test_len(self): + w = WeakSet() + d1 = Dummy() + d2 = Dummy() + w.add(d1) + w.add(d2) + self.assertEqual(len(w), 2) + del d1 + self.assertEqual(len(w), 1) + + def test_remove(self): + w = WeakSet() + dummy = Dummy() + w.add(dummy) + self.assertEqual(dummy in w, True) + w.remove(dummy) + self.assertEqual(dummy in w, False) + + def test_as_weakref_list(self): + w = WeakSet() + dummy = Dummy() + dummy2 = Dummy() + dummy3 = Dummy() + w.add(dummy) + w.add(dummy2) + w.add(dummy3) + del dummy3 + L = [x() for x in w.as_weakref_list()] + self.assertEqual(L, [dummy, dummy2]) + + def test_map(self): + w = WeakSet() + dummy = Dummy() + dummy2 = Dummy() + dummy3 = Dummy() + w.add(dummy) + w.add(dummy2) + w.add(dummy3) + def poker(x): + x.poked = 1 + w.map(poker) + for thing in dummy, dummy2, dummy3: + self.assertEqual(thing.poked, 1) + + +def test_suite(): + return unittest.makeSuite(WeakSetTests) + +if __name__ == '__main__': + unittest.main() + Property changes on: Sandbox/chrism/zodb/3.6.0/weakset/src/zope/weakset/tests.py ___________________________________________________________________ Name: svn:eol-style + native _______________________________________________ Zope-CVS maillist - [email protected] http://mail.zope.org/mailman/listinfo/zope-cvs Zope CVS instructions: http://dev.zope.org/CVS