Skip to content

Commit e9fe2aa

Browse files
committed
-
1 parent d44b0b6 commit e9fe2aa

File tree

4 files changed

+92
-2
lines changed

4 files changed

+92
-2
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
..
2+
Copyright 2009-2012 Ram Rachum. This work is licensed under a Creative
3+
Commons Attribution-ShareAlike 3.0 Unported License, with attribution to
4+
"Ram Rachum at ram.rachum.com" including link. The license may be obtained
5+
at http://creativecommons.org/licenses/by-sa/3.0/
6+
7+
.. _topics-caching-cached-property:
8+
9+
:class:`caching.CachedProperty`
10+
===============================
11+
12+
A cached property
13+
-----------------
14+
15+
Oftentimes you have a :class:`property` on a class that never gets changed and
16+
needs to be calculated only once. This is a good situation to use
17+
:class:`caching.CachedProperty` in order to have that property be calculated
18+
only one time per instance. Any future accesses to the property will use the
19+
cached value.
20+
21+
Example::
22+
23+
>>> import time
24+
>>> from python_toolbox import caching
25+
>>>
26+
>>> class MyObject(object):
27+
... # ... Regular definitions here
28+
... def _get_personality(self):
29+
... print('Calculating personality...')
30+
... time.sleep(5) # Time consuming process...
31+
... return 'Nice person'
32+
... personality = caching.CachedProperty(_get_personality)
33+
34+
35+
Now we create an object and calculate its "personality":
36+
37+
>>> my_object = MyObject()
38+
>>> my_object.personality
39+
'Nice person'
40+
>>> # We had to wait 5 seconds for the calculation!
41+
42+
Consecutive calls will be instantaneous:
43+
44+
>>> my_object.personality
45+
'Nice person'
46+
>>> # That one was cached and therefore instantaneous!
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
..
2+
Copyright 2009-2012 Ram Rachum. This work is licensed under a Creative
3+
Commons Attribution-ShareAlike 3.0 Unported License, with attribution to
4+
"Ram Rachum at ram.rachum.com" including link. The license may be obtained
5+
at http://creativecommons.org/licenses/by-sa/3.0/
6+
7+
.. _topics-caching-cached-type:
8+
9+
:class:`caching.CachedType`
10+
===========================
11+
12+
A class that automatically caches its instances
13+
-----------------------------------------------
14+
15+
Sometimes you define classes whose instances hold absolutely no state on them,
16+
and are completey determined by the arguments passed to them. In these cases
17+
using :class:`caching.CachedType` as a metaclass would cache class instances,
18+
preventing more than one of them from being created:
19+
20+
>>> from python_toolbox import caching
21+
>>>
22+
>>> class A(object):
23+
... __metaclass__ = caching.CachedType
24+
... def __init__(self, a=1, b=2):
25+
... self.a = a
26+
... self.b = b
27+
28+
Now every time you create an instance, it'll be cached:
29+
30+
>>> my_instance = A(b=3)
31+
32+
And the next time you'll create an instance with the same arguments:
33+
34+
>>> another_instance = A(b=3)
35+
36+
No instance will be actually created; the same instance from before will be used:
37+
38+
>>> assert another_instance is my_instance
39+

docs/topics/caching/index.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@
99
:mod:`caching`
1010
=============
1111

12+
The :mod:`caching` modules provides tools related to caching:
13+
1214
.. toctree::
1315
:maxdepth: 3
1416

15-
cache
17+
cache
18+
cached_type
19+
cached_property
20+

python_toolbox/caching/cached_property.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
class CachedProperty(misc_tools.OwnNameDiscoveringProperty):
1414
'''
15-
A property that is calculated (a) lazily and (b) only once for an object.
15+
A property that is calculated only once for an object, and then cached.
1616
1717
Usage:
1818

0 commit comments

Comments
 (0)