Skip to content

Commit 682eb0d

Browse files
committed
-
1 parent 0cee7fa commit 682eb0d

File tree

4 files changed

+26
-17
lines changed

4 files changed

+26
-17
lines changed

docs/index.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@ Contents:
1515
.. toctree::
1616
:maxdepth: 2
1717

18-
intro/index
1918
topics/index
20-
ref/index
2119
misc/index
2220

21+
..
22+
ref/index
23+
2324
This documentation is still incomplete. If you have any questions or feedback, say hello
2425
on the `mailing list`_!
2526

2627
.. _mailing list: https://groups.google.com/forum/#!forum/python-toolbox
2728

2829
.. * :ref:`genindex`
29-
.. * :ref:`modindex`
30+
.. * :ref:`modindex`

docs/topics/caching/cache.txt

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
"Ram Rachum at ram.rachum.com" including link. The license may be obtained
55
at http://creativecommons.org/licenses/by-sa/3.0/
66

7-
.. _topics-index:
7+
.. _topics-caching-cache:
88

9-
`caching.cache`
10-
===============
9+
:func:`caching.cache`
10+
====================
1111

1212
A caching decorator that understands arguments
1313
----------------------------------------------
@@ -16,10 +16,14 @@ The idea of a caching decorator is very cool. You decorate your function with a
1616
caching decorator:
1717

1818
>>> from python_toolbox import caching
19+
>>>
1920
>>> @caching.cache
2021
... def f(x):
2122
... print('Calculating...')
2223
... return x ** x # Some long expensive computation
24+
25+
And then, every time you call it, it'll cache the results for next time:
26+
2327
>>> f(4)
2428
Calculating...
2529
256
@@ -29,21 +33,21 @@ caching decorator:
2933
>>> f(5)
3034
3125
3135
>>> f(5)
32-
3125<
36+
3125
3337

3438
As you can see, after the first time we calculate ``f(5)`` the result gets
3539
saved to a cache and every time we'll call ``f(5)`` Python will return the
3640
result from the cache instead of calculating it again. This prevents making
3741
redundant performance-expensive calculations.
3842

39-
Now, depending on the function, there can be many different ways to make the same call. Example, if you have a function defined like this:
43+
Now, depending on the function, there can be many different ways to make the same call. For example, if you have a function defined like this::
4044

4145
def g(a, b=2, **kwargs):
4246
return whatever
4347

4448
Then ``g(1)``, ``g(1, 2)``, ``g(b=2, a=1)`` and even ``g(1, 2, **{})`` are all equivalent. They give the exact same arguments, just in different ways. Most caching decorators out there don't understand that. If you call ``g(1)`` and then ``g(1, 2)``, they will calculate the function again, because they don't understand that it's exactly the same call and they could use the cached result.
4549

46-
Enter `caching.cache`:
50+
Enter :func:`caching.cache`:
4751

4852
>>> @caching.cache()
4953
... def g(a, b=2, **kwargs):
@@ -63,15 +67,15 @@ Enter `caching.cache`:
6367
Calculating
6468
('something_else', 2, {})
6569

66-
As you can see above, `caching.cache` analyzes the function and understands
70+
As you can see above, :func:`caching.cache` analyzes the function and understands
6771
that calls like ``g(1)`` and ``g(1, 2)`` are identical and therefore should be
6872
cached together.
6973

7074

7175
Both limited and unlimited cache
7276
--------------------------------
7377

74-
By default, the cache size will be unlimited. If you want to limit the cache size, pass in the `max_size` argument:
78+
By default, the cache size will be unlimited. If you want to limit the cache size, pass in the ``max_size`` argument:
7579

7680
>>> @caching.cache(max_size=7)
7781
... def f(): pass
@@ -84,7 +88,7 @@ get thrown away according to a `LRU order`_.
8488
Sleekrefs
8589
----------
8690

87-
`caching.cache` arguments with sleekrefs. Sleekrefs are a more robust variation of `weakrefs`_. They are basically a gracefully-degrading version of weakrefs, so you can use them on un-weakreff-able objects like ``int``\s, and they will just use regular references.
91+
:func:`caching.cache` arguments with sleekrefs. Sleekrefs are a more robust variation of `weakrefs`_. They are basically a gracefully-degrading version of weakrefs, so you can use them on un-weakreff-able objects like :class:`int`\, and they will just use regular references.
8892

8993
The usage of sleekrefs prevents memory leaks when using potentially-heavy arguments.
9094

docs/topics/caching/index.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
"Ram Rachum at ram.rachum.com" including link. The license may be obtained
55
at http://creativecommons.org/licenses/by-sa/3.0/
66

7-
.. _topics-index:
7+
.. _topics-caching-index:
88

9-
Topical guides to `caching`
10-
====================================
9+
:mod:`caching`
10+
=============
1111

1212
.. toctree::
13-
:maxdepth: 1
13+
:maxdepth: 3
14+
15+
cache

docs/topics/index.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ explaining what it's good for and the basics of using it.
1818

1919

2020
.. toctree::
21-
:maxdepth: 1
21+
:maxdepth: 3
22+
23+
caching/index
2224

2325
..
2426
For a more complete and thorough documentation including more technical

0 commit comments

Comments
 (0)