From 858189e4ff7dec5adc0ebef5ff31e971592f3f45 Mon Sep 17 00:00:00 2001 From: Tom Kooij Date: Mon, 1 Jan 2018 09:37:36 +0100 Subject: [PATCH 1/2] _Cache is a minimal subclass of dict() Create an object that can be added to a list by reference, but preserve dict() functionality. --- checkpy/caches.py | 27 ++++++--------------------- 1 file changed, 6 insertions(+), 21 deletions(-) diff --git a/checkpy/caches.py b/checkpy/caches.py index 635ea05..f3a010c 100644 --- a/checkpy/caches.py +++ b/checkpy/caches.py @@ -2,28 +2,13 @@ _caches = [] -class _Cache(object): - def __init__(self): - self._cache = {} - _caches.append(self) - - def __setitem__(self, key, value): - self._cache[key] = value - - def __getitem__(self, key): - return self._cache.get(key, None) - def __contains__(self, key): - return key in self._cache - - def delete(self, key): - if key not in self._cache: - return False - del self._cache[key] - return True +class _Cache(dict): + """A dict() subclass that appends a self-reference to _caches""" + def __init__(self, *args, **kwargs): + super(_Cache, self).__init__(*args, **kwargs) + _caches.append(self) - def clear(self): - self._cache.clear() """ cache decorator @@ -48,7 +33,7 @@ def cachedFuncWrapper(*args, **kwargs): if key not in localCache: localCache[key] = func(*args, **kwargs) - + return localCache[key] return cachedFuncWrapper return cacheWrapper From f56dc575d003b5611a6b502db80dfd3a914f6c62 Mon Sep 17 00:00:00 2001 From: Tom Kooij Date: Mon, 1 Jan 2018 11:07:40 +0100 Subject: [PATCH 2/2] Refactor caches() decorator Preserve docstrings of cached functions Use str() to create hashable keys Update docstring / PEP8 --- checkpy/caches.py | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/checkpy/caches.py b/checkpy/caches.py index f3a010c..4ff35bc 100644 --- a/checkpy/caches.py +++ b/checkpy/caches.py @@ -1,4 +1,5 @@ import sys +from functools import wraps _caches = [] @@ -10,34 +11,32 @@ def __init__(self, *args, **kwargs): _caches.append(self) -""" -cache decorator -Caches input and output of a function. If arguments are passed to -the decorator, take those as key for the cache, otherwise the -function arguments. -""" def cache(*keys): - def cacheWrapper(func, localCache = _Cache()): + """cache decorator + + Caches input and output of a function. If arguments are passed to + the decorator, take those as key for the cache. Otherwise use the + function arguments and sys.argv as key. + + """ + def cacheWrapper(func): + localCache = _Cache() + + @wraps(func) def cachedFuncWrapper(*args, **kwargs): if keys: - key = keys + key = str(keys) else: - # treat all collections in kwargs as tuples for hashing purposes - values = list(kwargs.values()) - for i in range(len(values)): - try: - values[i] = tuple(values[i]) - except TypeError: - pass - key = args + tuple(values) + tuple(sys.argv) + key = str(args) + str(kwargs) + str(sys.argv) if key not in localCache: localCache[key] = func(*args, **kwargs) - return localCache[key] return cachedFuncWrapper + return cacheWrapper + def clearAllCaches(): for cache in _caches: cache.clear()