Popular recipes tagged "decorators"http://code.activestate.com/recipes/tags/decorators/popular/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> Decorator 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> 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> 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> 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> 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> 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> 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> 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> 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> 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> 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>