[Checkins] SVN: zope.cachedescriptors/trunk/src/zope/cachedescriptors/ added cachedIn decorator/descriptor for methods

Christian Zagrodnick cz at gocept.com
Thu May 10 05:51:53 EDT 2007


Log message for revision 75668:
  added cachedIn decorator/descriptor for methods

Changed:
  U   zope.cachedescriptors/trunk/src/zope/cachedescriptors/README.txt
  A   zope.cachedescriptors/trunk/src/zope/cachedescriptors/method.py
  A   zope.cachedescriptors/trunk/src/zope/cachedescriptors/method.txt
  U   zope.cachedescriptors/trunk/src/zope/cachedescriptors/tests.py

-=-
Modified: zope.cachedescriptors/trunk/src/zope/cachedescriptors/README.txt
===================================================================
--- zope.cachedescriptors/trunk/src/zope/cachedescriptors/README.txt	2007-05-10 09:30:07 UTC (rev 75667)
+++ zope.cachedescriptors/trunk/src/zope/cachedescriptors/README.txt	2007-05-10 09:51:52 UTC (rev 75668)
@@ -21,4 +21,4 @@
      arguments and on any instance attributes that the methods are
      defined to depend on.
 
-     **Note**, methods haven't been implemented yet.
+     **Note**, only a cache based on arguments has been implemented so far.

Added: zope.cachedescriptors/trunk/src/zope/cachedescriptors/method.py
===================================================================
--- zope.cachedescriptors/trunk/src/zope/cachedescriptors/method.py	2007-05-10 09:30:07 UTC (rev 75667)
+++ zope.cachedescriptors/trunk/src/zope/cachedescriptors/method.py	2007-05-10 09:51:52 UTC (rev 75668)
@@ -0,0 +1,47 @@
+##############################################################################
+# 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.
+##############################################################################
+"""Cached Methods
+
+$Id$
+"""
+
+import BTrees.OOBTree
+
+
+class cachedIn(object):
+    """Cached method with given cache attribute."""
+
+    def __init__(self, attribute_name):
+        self.attribute_name = attribute_name
+
+    def __call__(self, func):
+
+        def decorated(instance, *args, **kwargs):
+            kw = kwargs.items()
+            kw.sort()
+            key = (args, tuple(kw))
+            cache = self.cache(instance)
+            try:
+                v = cache[key]
+            except KeyError:
+                v = cache[key] = func(instance, *args, **kwargs)
+            return v
+
+        return decorated
+
+    def cache(self, instance):
+        try:
+            cache = getattr(instance, self.attribute_name)
+        except AttributeError:
+            cache = BTrees.OOBTree.OOBTree()
+            setattr(instance, self.attribute_name, cache)
+        return cache


Property changes on: zope.cachedescriptors/trunk/src/zope/cachedescriptors/method.py
___________________________________________________________________
Name: svn:keywords
   + Id Rev Date
Name: svn:eol-style
   + native

Added: zope.cachedescriptors/trunk/src/zope/cachedescriptors/method.txt
===================================================================
--- zope.cachedescriptors/trunk/src/zope/cachedescriptors/method.txt	2007-05-10 09:30:07 UTC (rev 75667)
+++ zope.cachedescriptors/trunk/src/zope/cachedescriptors/method.txt	2007-05-10 09:51:52 UTC (rev 75668)
@@ -0,0 +1,52 @@
+============
+Method Cache
+============
+
+
+
+cachedIn
+========
+
+The `cachedIn` property allows to specify the attribute where to store the
+computed value::
+
+    >>> import math
+    >>> from zope.cachedescriptors import method
+
+    >>> class Point:
+    ... 
+    ...     def __init__(self, x, y):
+    ...         self.x, self.y = x, y
+    ...
+    ...     @method.cachedIn('_cache')
+    ...     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::
+
+    >>> point.distance(2, 2)
+    computing distance
+    1.0
+    >>> point.distance(2, 2)
+    1.0
+
+
+Using different arguments calculates a new distance::
+
+    >>> point.distance(5, 2)
+    computing distance
+    4.0
+    >>> point.distance(5, 2)
+    4.0
+
+
+The data is stored at the given `_cache` attribute::
+
+    >>> point._cache
+    <BTrees.OOBTree.OOBTree object at 0x...>
+
+    >>> list(sorted(point._cache.items()))
+    [(((2, 2), ()), 1.0), (((5, 2), ()), 4.0)]


Property changes on: zope.cachedescriptors/trunk/src/zope/cachedescriptors/method.txt
___________________________________________________________________
Name: svn:keywords
   + Id Rev Date
Name: svn:eol-style
   + native

Modified: zope.cachedescriptors/trunk/src/zope/cachedescriptors/tests.py
===================================================================
--- zope.cachedescriptors/trunk/src/zope/cachedescriptors/tests.py	2007-05-10 09:30:07 UTC (rev 75667)
+++ zope.cachedescriptors/trunk/src/zope/cachedescriptors/tests.py	2007-05-10 09:51:52 UTC (rev 75668)
@@ -17,11 +17,14 @@
 """
 import unittest
 
+from zope.testing import doctest
+
+
 def test_suite():
-    from zope.testing import doctest
-    return doctest.DocFileSuite('property.txt')
+    return doctest.DocFileSuite(
+        'property.txt', 'method.txt',
+        optionflags=doctest.ELLIPSIS)
 
+
 if __name__ == '__main__':
     unittest.main(defaultTest='test_suite')
-
-



More information about the Checkins mailing list