[Zope3-checkins] SVN: Zope3/branches/jim-session/src/zope/cachedescriptors/ Provide property for caching volatile attrs

Jim Fulton jim at zope.com
Sun Jun 6 11:51:18 EDT 2004


Log message for revision 25279:
Provide property for caching volatile attrs


-=-
Modified: Zope3/branches/jim-session/src/zope/cachedescriptors/property.py
===================================================================
--- Zope3/branches/jim-session/src/zope/cachedescriptors/property.py	2004-06-06 15:49:41 UTC (rev 25278)
+++ Zope3/branches/jim-session/src/zope/cachedescriptors/property.py	2004-06-06 15:51:18 UTC (rev 25279)
@@ -90,3 +90,41 @@
         setattr(inst, value_name, value)
         
         return value
+
+class Volatile(object):
+    """Volatile attributs
+
+    Provide access to a volatile attribute, computing it if it
+    doesn't exist.
+
+    For example, suppose we want to have a volatile attribute,
+    _v_data that contains some data we want to cache:
+
+      >>> class C:
+      ...     _v_data = Volatile('_v_data', dict)
+
+      >>> c = C()
+      >>> data = c._v_data
+      >>> data['x'] = 1
+      >>> c._v_data
+      {'x': 1}
+      >>> c._v_data is data
+      True
+
+    Note that the name passed to the constructor must match the
+    name the proprty is assigned to.
+    """
+
+    def __init__(self, name, initfunc):
+        self.name, self.initfunc = name, initfunc
+
+    def __get__(self, inst, class_):
+        # Note that this is only called when an instance doesn't
+        # already have the var
+        if inst is None:
+            return self
+
+        value = self.initfunc()
+        setattr(inst, self.name, value)
+        return value
+    

Copied: Zope3/branches/jim-session/src/zope/cachedescriptors/tests.py (from rev 25125, Zope3/trunk/src/zope/cachedescriptors/tests/test_property.py)
===================================================================
--- Zope3/trunk/src/zope/cachedescriptors/tests/test_property.py	2004-05-29 13:20:46 UTC (rev 25125)
+++ Zope3/branches/jim-session/src/zope/cachedescriptors/tests.py	2004-06-06 15:51:18 UTC (rev 25279)
@@ -0,0 +1,104 @@
+##############################################################################
+#
+# Copyright (c) 2003 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (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.
+#
+##############################################################################
+"""XXX short summary goes here.
+
+XXX longer description goes here.
+
+$Id$
+"""
+__metaclass__ = type
+
+from unittest import TestCase, TestSuite, main, makeSuite
+from zope.cachedescriptors.property import CachedProperty
+
+class C:
+
+    called0 = called1 = called2 = 0
+    state1 = state2 = 0
+
+    def get0(self):
+        self.called0 += 1
+        return self.called0
+
+    def get1(self):
+        self.called1 += 1
+        return self.called1
+
+    def get2(self):
+        self.called2 += 1
+        return self.called2
+
+    p0 = CachedProperty(get0)
+    p1 = CachedProperty(get1, 'state1')
+    p2 = CachedProperty(get2, 'state1', 'state2')
+
+class Test(TestCase):
+
+    def test_no_parameters(self):
+        c = C()
+        self.assertEqual(c.p0, 1)
+        self.assertEqual(c.called0, 1)
+        self.assertEqual(c.p0, 1)
+        self.assertEqual(c.called0, 1)
+
+    def test_one_parameters(self):
+        c = C()
+        self.assertEqual(c.p1, 1)
+        self.assertEqual(c.called1, 1)
+        self.assertEqual(c.p1, 1)
+        self.assertEqual(c.called1, 1)
+        c.state2 += 1
+        self.assertEqual(c.p1, 1)
+        self.assertEqual(c.called1, 1)
+        c.state1 += 1
+        self.assertEqual(c.p1, 2)
+        self.assertEqual(c.called1, 2)
+        self.assertEqual(c.p1, 2)
+        self.assertEqual(c.called1, 2)
+
+    def test_multiple_parameters(self):
+        c = C()
+        self.assertEqual(c.p2, 1)
+        self.assertEqual(c.called2, 1)
+        self.assertEqual(c.p2, 1)
+        self.assertEqual(c.called2, 1)
+        c.x = 1
+        self.assertEqual(c.p2, 1)
+        self.assertEqual(c.called2, 1)
+        c.state2 += 1
+        self.assertEqual(c.p2, 2)
+        self.assertEqual(c.called2, 2)
+        self.assertEqual(c.p2, 2)
+        self.assertEqual(c.called2, 2)
+        c.state1 += 1
+        self.assertEqual(c.p2, 3)
+        self.assertEqual(c.called2, 3)
+        self.assertEqual(c.p2, 3)
+        self.assertEqual(c.called2, 3)
+        c.state1 += 1
+        c.state2 += 1
+        self.assertEqual(c.p2, 4)
+        self.assertEqual(c.called2, 4)
+        self.assertEqual(c.p2, 4)
+        self.assertEqual(c.called2, 4)
+
+def test_suite():
+    from doctest import DocTestSuite
+    return TestSuite((
+        makeSuite(Test),
+        DocTestSuite('zope.cachedescriptors.property')
+        ))
+
+if __name__=='__main__':
+    main(defaultTest='test_suite')




More information about the Zope3-Checkins mailing list