summaryrefslogtreecommitdiff
path: root/sqlparse/utils.py
diff options
context:
space:
mode:
authorJesús Leganés Combarro "Piranna" <piranna@gmail.com>2012-05-17 22:34:54 +0200
committerJesús Leganés Combarro "Piranna" <piranna@gmail.com>2012-05-17 22:34:54 +0200
commit8b3e88e60d8773ef2432bdc511ed7eaf1dc09a14 (patch)
tree07aeee927435bc690930874d78d50f12a879ab01 /sqlparse/utils.py
parent0aba698a5275f9c437f3142af1c29b5aab387de0 (diff)
downloadsqlparse-8b3e88e60d8773ef2432bdc511ed7eaf1dc09a14.tar.gz
Added memoize_generator and use it on IncludeStatement
Diffstat (limited to 'sqlparse/utils.py')
-rw-r--r--sqlparse/utils.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/sqlparse/utils.py b/sqlparse/utils.py
new file mode 100644
index 0000000..0dbb09f
--- /dev/null
+++ b/sqlparse/utils.py
@@ -0,0 +1,41 @@
+'''
+Created on 17/05/2012
+
+@author: piranna
+'''
+
+
+def memoize_generator(func):
+ """
+ Memoize decorator for generators
+
+ Store `func` results in a cache according to their arguments as 'memoize'
+ does but instead this works on decorators instead of regular functions.
+ Obviusly, this is only useful if the generator will always return the same
+ values for each specific parameters...
+ """
+ cache = {}
+
+ def wrapped_func(*args, **kwargs):
+ params = (args, kwargs)
+
+ # Look if cached
+ try:
+ cached = cache[params]
+
+ # Not cached, exec and store it
+ except KeyError:
+ cached = []
+
+ for item in func(*args, **kwargs):
+ cached.append(item)
+ yield item
+
+ cache[params] = cached
+
+ # Cached, yield its items
+ else:
+ for item in cached:
+ yield item
+
+ return wrapped_func