[Checkins] SVN: zope.cachedescriptors/trunk/s removed use of Btree;
thus removing dependency on ZODB
Christian Zagrodnick
cz at gocept.com
Tue Sep 18 05:36:00 EDT 2007
Log message for revision 79733:
removed use of Btree; thus removing dependency on ZODB
Changed:
U zope.cachedescriptors/trunk/setup.py
U zope.cachedescriptors/trunk/src/zope/cachedescriptors/method.py
U zope.cachedescriptors/trunk/src/zope/cachedescriptors/method.txt
-=-
Modified: zope.cachedescriptors/trunk/setup.py
===================================================================
--- zope.cachedescriptors/trunk/setup.py 2007-09-18 09:16:38 UTC (rev 79732)
+++ zope.cachedescriptors/trunk/setup.py 2007-09-18 09:36:00 UTC (rev 79733)
@@ -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/trunk/src/zope/cachedescriptors/method.py
===================================================================
--- zope.cachedescriptors/trunk/src/zope/cachedescriptors/method.py 2007-09-18 09:16:38 UTC (rev 79732)
+++ zope.cachedescriptors/trunk/src/zope/cachedescriptors/method.py 2007-09-18 09:36:00 UTC (rev 79733)
@@ -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/trunk/src/zope/cachedescriptors/method.txt
===================================================================
--- zope.cachedescriptors/trunk/src/zope/cachedescriptors/method.txt 2007-09-18 09:16:38 UTC (rev 79732)
+++ zope.cachedescriptors/trunk/src/zope/cachedescriptors/method.txt 2007-09-18 09:36:00 UTC (rev 79733)
@@ -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