Latest recipes tagged "decorators" but not "decorator"http://code.activestate.com/recipes/tags/decorators-decorator/new/2014-01-14T11:23:40-08:00ActiveState Code RecipesDecorator to check method param types (Python)
2014-01-14T11:23:40-08:00Andrey Nikishaevhttp://code.activestate.com/recipes/users/4176176/http://code.activestate.com/recipes/578809-decorator-to-check-method-param-types/
<p style="color: grey">
Python
recipe 578809
by <a href="/recipes/users/4176176/">Andrey Nikishaev</a>
(<a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/method/">method</a>, <a href="/recipes/tags/typecheck/">typecheck</a>).
</p>
<p>This solution give possibility to check method param type, raise needed exception type, and also have good readability in the decorator definition.</p>
type Checking in Python using decorators (version 2.0) (Python)
2012-11-19T13:15:35-08:00LL Snarkhttp://code.activestate.com/recipes/users/4180463/http://code.activestate.com/recipes/578330-type-checking-in-python-using-decorators-version-2/
<p style="color: grey">
Python
recipe 578330
by <a href="/recipes/users/4180463/">LL Snark</a>
(<a href="/recipes/tags/checking/">checking</a>, <a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/type/">type</a>).
Revision 2.
</p>
<p>The two decorators checkparams and checkreturn allow you to check (at execution time) that function parameters or a function return value are the right type.</p>
General decorators decorator (Python)
2012-11-13T09:44:07-08:00Hans Zauberhttp://code.activestate.com/recipes/users/4184246/http://code.activestate.com/recipes/578325-general-decorators-decorator/
<p style="color: grey">
Python
recipe 578325
by <a href="/recipes/users/4184246/">Hans Zauber</a>
(<a href="/recipes/tags/decorators/">decorators</a>).
</p>
<p>A general decorator aimed to decorate decorators, letting them keep (some of) the original attributes of the functions they decorate.</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>
Cached 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>
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>
Mixins by Inheritance vs. by Decorator...Let's Try Decorators (Python)
2011-08-12T15:29:21-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577824-mixins-by-inheritance-vs-by-decoratorlets-try-deco/
<p style="color: grey">
Python
recipe 577824
by <a href="/recipes/users/4177816/">Eric Snow</a>
(<a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/mixins/">mixins</a>).
</p>
<p>Using mixin classes via inheritance has its pros and cons. Here is an easy alternative via a decorator. As a bonus, you can mix in attributes from any object, not just classes.</p>
Turn @some_decorator() into @some_decorator (Python)
2011-08-04T18:48:36-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577820-turn-some_decorator-into-some_decorator/
<p style="color: grey">
Python
recipe 577820
by <a href="/recipes/users/4177816/">Eric Snow</a>
(<a href="/recipes/tags/decorators/">decorators</a>).
</p>
<p>A decorator factory is a function that returns a decorator based on the arguments you pass in. Sometimes you make a decorator factory to cover uncommon use cases. In that case you'll probably use default arguments to cover your common case.</p>
<p>The problem with this, however, is that you still have to use the call syntax (with no arguments) to get the common-use-case decorator, as demonstrated in the recipe title. This recipe effectively makes it so that your decorator factory and your common-use-case decorator have the same name (and actually the same object).</p>
Apply decorators to all functions in a module (Python)
2011-06-09T22:51:28-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577742-apply-decorators-to-all-functions-in-a-module/
<p style="color: grey">
Python
recipe 577742
by <a href="/recipes/users/4177816/">Eric Snow</a>
(<a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/import/">import</a>, <a href="/recipes/tags/modules/">modules</a>).
Revision 2.
</p>
<p>Use my modulehacker recipe (<a href="http://code.activestate.com/recipes/577740/">recipe 577740</a>) to apply decorators to any number of modules, or even (nearly) all of them.</p>
Class decorator to check that methods are implemented. (Python)
2011-05-27T23:52:28-07:00Jeffrey Fischerhttp://code.activestate.com/recipes/users/4178137/http://code.activestate.com/recipes/577725-class-decorator-to-check-that-methods-are-implemen/
<p style="color: grey">
Python
recipe 577725
by <a href="/recipes/users/4178137/">Jeffrey Fischer</a>
(<a href="/recipes/tags/decorators/">decorators</a>).
Revision 2.
</p>
<p>This decorator wraps the class's __init__ function to check that provided set of methods is present on the instantiated class. This is useful for avoiding inheritance in mix-in classes. We can inherit directly from object and still make it clear what methods we expect in other classes to be combined with the mix-in.</p>
<p>Requires at least Python 2.6, which added class decorators.</p>
Trace decorator for debugging (Python)
2011-01-24T18:40:51-08:00Kevin L. Sitzehttp://code.activestate.com/recipes/users/4173535/http://code.activestate.com/recipes/577551-trace-decorator-for-debugging/
<p style="color: grey">
Python
recipe 577551
by <a href="/recipes/users/4173535/">Kevin L. Sitze</a>
(<a href="/recipes/tags/classes/">classes</a>, <a href="/recipes/tags/debug/">debug</a>, <a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/functions/">functions</a>, <a href="/recipes/tags/inspection/">inspection</a>, <a href="/recipes/tags/modules/">modules</a>, <a href="/recipes/tags/trace/">trace</a>).
Revision 2.
</p>
<p>This package provides a decorator for tracing function and method calls in your applications. The tracing capabilities are managed through the logging package, and several mechanisms are provided for controlling the destination of the trace output.</p>
<p>It also provides functionality for adding decorators to existing classes or modules.</p>
Counting decorator (Python)
2011-01-07T11:22:55-08:00Noufal Ibrahimhttp://code.activestate.com/recipes/users/4173873/http://code.activestate.com/recipes/577534-counting-decorator/
<p style="color: grey">
Python
recipe 577534
by <a href="/recipes/users/4173873/">Noufal Ibrahim</a>
(<a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/testing/">testing</a>).
</p>
<p>To be used as a decorator for a function that will maintain the number of times it was called.
Here is an example use. </p>
<pre class="prettyprint"><code>>>> def test():
... print "Hello"
...
>>> test = counter(test)
>>> test()
Hello
>>> test()
Hello
>>> test()
Hello
>>> test()
Hello
>>> test()
Hello
>>> test.invocations
5
>>> test()
Hello
>>> test.invocations
6
>>>
</code></pre>
"public" decorator, adds an item to __all__ (Python)
2009-12-30T12:53:29-08:00Sam Dentonhttp://code.activestate.com/recipes/users/4172262/http://code.activestate.com/recipes/576993-public-decorator-adds-an-item-to-__all__/
<p style="color: grey">
Python
recipe 576993
by <a href="/recipes/users/4172262/">Sam Denton</a>
(<a href="/recipes/tags/decorators/">decorators</a>).
</p>
<p>The DRY principle says, "Don't repeat yourself". A Python module typically defines a global variable named "__all__" that holds the names of everything the author wants visible. This means you have to type each function or class name a second time, and you have to maintain the contents of __all__ manually. This fixes that.</p>
Recursivemethod (Python)
2009-07-10T07:01:25-07:00bazookahttp://code.activestate.com/recipes/users/4170765/http://code.activestate.com/recipes/576838-recursivemethod/
<p style="color: grey">
Python
recipe 576838
by <a href="/recipes/users/4170765/">bazooka</a>
(<a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/staticmethod/">staticmethod</a>).
</p>
<p>Make staticmethod call itself without calling self.method</p>
Easy State Pattern - support for implementing state machines (Python)
2009-06-15T11:25:17-07:00Rodney Drenthhttp://code.activestate.com/recipes/users/4050661/http://code.activestate.com/recipes/576613-easy-state-pattern-support-for-implementing-state-/
<p style="color: grey">
Python
recipe 576613
by <a href="/recipes/users/4050661/">Rodney Drenth</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/metaclasses/">metaclasses</a>, <a href="/recipes/tags/oop/">oop</a>, <a href="/recipes/tags/state_machine/">state_machine</a>, <a href="/recipes/tags/state_pattern/">state_pattern</a>).
Revision 3.
</p>
<p>Provides is a module that gives support for implementing state machines. States are implemented as subclasses, derived from the state machine class. Methods that are state dependant or which cause transitions are declared using decorators. Because states can be subclasses of other states, common behaviour among several states is easily supported. The implementation allows for implementing multiple states or substates within a class.</p>
<p>This module best support statem achines implemeting controllers for embedded systems, implementing user interfaces, or in discrete event model simulation.
Parsers, which generally have many states and where you would need to define
a Transaction method for each different character encountered would be more easily implemented by other means.</p>
Adaptive Replacement Cache in python (Python)
2008-10-08T12:24:40-07:00eric casteleijnhttp://code.activestate.com/recipes/users/4167234/http://code.activestate.com/recipes/576532-adaptive-replacement-cache-in-python/
<p style="color: grey">
Python
recipe 576532
by <a href="/recipes/users/4167234/">eric casteleijn</a>
(<a href="/recipes/tags/caching/">caching</a>, <a href="/recipes/tags/decorators/">decorators</a>).
</p>
<p>Python implementation of an adaptive replacement cache, as described in:</p>
<p><a href="http://en.wikipedia.org/wiki/Adaptive_Replacement_Cache" rel="nofollow">http://en.wikipedia.org/wiki/Adaptive_Replacement_Cache</a></p>
<p>Caveat: This may or may not be under patent by IBM, beware of putting it in production code. I implemented it as an experiment, and since I'm in Europe I do not believe a software patent can apply to anything I write.</p>