/usr/share/pyshared/zope/cachedescriptors/method.txt is in python-zope.cachedescriptors 3.5.1-2fakesync1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | 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
|