[Checkins] SVN: zope.cachedescriptors/branches/3.4/ - Backported changes from trunk which removes dependency to ZODB.

Christian Zagrodnick cz at gocept.com
Wed Apr 23 02:59:37 EDT 2008


Log message for revision 85630:
  - Backported changes from trunk which removes dependency to ZODB.
  
  

Changed:
  U   zope.cachedescriptors/branches/3.4/CHANGES.txt
  U   zope.cachedescriptors/branches/3.4/setup.py
  U   zope.cachedescriptors/branches/3.4/src/zope/cachedescriptors/method.py
  U   zope.cachedescriptors/branches/3.4/src/zope/cachedescriptors/method.txt

-=-
Modified: zope.cachedescriptors/branches/3.4/CHANGES.txt
===================================================================
--- zope.cachedescriptors/branches/3.4/CHANGES.txt	2008-04-23 00:25:25 UTC (rev 85629)
+++ zope.cachedescriptors/branches/3.4/CHANGES.txt	2008-04-23 06:59:31 UTC (rev 85630)
@@ -1,7 +1,11 @@
 Changes for zope.cachedescriptors
 =================================
 
+3.4.1 (2008-04-23)
+------------------
 
+- Backported changes from trunk which removes dependency to ZODB.
+
 3.4.0 (2007-08-30)
 ------------------
 

Modified: zope.cachedescriptors/branches/3.4/setup.py
===================================================================
--- zope.cachedescriptors/branches/3.4/setup.py	2008-04-23 00:25:25 UTC (rev 85629)
+++ zope.cachedescriptors/branches/3.4/setup.py	2008-04-23 06:59:31 UTC (rev 85630)
@@ -61,7 +61,6 @@
     package_dir={'': 'src'},
     namespace_packages=['zope',],
     include_package_data=True,
-    install_requires=['setuptools',
-                      'ZODB3'], # persistent
+    install_requires=['setuptools'],
     zip_safe=False,
     )

Modified: zope.cachedescriptors/branches/3.4/src/zope/cachedescriptors/method.py
===================================================================
--- zope.cachedescriptors/branches/3.4/src/zope/cachedescriptors/method.py	2008-04-23 00:25:25 UTC (rev 85629)
+++ zope.cachedescriptors/branches/3.4/src/zope/cachedescriptors/method.py	2008-04-23 06:59:31 UTC (rev 85630)
@@ -14,14 +14,12 @@
 $Id$
 """
 
-import BTrees.OOBTree
-
-
 class cachedIn(object):
     """Cached method with given cache attribute."""
 
-    def __init__(self, attribute_name):
+    def __init__(self, attribute_name, factory=dict):
         self.attribute_name = attribute_name
+        self.factory = factory
 
     def __call__(self, func):
 
@@ -49,7 +47,7 @@
         try:
             cache = getattr(instance, self.attribute_name)
         except AttributeError:
-            cache = BTrees.OOBTree.OOBTree()
+            cache = self.factory()
             setattr(instance, self.attribute_name, cache)
         return cache
 

Modified: zope.cachedescriptors/branches/3.4/src/zope/cachedescriptors/method.txt
===================================================================
--- zope.cachedescriptors/branches/3.4/src/zope/cachedescriptors/method.txt	2008-04-23 00:25:25 UTC (rev 85629)
+++ zope.cachedescriptors/branches/3.4/src/zope/cachedescriptors/method.txt	2008-04-23 06:59:31 UTC (rev 85630)
@@ -5,12 +5,12 @@
 --------
 
 The `cachedIn` property allows to specify the attribute where to store the
-computed value::
+computed value:
 
     >>> import math
     >>> from zope.cachedescriptors import method
 
-    >>> class Point:
+    >>> class Point(object):
     ... 
     ...     def __init__(self, x, y):
     ...         self.x, self.y = x, y
@@ -19,10 +19,10 @@
     ...     def distance(self, x, y):
     ...         print 'computing distance'
     ...         return math.sqrt((self.x - x)**2 + (self.y - y)**2)
-    
+    ...
     >>> point = Point(1.0, 2.0)   
 
-The value is computed once::
+The value is computed once:
 
     >>> point.distance(2, 2)
     computing distance
@@ -31,7 +31,7 @@
     1.0
 
 
-Using different arguments calculates a new distance::
+Using different arguments calculates a new distance:
 
     >>> point.distance(5, 2)
     computing distance
@@ -40,23 +40,49 @@
     4.0
 
 
-The data is stored at the given `_cache` attribute::
+The data is stored at the given `_cache` attribute:
 
-    >>> point._cache
-    <BTrees.OOBTree.OOBTree object at 0x...>
+    >>> isinstance(point._cache, dict)
+    True
 
-    >>> list(sorted(point._cache.items()))
+    >>> sorted(point._cache.items())
     [(((2, 2), ()), 1.0), (((5, 2), ()), 4.0)]
 
 
-It is possible to exlicitly invalidate the data::
+It is possible to exlicitly invalidate the data:
 
     >>> point.distance.invalidate(point, 5, 2)
     >>> point.distance(5, 2)
     computing distance
     4.0
 
-Invalidating keys which are not in the cache, does not result in an error::
+Invalidating keys which are not in the cache, does not result in an error:
 
+    >>> point.distance.invalidate(point, 47, 11)
 
-    >>> point.distance.invalidate(point, 47, 11)
+
+It is possible to pass in a factory for the cache attribute. Create another
+Point class:
+
+
+    >>> class MyDict(dict):
+    ...     pass
+    >>> class Point(object):
+    ... 
+    ...     def __init__(self, x, y):
+    ...         self.x, self.y = x, y
+    ...
+    ...     @method.cachedIn('_cache', MyDict)
+    ...     def distance(self, x, y):
+    ...         print 'computing distance'
+    ...         return math.sqrt((self.x - x)**2 + (self.y - y)**2)
+    ...
+    >>> point = Point(1.0, 2.0)   
+    >>> point.distance(2, 2)
+    computing distance
+    1.0
+
+Now the cache is a MyDict instance:
+
+    >>> isinstance(point._cache, MyDict)
+    True



More information about the Checkins mailing list