Index: sitescripts/utils.py |
=================================================================== |
--- a/sitescripts/utils.py |
+++ b/sitescripts/utils.py |
@@ -11,6 +11,22 @@ |
siteScriptsPath = sitescripts.__path__[0] |
+class memoize: |
+ """ |
+ Decorator that caches a function with arguments return value. |
+ """ |
Wladimir Palant
2012/10/22 13:32:52
Any reason to introduce a new decorator instead of
Andrey Novikov
2012/10/23 14:15:29
I thought you were not considering them on purpose
|
+ def __init__ (self, f): |
+ self.f = f |
+ self.mem = {} |
+ |
+ def __call__ (self, *args, **kwargs): |
+ if (args, str(kwargs)) in self.mem: |
Wladimir Palant
2012/10/22 13:32:52
How about calculating str(kwargs) once at the begi
Andrey Novikov
2012/10/23 14:15:29
Done.
|
+ return self.mem[args, str(kwargs)] |
+ else: |
+ tmp = self.f(*args, **kwargs) |
+ self.mem[args, str(kwargs)] = tmp |
+ return tmp |
+ |
class cached(object): |
""" |
Decorator that caches a function's return value for a given number of seconds. |