Popular recipes tagged "decorator" and "decorators"http://code.activestate.com/recipes/tags/decorator+decorators/2015-07-10T15:25:04-07:00ActiveState Code RecipesDecorator for defining functions with keyword-only arguments (Python)
2015-07-10T15:25:04-07:00Oscar Byrnehttp://code.activestate.com/recipes/users/4192487/http://code.activestate.com/recipes/579079-decorator-for-defining-functions-with-keyword-only/
<p style="color: grey">
Python
recipe 579079
by <a href="/recipes/users/4192487/">Oscar Byrne</a>
(<a href="/recipes/tags/arguments/">arguments</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/keyword/">keyword</a>, <a href="/recipes/tags/metadecorator/">metadecorator</a>, <a href="/recipes/tags/positional/">positional</a>, <a href="/recipes/tags/python2/">python2</a>, <a href="/recipes/tags/python3/">python3</a>).
Revision 2.
</p>
<p>Python2.x implementation of python3's keyword-only arguments (aka arguments that must be specified as keywords, and are not automatically filled in by positional arguments - see PEP 3102).</p>
Fluent API method decorator (Python)
2015-07-08T13:20:58-07:00Oscar Byrnehttp://code.activestate.com/recipes/users/4192487/http://code.activestate.com/recipes/579078-fluent-api-method-decorator/
<p style="color: grey">
Python
recipe 579078
by <a href="/recipes/users/4192487/">Oscar Byrne</a>
(<a href="/recipes/tags/api/">api</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/fluent/">fluent</a>, <a href="/recipes/tags/interface/">interface</a>).
</p>
<p>A decorator for producing robust fluent API interfaces by returning a copy of self</p>
Inherit method docstrings without breaking decorators or violating DRY (Python)
2013-07-01T02:29:40-07:00nikratiohttp://code.activestate.com/recipes/users/4180248/http://code.activestate.com/recipes/578587-inherit-method-docstrings-without-breaking-decorat/
<p style="color: grey">
Python
recipe 578587
by <a href="/recipes/users/4180248/">nikratio</a>
(<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/docstring/">docstring</a>, <a href="/recipes/tags/inheritance/">inheritance</a>).
Revision 2.
</p>
<p>There are several recipes for inheriting method docstrings. However, most of them either violate DRY (and have you repeat the ancestor class name in the method decorator that sets the docstring), or do break decorators that try to access the docstring (because the docstring is only assigned after class creation). This recipe avoids both problems.</p>
<p><strong>Note</strong>: This recipe uses the mro method from <a href="http://code.activestate.com/recipes/577748-calculate-the-mro-of-a-class/" rel="nofollow">http://code.activestate.com/recipes/577748-calculate-the-mro-of-a-class/</a></p>
Optional arguments decorator (Python)
2013-01-28T05:03:43-08:00Maxime H Lapointehttp://code.activestate.com/recipes/users/4185033/http://code.activestate.com/recipes/578435-optional-arguments-decorator/
<p style="color: grey">
Python
recipe 578435
by <a href="/recipes/users/4185033/">Maxime H Lapointe</a>
(<a href="/recipes/tags/class_decorator/">class_decorator</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/python/">python</a>).
Revision 2.
</p>
<p>If you need your decorator to receive arguments, optional or not, then you should decorate it with the opt_arg_dec decorator to handle all the logic that this implies for you.</p>
timeout decorator (with multiprocessing) (Python)
2011-08-23T05:25:09-07:00matt harrisonhttp://code.activestate.com/recipes/users/4179061/http://code.activestate.com/recipes/577853-timeout-decorator-with-multiprocessing/
<p style="color: grey">
Python
recipe 577853
by <a href="/recipes/users/4179061/">matt harrison</a>
(<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/multiprocessing/">multiprocessing</a>, <a href="/recipes/tags/timeout/">timeout</a>).
</p>
<p>Given that threads shouldn't be killed, multiprocessing is one mechanism for limiting time on a potentially long running computation.</p>
Inherit Method Docstrings Using Only Function Decorators (Python)
2011-06-26T02:28:55-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577746-inherit-method-docstrings-using-only-function-deco/
<p style="color: grey">
Python
recipe 577746
by <a href="/recipes/users/4177816/">Eric Snow</a>
(<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/docstring/">docstring</a>).
Revision 3.
</p>
<p>This recipe provides a descriptor and a decorator. The decorator will be used on any method in your class to indicate that you want that method to inherit its docstring.</p>
<p>This is useful when you are using abstract bases classes and want a method to have the same docstring as the abstract method it implements.</p>
<p>This recipe uses <a href="http://code.activestate.com/recipes/577745/">recipe #577745</a>, the deferred_binder module.</p>
Lazy Dynamic Binding on Classes (You'll Never Go Back) (Python)
2011-08-12T23:42:27-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577745-lazy-dynamic-binding-on-classes-youll-never-go-bac/
<p style="color: grey">
Python
recipe 577745
by <a href="/recipes/users/4177816/">Eric Snow</a>
(<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/descriptor/">descriptor</a>, <a href="/recipes/tags/descriptors/">descriptors</a>).
Revision 4.
</p>
<p>This recipe provides a descriptor class and a decorator to make the deferred binding. Before the first access to that name, the __dict__ of the class will have the descriptor object. After that first access it will have whatever was returned by the first access.</p>
<p>One big advantage of deferring the binding is that the class object will be available at that time and can be passed to the object. During normal class creation the class body is exec'd before the class object is created, so the objects bound there can't be passed the class.</p>
<p><a href="http://code.activestate.com/recipes/577746/">Recipe #577746</a> provides a concrete example of how the deferred_binder can be used.</p>
Using Metaclasses and Class Decorators to Inherit Function Docstrings (Python)
2011-06-11T00:59:35-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577743-using-metaclasses-and-class-decorators-to-inherit-/
<p style="color: grey">
Python
recipe 577743
by <a href="/recipes/users/4177816/">Eric Snow</a>
(<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/docstring/">docstring</a>, <a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/metaclass/">metaclass</a>, <a href="/recipes/tags/metaclasses/">metaclasses</a>).
Revision 4.
</p>
<p>You'll find three different approaches to copying the method's docstring to the overriding method on a child class.</p>
<p>The function decorator approach is limited by the fact that you have to know the class when you call the decorator, so it can't be used inside a class body. However, <a href="http://code.activestate.com/recipes/577746/">recipe #577746</a> provides a function decorator that does not have this limitation.</p>
Decorator and context manager from a single API (Python)
2010-06-27T15:15:01-07:00Michael Foordhttp://code.activestate.com/recipes/users/2183852/http://code.activestate.com/recipes/577273-decorator-and-context-manager-from-a-single-api/
<p style="color: grey">
Python
recipe 577273
by <a href="/recipes/users/2183852/">Michael Foord</a>
(<a href="/recipes/tags/contextmanager/">contextmanager</a>, <a href="/recipes/tags/context_manager/">context_manager</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/with/">with</a>, <a href="/recipes/tags/with_statement/">with_statement</a>).
Revision 12.
</p>
<p>Create objects that act as both context managers <em>and</em> as decorators, and behave the same in both cases.</p>
<p>Works with Python 2.4 - 2.7 and Python 3. The tests require unittest2 or Python 3.2 to run. (And because the tests use the with statement they won't work with Python 2.4.)</p>
<p>Example:</p>
<pre class="prettyprint"><code>from contextdecorator import ContextDecorator
class mycontext(ContextDecorator):
def __init__(self, *args):
"""Normal initialiser"""
def before(self):
"""
Called on entering the with block or starting the decorated function.
If used in a with statement whatever this method returns will be the
context manager.
"""
def after(self, *exc):
"""
Called on exit. Arguments and return value of this method have
the same meaning as the __exit__ method of a normal context
manager.
"""
</code></pre>
<p>Both before and after methods are optional (but providing neither is somewhat pointless). See the tests for more usage examples.</p>