Latest recipes tagged "memoization"http://code.activestate.com/recipes/tags/memoization/new/2012-08-02T07:27:12-07:00ActiveState Code RecipesProbably the fastest memoization decorator in the world (Python) 2012-08-02T07:27:12-07:00Oren Tiroshhttp://code.activestate.com/recipes/users/2033964/http://code.activestate.com/recipes/578231-probably-the-fastest-memoization-decorator-in-the-/ <p style="color: grey"> Python recipe 578231 by <a href="/recipes/users/2033964/">Oren Tirosh</a> (<a href="/recipes/tags/cache/">cache</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/memo/">memo</a>, <a href="/recipes/tags/memoization/">memoization</a>). </p> <p>With apologies to a certain European brand of beer, this is probably the fastest memoization decorator in the world. Implemented using the __missing__ method in a dict subclass. </p> Memoize Generator (Python) 2012-01-02T05:53:19-08:00Peter Donishttp://code.activestate.com/recipes/users/4180313/http://code.activestate.com/recipes/577992-memoize-generator/ <p style="color: grey"> Python recipe 577992 by <a href="/recipes/users/4180313/">Peter Donis</a> (<a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/generators/">generators</a>, <a href="/recipes/tags/memoization/">memoization</a>, <a href="/recipes/tags/memoize/">memoize</a>). Revision 2. </p> <p>A wrapper class for generators that "memoizes" them, so that even if the generator is realized multiple times, each term only gets computed once (after that the result is simply returned from a cache).</p> Simple local cache and cache decorator (Python) 2010-12-08T09:06:34-08:00Andrey Nikishaevhttp://code.activestate.com/recipes/users/4176176/http://code.activestate.com/recipes/577492-simple-local-cache-and-cache-decorator/ <p style="color: grey"> Python recipe 577492 by <a href="/recipes/users/4176176/">Andrey Nikishaev</a> (<a href="/recipes/tags/cache/">cache</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/memoization/">memoization</a>, <a href="/recipes/tags/python/">python</a>). </p> <p>Simple local cache. It saves local data in singleton dictionary with convenient interface</p> <h4 id="examples-of-use">Examples of use:</h4> <pre class="prettyprint"><code># Initialize SimpleCache({'data':{'example':'example data'}}) # Getting instance c = SimpleCache.getInstance() c.set('re.reg_exp_compiled',re.compile(r'\W*')) reg_exp = c.get('re.reg_exp_compiled',default=re.compile(r'\W*')) # -------------------------------------------------------------- c = SimpleCache.getInstance() reg_exp = c.getset('re.reg_exp_compiled',re.compile(r'\W*')) # -------------------------------------------------------------- @scache def func1(): return 'OK' </code></pre> Simple caching decorator (Python) 2010-12-01T00:43:01-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577479-simple-caching-decorator/ <p style="color: grey"> Python recipe 577479 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/cache/">cache</a>, <a href="/recipes/tags/memoization/">memoization</a>). </p> <p>Memoizing decorator. Has the same API as the functools.lru_cache() in Py3.2 but without the LRU feature, so it takes less memory, runs faster, and doesn't need locks to keep the dictionary in a consistent state.</p> Minimalistic Memoization (Python) 2010-05-06T16:59:52-07:00Ahmed El Deebhttp://code.activestate.com/recipes/users/4173897/http://code.activestate.com/recipes/577219-minimalistic-memoization/ <p style="color: grey"> Python recipe 577219 by <a href="/recipes/users/4173897/">Ahmed El Deeb</a> (<a href="/recipes/tags/caching/">caching</a>, <a href="/recipes/tags/memoization/">memoization</a>, <a href="/recipes/tags/recursion/">recursion</a>). </p> <p>Minimalistic Memoization in python, just works, doesn't take care of cleaning up the cache however.</p> Lazy Lists for python 2.5 (Python) 2008-08-18T19:59:03-07:00Michael Pusthttp://code.activestate.com/recipes/users/4166509/http://code.activestate.com/recipes/576429-lazy-lists-for-python-25/ <p style="color: grey"> Python recipe 576429 by <a href="/recipes/users/4166509/">Michael Pust</a> (<a href="/recipes/tags/generators/">generators</a>, <a href="/recipes/tags/lazy/">lazy</a>, <a href="/recipes/tags/memoization/">memoization</a>). </p> <p>Dan Spitz submitted a recipe ( <a href="http://code.activestate.com/recipes/576410/">576410</a> ) for recursively defined lazy lists backed by a generator. Only catch was that it was written for python 3k. But there is nothing intrinsic in 3k that prevents you from having the same functionality in python 2.5, so I am supplying the backport here.</p> Lazy Lists (Python) 2008-08-08T10:23:52-07:00Dan Spitzhttp://code.activestate.com/recipes/users/4166237/http://code.activestate.com/recipes/576410-lazy-lists/ <p style="color: grey"> Python recipe 576410 by <a href="/recipes/users/4166237/">Dan Spitz</a> (<a href="/recipes/tags/generators/">generators</a>, <a href="/recipes/tags/lazy/">lazy</a>, <a href="/recipes/tags/memoization/">memoization</a>). Revision 6. </p> <p>This is a (python 3.0) recipe for LazyLists, or ordered sequences whose contents are generated lazily by an iterator.</p>