Created
January 27, 2014 09:11
-
-
Save TheWaWaR/8645401 to your computer and use it in GitHub Desktop.
Memoize decorator function with cache size limit (Python recipe)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cPickle | |
__all__ = ["memoize"] | |
def memoize(function, limit=None): | |
if isinstance(function, int): | |
def memoize_wrapper(f): | |
return memoize(f, function) | |
return memoize_wrapper | |
dict = {} | |
list = [] | |
def memoize_wrapper(*args, **kwargs): | |
key = cPickle.dumps((args, kwargs)) | |
try: | |
list.append(list.pop(list.index(key))) | |
except ValueError: | |
dict[key] = function(*args, **kwargs) | |
list.append(key) | |
if limit is not None and len(list) > limit: | |
del dict[list.pop(0)] | |
return dict[key] | |
memoize_wrapper._memoize_dict = dict | |
memoize_wrapper._memoize_list = list | |
memoize_wrapper._memoize_limit = limit | |
memoize_wrapper._memoize_origfunc = function | |
memoize_wrapper.func_name = function.func_name | |
return memoize_wrapper | |
# Example usage | |
if __name__ == "__main__": | |
# Will cache up to 100 items, dropping the least recently used if | |
# the limit is exceeded. | |
@memoize(100) | |
def fibo(n): | |
if n > 1: | |
return fibo(n - 1) + fibo(n - 2) | |
else: | |
return n | |
# Same as above, but with no limit on cache size | |
@memoize | |
def fibonl(n): | |
if n > 1: | |
return fibo(n - 1) + fibo(n - 2) | |
else: | |
return n |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment