aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cachetools/__init__.py10
-rw-r--r--cachetools/cache.py2
-rw-r--r--cachetools/func.py6
-rw-r--r--cachetools/keys.py4
-rw-r--r--cachetools/lfu.py2
-rw-r--r--cachetools/lru.py2
-rw-r--r--cachetools/rr.py2
-rw-r--r--cachetools/ttl.py2
-rw-r--r--docs/index.rst20
-rw-r--r--tests/test_keys.py8
-rw-r--r--tests/test_method.py4
-rw-r--r--tests/test_wrapper.py40
12 files changed, 58 insertions, 44 deletions
diff --git a/cachetools/__init__.py b/cachetools/__init__.py
index ed9586b..4144e48 100644
--- a/cachetools/__init__.py
+++ b/cachetools/__init__.py
@@ -1,9 +1,11 @@
"""Extensible memoizing collections and decorators."""
+from __future__ import absolute_import
+
import functools
+from . import keys
from .cache import Cache
-from .keys import hashkey, typedkey
from .lfu import LFUCache
from .lru import LRUCache
from .rr import RRCache
@@ -11,7 +13,7 @@ from .ttl import TTLCache
__all__ = (
'Cache', 'LFUCache', 'LRUCache', 'RRCache', 'TTLCache',
- 'cached', 'cachedmethod', 'hashkey', 'typedkey'
+ 'cached', 'cachedmethod'
)
__version__ = '1.1.6'
@@ -27,7 +29,7 @@ else:
return wrapper
-def cached(cache, key=hashkey, lock=None):
+def cached(cache, key=keys.hashkey, lock=None):
"""Decorator to wrap a function with a memoizing callable that saves
results in a cache.
@@ -68,7 +70,7 @@ def cached(cache, key=hashkey, lock=None):
return decorator
-def cachedmethod(cache, key=hashkey, lock=None):
+def cachedmethod(cache, key=keys.hashkey, lock=None):
"""Decorator to wrap a class or instance method with a memoizing
callable that saves results in a cache.
diff --git a/cachetools/cache.py b/cachetools/cache.py
index 15f526e..0852631 100644
--- a/cachetools/cache.py
+++ b/cachetools/cache.py
@@ -1,3 +1,5 @@
+from __future__ import absolute_import
+
from .abc import DefaultMapping
diff --git a/cachetools/func.py b/cachetools/func.py
index ce32f59..5a2ce84 100644
--- a/cachetools/func.py
+++ b/cachetools/func.py
@@ -1,5 +1,7 @@
"""`functools.lru_cache` compatible memoizing function decorators."""
+from __future__ import absolute_import
+
import collections
import functools
import random
@@ -10,7 +12,7 @@ try:
except ImportError:
from dummy_threading import RLock
-from .keys import hashkey, typedkey
+from . import keys
from .lfu import LFUCache
from .lru import LRUCache
from .rr import RRCache
@@ -26,7 +28,7 @@ _CacheInfo = collections.namedtuple('CacheInfo', [
def _cache(cache, typed=False):
def decorator(func):
- key = typedkey if typed else hashkey
+ key = keys.typedkey if typed else keys.hashkey
lock = RLock()
stats = [0, 0]
diff --git a/cachetools/keys.py b/cachetools/keys.py
index 887fb30..ba1e2fc 100644
--- a/cachetools/keys.py
+++ b/cachetools/keys.py
@@ -1,3 +1,7 @@
+"""Key functions for memoizing decorators."""
+
+from __future__ import absolute_import
+
__all__ = ('hashkey', 'typedkey')
diff --git a/cachetools/lfu.py b/cachetools/lfu.py
index 160f537..f5817a4 100644
--- a/cachetools/lfu.py
+++ b/cachetools/lfu.py
@@ -1,3 +1,5 @@
+from __future__ import absolute_import
+
import collections
from .cache import Cache
diff --git a/cachetools/lru.py b/cachetools/lru.py
index 525abd8..b945797 100644
--- a/cachetools/lru.py
+++ b/cachetools/lru.py
@@ -1,3 +1,5 @@
+from __future__ import absolute_import
+
import collections
from .cache import Cache
diff --git a/cachetools/rr.py b/cachetools/rr.py
index c82919e..8cd856c 100644
--- a/cachetools/rr.py
+++ b/cachetools/rr.py
@@ -1,3 +1,5 @@
+from __future__ import absolute_import
+
import random
from .cache import Cache
diff --git a/cachetools/ttl.py b/cachetools/ttl.py
index 04e9d85..d20cc0b 100644
--- a/cachetools/ttl.py
+++ b/cachetools/ttl.py
@@ -1,3 +1,5 @@
+from __future__ import absolute_import
+
import collections
import time
diff --git a/docs/index.rst b/docs/index.rst
index bc850e0..23c4dae 100644
--- a/docs/index.rst
+++ b/docs/index.rst
@@ -135,7 +135,7 @@ of one argument used to retrieve the size of an item's value.
expired by the current value returned by :attr:`timer`.
-Decorators
+Memoizing decorators
------------------------------------------------------------------------
The :mod:`cachetools` module provides decorators for memoizing
@@ -151,7 +151,7 @@ often called with the same arguments::
for i in range(100):
print('fib(%d) = %d' % (i, fib(i)))
-.. decorator:: cached(cache, key=hashkey, lock=None)
+.. decorator:: cached(cache, key=cachetools.keys.hashkey, lock=None)
Decorator to wrap a function with a memoizing callable that saves
results in a cache.
@@ -167,7 +167,7 @@ often called with the same arguments::
positional and keyword arguments as the wrapped function itself,
and which has to return a suitable cache key. Since caches are
mappings, the object returned by `key` must be hashable. The
- default is to call :func:`hashkey`.
+ default is to call :func:`cachetools.keys.hashkey`.
If `lock` is not :const:`None`, it must specify an object
implementing the `context manager`_ protocol. Any access to the
@@ -227,9 +227,7 @@ often called with the same arguments::
print(fac(42))
print(cache)
- .. versionadded:: 1.1
-
-.. decorator:: cachedmethod(cache, key=hashkey, lock=None)
+.. decorator:: cachedmethod(cache, key=cachetools.keys.hashkey, lock=None)
Decorator to wrap a class or instance method with a memoizing
callable that saves results in a (possibly shared) cache.
@@ -272,11 +270,13 @@ often called with the same arguments::
print("PEP #1: %s" % peps.get(1))
-Key functions
-------------------------------------------------------------------------
+:mod:`cachetools.keys` --- Key functions for memoizing decorators
+============================================================================
+
+.. module:: cachetools.keys
-The following functions can be used as key functions with the
-:func:`cached` and :func:`cachedmethod` decorators:
+This module provides several functions that can be used as key
+functions with the :func:`cached` and :func:`cachedmethod` decorators:
.. autofunction:: hashkey
diff --git a/tests/test_keys.py b/tests/test_keys.py
index 94184a5..2b9ced6 100644
--- a/tests/test_keys.py
+++ b/tests/test_keys.py
@@ -1,11 +1,11 @@
import unittest
-import cachetools
+import cachetools.keys
class CacheKeysTest(unittest.TestCase):
- def test_hashkey(self, key=cachetools.hashkey):
+ def test_hashkey(self, key=cachetools.keys.hashkey):
self.assertEqual(key(), key())
self.assertEqual(hash(key()), hash(key()))
self.assertEqual(key(1, 2, 3), key(1, 2, 3))
@@ -22,7 +22,7 @@ class CacheKeysTest(unittest.TestCase):
self.assertEqual(key(1, 2, 3), key(1.0, 2.0, 3.0))
self.assertEqual(hash(key(1, 2, 3)), hash(key(1.0, 2.0, 3.0)))
- def test_typedkey(self, key=cachetools.typedkey):
+ def test_typedkey(self, key=cachetools.keys.typedkey):
self.assertEqual(key(), key())
self.assertEqual(hash(key()), hash(key()))
self.assertEqual(key(1, 2, 3), key(1, 2, 3))
@@ -38,7 +38,7 @@ class CacheKeysTest(unittest.TestCase):
# typed keys compare unequal
self.assertNotEqual(key(1, 2, 3), key(1.0, 2.0, 3.0))
- def test_addkeys(self, key=cachetools.hashkey):
+ def test_addkeys(self, key=cachetools.keys.hashkey):
self.assertIsInstance(key(), tuple)
self.assertIsInstance(key(1, 2, 3) + key(4, 5, 6), type(key()))
self.assertIsInstance(key(1, 2, 3) + (4, 5, 6), type(key()))
diff --git a/tests/test_method.py b/tests/test_method.py
index cf53536..db810b6 100644
--- a/tests/test_method.py
+++ b/tests/test_method.py
@@ -1,7 +1,7 @@
import operator
import unittest
-from cachetools import LRUCache, cachedmethod, typedkey
+from cachetools import LRUCache, cachedmethod, keys
class Cached(object):
@@ -16,7 +16,7 @@ class Cached(object):
self.count += 1
return count
- @cachedmethod(operator.attrgetter('cache'), key=typedkey)
+ @cachedmethod(operator.attrgetter('cache'), key=keys.typedkey)
def get_typed(self, value):
count = self.count
self.count += 1
diff --git a/tests/test_wrapper.py b/tests/test_wrapper.py
index 1d03fb2..a6e649c 100644
--- a/tests/test_wrapper.py
+++ b/tests/test_wrapper.py
@@ -1,6 +1,7 @@
import unittest
import cachetools
+import cachetools.keys
class DecoratorTestMixin(object):
@@ -24,15 +25,15 @@ class DecoratorTestMixin(object):
self.assertEqual(wrapper(0), 0)
self.assertEqual(len(cache), 1)
- self.assertIn(cachetools.hashkey(0), cache)
- self.assertNotIn(cachetools.hashkey(1), cache)
- self.assertNotIn(cachetools.hashkey(1.0), cache)
+ self.assertIn(cachetools.keys.hashkey(0), cache)
+ self.assertNotIn(cachetools.keys.hashkey(1), cache)
+ self.assertNotIn(cachetools.keys.hashkey(1.0), cache)
self.assertEqual(wrapper(1), 1)
self.assertEqual(len(cache), 2)
- self.assertIn(cachetools.hashkey(0), cache)
- self.assertIn(cachetools.hashkey(1), cache)
- self.assertIn(cachetools.hashkey(1.0), cache)
+ self.assertIn(cachetools.keys.hashkey(0), cache)
+ self.assertIn(cachetools.keys.hashkey(1), cache)
+ self.assertIn(cachetools.keys.hashkey(1.0), cache)
self.assertEqual(wrapper(1), 1)
self.assertEqual(len(cache), 2)
@@ -45,37 +46,32 @@ class DecoratorTestMixin(object):
def test_decorator_typed(self):
cache = self.cache(3)
-
- def typedkey(*args, **kwargs):
- key = cachetools.hashkey(*args, **kwargs)
- key += tuple(type(v) for v in args)
- key += tuple(type(v) for _, v in sorted(kwargs.items()))
- return key
- wrapper = cachetools.cached(cache, key=typedkey)(self.func)
+ key = cachetools.keys.typedkey
+ wrapper = cachetools.cached(cache, key=key)(self.func)
self.assertEqual(len(cache), 0)
self.assertEqual(wrapper.__wrapped__, self.func)
self.assertEqual(wrapper(0), 0)
self.assertEqual(len(cache), 1)
- self.assertIn(typedkey(0), cache)
- self.assertNotIn(typedkey(1), cache)
- self.assertNotIn(typedkey(1.0), cache)
+ self.assertIn(cachetools.keys.typedkey(0), cache)
+ self.assertNotIn(cachetools.keys.typedkey(1), cache)
+ self.assertNotIn(cachetools.keys.typedkey(1.0), cache)
self.assertEqual(wrapper(1), 1)
self.assertEqual(len(cache), 2)
- self.assertIn(typedkey(0), cache)
- self.assertIn(typedkey(1), cache)
- self.assertNotIn(typedkey(1.0), cache)
+ self.assertIn(cachetools.keys.typedkey(0), cache)
+ self.assertIn(cachetools.keys.typedkey(1), cache)
+ self.assertNotIn(cachetools.keys.typedkey(1.0), cache)
self.assertEqual(wrapper(1), 1)
self.assertEqual(len(cache), 2)
self.assertEqual(wrapper(1.0), 2)
self.assertEqual(len(cache), 3)
- self.assertIn(typedkey(0), cache)
- self.assertIn(typedkey(1), cache)
- self.assertIn(typedkey(1.0), cache)
+ self.assertIn(cachetools.keys.typedkey(0), cache)
+ self.assertIn(cachetools.keys.typedkey(1), cache)
+ self.assertIn(cachetools.keys.typedkey(1.0), cache)
self.assertEqual(wrapper(1.0), 2)
self.assertEqual(len(cache), 3)