diff options
author | Jesús Leganés Combarro "Piranna" <piranna@gmail.com> | 2012-05-19 21:43:54 +0200 |
---|---|---|
committer | Jesús Leganés Combarro "Piranna" <piranna@gmail.com> | 2012-05-19 21:43:54 +0200 |
commit | 59bfe75aaec583fdb92668819f021ba18c4595e3 (patch) | |
tree | 07a58dce5da5649b7e1630fa5e0b35b5589f54a7 /sqlparse/utils.py | |
parent | 8bd03f158343bd2b83802c1059e15953c72f9f36 (diff) | |
parent | 66742da10ebdc2bc485022ecbd59278d3fc96488 (diff) | |
download | sqlparse-59bfe75aaec583fdb92668819f021ba18c4595e3.tar.gz |
Merge branch 'master' into antiorm
Conflicts:
sqlparse/filters.py
Diffstat (limited to 'sqlparse/utils.py')
-rw-r--r-- | sqlparse/utils.py | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/sqlparse/utils.py b/sqlparse/utils.py new file mode 100644 index 0000000..443c64d --- /dev/null +++ b/sqlparse/utils.py @@ -0,0 +1,47 @@ +''' +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: + print params + 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 |