diff options
author | Thomas Kemmer <tkemmer@computer.org> | 2018-11-04 16:11:55 +0100 |
---|---|---|
committer | Thomas Kemmer <tkemmer@computer.org> | 2018-11-04 18:30:07 +0100 |
commit | eac1c25ec0f2501e8fd93f31b50d01ed93df3c2c (patch) | |
tree | 4938dc537fb3480a159ac626407e855e813f4cbf | |
parent | aa21f86b12c5cc7886c3a0e4d57c30de5352dd0e (diff) | |
download | cachetools-eac1c25ec0f2501e8fd93f31b50d01ed93df3c2c.tar.gz |
Fix #54: Update README examples.
-rw-r--r-- | README.rst | 35 |
1 files changed, 20 insertions, 15 deletions
@@ -5,21 +5,26 @@ This module provides various memoizing collections and decorators, including variants of the Python 3 Standard Library `@lru_cache`_ function decorator. -.. code-block:: pycon - - >>> from cachetools import LRUCache - >>> cache = LRUCache(maxsize=2) - >>> cache.update([('first', 1), ('second', 2)]) - >>> cache - LRUCache([('second', 2), ('first', 1)], maxsize=2, currsize=2) - >>> cache['third'] = 3 - >>> cache - LRUCache([('second', 2), ('third', 3)], maxsize=2, currsize=2) - >>> cache['second'] - 2 - >>> cache['fourth'] = 4 - >>> cache - LRUCache([('second', 2), ('fourth', 4)], maxsize=2, currsize=2) +.. code-block:: python + + from cachetools import cached, LRUCache, TTLCache + + # speed up calculating Fibonacci numbers with dynamic programming + @cached(cache={}) + def fib(n): + return n if n < 2 else fib(n - 1) + fib(n - 2) + + # cache least recently used Python Enhancement Proposals + @cached(cache=LRUCache(maxsize=32)) + def get_pep(num): + url = 'http://www.python.org/dev/peps/pep-%04d/' % num + with urllib.request.urlopen(url) as s: + return s.read() + + # cache weather data for no longer than ten minutes + @cached(cache=TTLCache(maxsize=1024, ttl=600)) + def get_weather(place): + return owm.weather_at_place(place).get_weather() For the purpose of this module, a *cache* is a mutable_ mapping_ of a fixed maximum size. When the cache is full, i.e. by adding another |