Popular recipes by Peter Donis http://code.activestate.com/recipes/users/4180313/2012-01-06T02:24:08-08:00ActiveState Code RecipesCached Class (Python) 2012-01-06T02:24:08-08:00Peter Donishttp://code.activestate.com/recipes/users/4180313/http://code.activestate.com/recipes/577998-cached-class/ <p style="color: grey"> Python recipe 577998 by <a href="/recipes/users/4180313/">Peter Donis</a> (<a href="/recipes/tags/cache/">cache</a>, <a href="/recipes/tags/decorators/">decorators</a>). Revision 5. </p> <p>A class decorator that ensures that only one instance of the class exists for each distinct set of constructor arguments.</p> <p>Note that if a decorated class is subclassed, each subclass is cached separately. (This is because each cached subclass is a different <code>cls</code> argument to the <code>__new__</code> method.)</p> Indexable Generator (Python) 2012-01-03T06:10:18-08:00Peter Donishttp://code.activestate.com/recipes/users/4180313/http://code.activestate.com/recipes/578000-indexable-generator/ <p style="color: grey"> Python recipe 578000 by <a href="/recipes/users/4180313/">Peter Donis</a> (<a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/generators/">generators</a>). Revision 4. </p> <p>A decorator for generators that makes them indexable like a sequence.</p> Delayed Decorator (Python) 2011-12-20T06:37:15-08:00Peter Donishttp://code.activestate.com/recipes/users/4180313/http://code.activestate.com/recipes/577993-delayed-decorator/ <p style="color: grey"> Python recipe 577993 by <a href="/recipes/users/4180313/">Peter Donis</a> (<a href="/recipes/tags/decorators/">decorators</a>). </p> <p>This is a decorator wrapper class to delay decorating the base function until it is actually invoked. This can be useful when decorating ordinary functions if some decorator parameters depend on data that is only known at function invocation. It can also be used (and was written) to ensure that a decorated method of a class gets decorated once per instance instead of once per class; the use case that prompted this was the need to memoize a generator (see the <a href="http://code.activestate.com/recipes/577992-memoize-generator/">Memoized Generator</a> recipe), but the implementation is general.</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>