From aa546823dffa0fcaabdbcf32318d484383a99763 Mon Sep 17 00:00:00 2001 From: Jay Hutchinson Date: Tue, 24 Aug 2010 18:14:48 -0500 Subject: Added a function decorator that would allow memorization of function values. --- lru.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lru.py b/lru.py index 7b2fd99..ec257c3 100644 --- a/lru.py +++ b/lru.py @@ -289,3 +289,20 @@ class lruwrap(object): except KeyError: pass del self.store[key] + + +class lrudecorator(object): + def __init__(self, func, size): + self.func = func + self.cache = lrucache(size) + + def __call__(self, *args, **kwargs): + try: + value = self.cache[(args, kwargs)] + except KeyError: + pass + + value = self.func(*args, **kwargs) + self.cache[(args, kwargs)] = value + return value + -- cgit v1.2.1