File: PKG-INFO

package info (click to toggle)
zope.cachedescriptors 3.5.0-4
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 152 kB
  • ctags: 30
  • sloc: python: 166; makefile: 2; sh: 1
file content (372 lines) | stat: -rw-r--r-- 11,468 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
Metadata-Version: 1.0
Name: zope.cachedescriptors
Version: 3.5.0
Summary: Method and property caching decorators
Home-page: http://pypi.python.org/pypi/zope.cachedescriptors
Author: Zope Corporation and Contributors
Author-email: zope-dev@zope.org
License: ZPL 2.1
Description: .. contents::
        
        ==================
        Cached descriptors
        ==================
        
        Cached descriptors cache their output.  They take into account
        instance attributes that they depend on, so when the instance
        attributes change, the descriptors will change the values they
        return.
        
        Cached descriptors cache their data in _v_ attributes, so they are
        also useful for managing the computation of volatile attributes for
        persistent objects.
        
        Persistent descriptors:
        
        property
        
        A simple computed property. See property.txt.
        
        method
        
        Idempotent method.  The return values are cached based on method
        arguments and on any instance attributes that the methods are
        defined to depend on.
        
        **Note**, only a cache based on arguments has been implemented so far.
        
        See method.txt.
        
        Cached Properties
        -----------------
        
        Cached properties are computed properties that cache their computed
        values.  They take into account instance attributes that they depend
        on, so when the instance attributes change, the properties will change
        the values they return.
        
        CachedProperty
        ~~~~~~~~~~~~~~
        
        Cached properties cache their data in _v_ attributes, so they are
        also useful for managing the computation of volatile attributes for
        persistent objects. Let's look at an example::
        
        >>> from zope.cachedescriptors import property
        >>> import math
        
        >>> class Point:
        ...
        ...     def __init__(self, x, y):
        ...         self.x, self.y = x, y
        ...
        ...     def radius(self):
        ...         print 'computing radius'
        ...         return math.sqrt(self.x**2 + self.y**2)
        ...     radius = property.CachedProperty(radius, 'x', 'y')
        
        >>> point = Point(1.0, 2.0)
        
        If we ask for the radius the first time::
        
        >>> '%.2f' % point.radius
        computing radius
        '2.24'
        
        We see that the radius function is called, but if we ask for it again::
        
        >>> '%.2f' % point.radius
        '2.24'
        
        The function isn't called.  If we change one of the attribute the
        radius depends on, it will be recomputed::
        
        >>> point.x = 2.0
        >>> '%.2f' % point.radius
        computing radius
        '2.83'
        
        But changing other attributes doesn't cause recomputation::
        
        >>> point.q = 1
        >>> '%.2f' % point.radius
        '2.83'
        
        Note that we don't have any non-volitile attributes added::
        
        >>> names = [name for name in point.__dict__ if not name.startswith('_v_')]
        >>> names.sort()
        >>> names
        ['q', 'x', 'y']
        
        
        Lazy Computed Attributes
        ~~~~~~~~~~~~~~~~~~~~~~~~
        
        The `property` module provides another descriptor that supports a
        slightly different caching model: lazy attributes.  Like cached
        proprties, they are computed the first time they are used. however,
        they aren't stored in volatile attributes and they aren't
        automatically updated when other attributes change.  Furthermore, the
        store their data using their attribute name, thus overriding
        themselves. This provides much faster attribute access after the
        attribute has been computed. Let's look at the previous example using
        lazy attributes::
        
        >>> class Point:
        ...
        ...     def __init__(self, x, y):
        ...         self.x, self.y = x, y
        ...
        ...     @property.Lazy
        ...     def radius(self):
        ...         print 'computing radius'
        ...         return math.sqrt(self.x**2 + self.y**2)
        
        >>> point = Point(1.0, 2.0)
        
        If we ask for the radius the first time::
        
        >>> '%.2f' % point.radius
        computing radius
        '2.24'
        
        We see that the radius function is called, but if we ask for it again::
        
        >>> '%.2f' % point.radius
        '2.24'
        
        The function isn't called.  If we change one of the attribute the
        radius depends on, it still isn't called::
        
        >>> point.x = 2.0
        >>> '%.2f' % point.radius
        '2.24'
        
        If we want the radius to be recomputed, we have to manually delete it::
        
        >>> del point.radius
        
        >>> point.x = 2.0
        >>> '%.2f' % point.radius
        computing radius
        '2.83'
        
        Note that the radius is stored in the instance dictionary::
        
        >>> '%.2f' % point.__dict__['radius']
        '2.83'
        
        The lazy attribute needs to know the attribute name.  It normally
        deduces the attribute name from the name of the function passed. If we
        want to use a different name, we need to pass it::
        
        >>> def d(point):
        ...     print 'computing diameter'
        ...     return 2*point.radius
        
        >>> Point.diameter = property.Lazy(d, 'diameter')
        >>> '%.2f' % point.diameter
        computing diameter
        '5.66'
        
        
        readproperty
        ~~~~~~~~~~~~
        
        readproperties are like lazy computed attributes except that the
        attribute isn't set by the property::
        
        
        >>> class Point:
        ...
        ...     def __init__(self, x, y):
        ...         self.x, self.y = x, y
        ...
        ...     @property.readproperty
        ...     def radius(self):
        ...         print 'computing radius'
        ...         return math.sqrt(self.x**2 + self.y**2)
        
        >>> point = Point(1.0, 2.0)
        
        >>> '%.2f' % point.radius
        computing radius
        '2.24'
        
        >>> '%.2f' % point.radius
        computing radius
        '2.24'
        
        But you *can* replace the property by setting a value. This is the major
        difference to the builtin `property`::
        
        >>> point.radius = 5
        >>> point.radius
        5
        
        
        cachedIn
        ~~~~~~~~
        
        The `cachedIn` property allows to specify the attribute where to store the
        computed value::
        
        >>> class Point:
        ...
        ...     def __init__(self, x, y):
        ...         self.x, self.y = x, y
        ...
        ...     @property.cachedIn('_radius_attribute')
        ...     def radius(self):
        ...         print 'computing radius'
        ...         return math.sqrt(self.x**2 + self.y**2)
        
        >>> point = Point(1.0, 2.0)
        
        >>> '%.2f' % point.radius
        computing radius
        '2.24'
        
        >>> '%.2f' % point.radius
        '2.24'
        
        The radius is cached in the attribute with the given name, `_radius_attribute`
        in this case::
        
        >>> '%.2f' % point._radius_attribute
        '2.24'
        
        When the attribute is removed the radius is re-calculated once. This allows
        invalidation::
        
        >>> del point._radius_attribute
        
        >>> '%.2f' % point.radius
        computing radius
        '2.24'
        
        >>> '%.2f' % point.radius
        '2.24'
        
        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(object):
        ...
        ...     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:
        
        >>> isinstance(point._cache, dict)
        True
        
        >>> sorted(point._cache.items())
        [(((2, 2), ()), 1.0), (((5, 2), ()), 4.0)]
        
        
        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:
        
        >>> 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
        
        =======
        CHANGES
        =======
        
        3.5.0 (2009-02-10)
        ------------------
        
        - Remove dependency on ZODB by allowing to specify storage factory for
        ``zope.cachedescriptors.method.cachedIn`` which is now `dict` by default.
        If you need to use BTree instead, you must pass it as `factory` argument
        to the ``zope.cachedescriptors.method.cachedIn`` decorator.
        
        - Remove zpkg-related file.
        
        - Clean up package description and documentation a bit.
        
        - Change package mailing list address to zope-dev at zope.org, as
        zope3-dev at zope.org is now retired.
        
        3.4.0 (2007-08-30)
        ------------------
        
        Initial release as an independent package
        
Platform: UNKNOWN
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: Zope Public License
Classifier: Programming Language :: Python
Classifier: Operating System :: OS Independent
Classifier: Topic :: Internet :: WWW/HTTP
Classifier: Topic :: Software Development