summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJay Hutchinson <jlhutch@gmail.com>2010-08-24 18:14:48 -0500
committerJay Hutchinson <jlhutch@gmail.com>2010-08-24 18:14:48 -0500
commitaa546823dffa0fcaabdbcf32318d484383a99763 (patch)
tree1dca4516da8d0a71577cc88b8ef572bba63b8fcf
parentbec2a29890e5aba3f17ad423f1fea421555bd0e7 (diff)
downloadpylru-aa546823dffa0fcaabdbcf32318d484383a99763.tar.gz
Added a function decorator that would allow memorization of function values.
-rw-r--r--lru.py17
1 files changed, 17 insertions, 0 deletions
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
+