summaryrefslogtreecommitdiff
path: root/sqlparse/utils.py
diff options
context:
space:
mode:
authorAndi Albrecht <albrecht.andi@googlemail.com>2012-05-19 12:18:04 -0700
committerAndi Albrecht <albrecht.andi@googlemail.com>2012-05-19 12:18:04 -0700
commit66742da10ebdc2bc485022ecbd59278d3fc96488 (patch)
treea4d93108dc0b74c92d202cd9dec751c9acbdac61 /sqlparse/utils.py
parent7442f145233db0d23e1a7d74cf20ce316b890f97 (diff)
parent7e532bcd9af6f36280f497346d50b4cdd028cfbf (diff)
downloadsqlparse-66742da10ebdc2bc485022ecbd59278d3fc96488.tar.gz
Merge pull request #68 from piranna/master
Optimizations on INCLUDE statement
Diffstat (limited to 'sqlparse/utils.py')
-rw-r--r--sqlparse/utils.py46
1 files changed, 46 insertions, 0 deletions
diff --git a/sqlparse/utils.py b/sqlparse/utils.py
new file mode 100644
index 0000000..fd6651a
--- /dev/null
+++ b/sqlparse/utils.py
@@ -0,0 +1,46 @@
+'''
+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:
+ # Reset the cache if we have too much cached entries and start over
+ # In the future would be better to use an OrderedDict and drop the
+ # Least Recent Used entries
+ if len(cache) >= 10:
+ cache.clear()
+
+ 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