Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: sitescripts/utils.py

Issue 8625042: Reports - user usefullness (Closed)
Patch Set: Reports - user usefulness Created Oct. 19, 2012, 12:54 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« sitescripts/reports/utils.py ('K') | « sitescripts/reports/web/updateReport.py ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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.
« sitescripts/reports/utils.py ('K') | « sitescripts/reports/web/updateReport.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld