Top-rated recipes tagged "decorator"http://code.activestate.com/recipes/tags/decorator/top/2015-09-22T18:16:40-07:00ActiveState Code RecipesThe goto decorator (Python) 2009-11-03T13:14:57-08:00Carl Cereckehttp://code.activestate.com/recipes/users/4172182/http://code.activestate.com/recipes/576944-the-goto-decorator/ <p style="color: grey"> Python recipe 576944 by <a href="/recipes/users/4172182/">Carl Cerecke</a> (<a href="/recipes/tags/bytecodes/">bytecodes</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/goto/">goto</a>). Revision 5. </p> <p>A function-decorator that provides the goto command.</p> Probably 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> Humanize decorator (Python) 2013-07-31T16:04:13-07:00tomer filibahttp://code.activestate.com/recipes/users/2520014/http://code.activestate.com/recipes/578619-humanize-decorator/ <p style="color: grey"> Python recipe 578619 by <a href="/recipes/users/2520014/">tomer filiba</a> (<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/objects/">objects</a>, <a href="/recipes/tags/pretty/">pretty</a>, <a href="/recipes/tags/print/">print</a>). </p> <p>When you need to inspect Python objects in a human-readable way, you're usually required to implement a custom <code>__str__</code> or <code>__repr__</code> which are just boilerplate (e.g., <code>return "Foo(%r, %r, %r)" % (self.bar, self.spam, self.eggs)</code>. You may implement <code>__str__</code> and <code>__repr__</code> by a base-class, but it's hard to call it <em>inheritance</em> and moreover, you may wish to remove it when you're done debugging.</p> <p>This simple (yet complete) recipe is a class decorator that injects <code>__str__</code> and <code>__repr__</code> into the class being printed. It handles nesting and even cycle detection, allowing you to just plug it into existing classes to get them pretty-printed and perhaps remove it later.</p> Decorators for adding aliases to methods in a class (Python) 2011-04-16T19:29:35-07:00José Nahuel Cuesta Luengohttp://code.activestate.com/recipes/users/4177689/http://code.activestate.com/recipes/577659-decorators-for-adding-aliases-to-methods-in-a-clas/ <p style="color: grey"> Python recipe 577659 by <a href="/recipes/users/4177689/">José Nahuel Cuesta Luengo</a> (<a href="/recipes/tags/alias/">alias</a>, <a href="/recipes/tags/decorator/">decorator</a>). </p> <p>The following recipe can be used as a means to adding aliases to methods in a class.</p> Asynchronous subprocess using asyncore (Python) 2013-01-21T19:51:00-08:00Glenn Eychanerhttp://code.activestate.com/recipes/users/4172294/http://code.activestate.com/recipes/576957-asynchronous-subprocess-using-asyncore/ <p style="color: grey"> Python recipe 576957 by <a href="/recipes/users/4172294/">Glenn Eychaner</a> (<a href="/recipes/tags/async/">async</a>, <a href="/recipes/tags/asynchronous/">asynchronous</a>, <a href="/recipes/tags/asyncore/">asyncore</a>, <a href="/recipes/tags/coroutine/">coroutine</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/ipc/">ipc</a>, <a href="/recipes/tags/subprocess/">subprocess</a>). Revision 21. </p> <p>A coroutine-based wrapper for subprocess.Popen that uses asyncore to communicate with child processes asynchronously. This allows subprocesses to be called from within socket servers or clients without needing a complicated event loop to check both. Uses <a href="http://code.activestate.com/recipes/576965/">recipe 576965</a> to provide the asynchronous coroutine framework, <a href="http://code.activestate.com/recipes/576967/">recipe 576967</a> to provide asynchronous pipes, and <a href="http://code.activestate.com/recipes/577600/">recipe 577600</a> to provide multiple alarms.</p> Run async code inline, nonblocking (Python) 2009-11-11T12:55:01-08:00Thomas Ahlehttp://code.activestate.com/recipes/users/4060075/http://code.activestate.com/recipes/576952-run-async-code-inline-nonblocking/ <p style="color: grey"> Python recipe 576952 by <a href="/recipes/users/4060075/">Thomas Ahle</a> (<a href="/recipes/tags/asynchronous/">asynchronous</a>, <a href="/recipes/tags/closure/">closure</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/threads/">threads</a>). Revision 7. </p> <p>A decorator, that makes it easy to switch between the mainthread and background threads.</p> C function decorator (Python) 2009-04-29T23:55:03-07:00geremy condrahttp://code.activestate.com/recipes/users/4170000/http://code.activestate.com/recipes/576731-c-function-decorator/ <p style="color: grey"> Python recipe 576731 by <a href="/recipes/users/4170000/">geremy condra</a> (<a href="/recipes/tags/cpp/">cpp</a>, <a href="/recipes/tags/ctypes/">ctypes</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/function/">function</a>). Revision 6. </p> <p>This recipe provides an easy-to-use, decorator-based solution to the problem of using functions from other languages, especially C, in Python. It takes advantage of Python's new annotations to provide simple type checking and automatic conversion. If you like it, you may also want to check out my <a href="http://code.activestate.com/recipes/576734/">C structure decorator</a></p> Immutable class decorator (Python) 2012-08-05T08:30:41-07:00Oren Tiroshhttp://code.activestate.com/recipes/users/2033964/http://code.activestate.com/recipes/578233-immutable-class-decorator/ <p style="color: grey"> Python recipe 578233 by <a href="/recipes/users/2033964/">Oren Tirosh</a> (<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/immutable/">immutable</a>). </p> <p>Apply this decorator to a class with __slots__. Members will be mutable during the execution of __init__ but read-only afterwards.</p> partial with out of order arguments (Python) 2011-10-27T16:54:26-07:00Przemyslaw Podczasihttp://code.activestate.com/recipes/users/4179716/http://code.activestate.com/recipes/577922-partial-with-out-of-order-arguments/ <p style="color: grey"> Python recipe 577922 by <a href="/recipes/users/4179716/">Przemyslaw Podczasi</a> (<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/partial/">partial</a>). Revision 2. </p> <p>Working with Windows API which usually takes like a zillion for each function can be a little bit frustrating and if I want to only change two in the middle for each call I had to wrap everything into lambda functions which change arguments to the order that I need to use with partial.</p> <p>So finally I added kinda dangerous decorator which inserts keywords into right position if detected and was about to use it but ctypes functions don't accept keyword arguments :D so just ended up with decorator :)</p> Fast and elegant switch/case-like dispatch (Python) 2011-09-02T01:49:40-07:00Jan Kaliszewskihttp://code.activestate.com/recipes/users/4172762/http://code.activestate.com/recipes/577864-fast-and-elegant-switchcase-like-dispatch/ <p style="color: grey"> Python recipe 577864 by <a href="/recipes/users/4172762/">Jan Kaliszewski</a> (<a href="/recipes/tags/case/">case</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/dispatcher/">dispatcher</a>, <a href="/recipes/tags/performance/">performance</a>, <a href="/recipes/tags/switch/">switch</a>). Revision 4. </p> <p>My approach to that common issue focuses on <strong>efficiency</strong> and <strong>elegant, declarative style</strong> of definition. That's why:</p> <ul> <li>The way switches work is based on unwrapped defaultdict/list lookup.</li> <li>The way you define switches is based on classes and easy-to-use decorators (note that you can use subclassing in a flexible way -- without any negative impact on efficiency!).</li> <li>Its use cases focus on a-lot-of-keys situations and it does not cover the <em>fall-through</em> feature (though you can reach its semantics if you really need that -- by calling class methods...).</li> </ul> Deprecated decorator (Python) 2013-12-14T01:24:31-08:00Giampaolo Rodolàhttp://code.activestate.com/recipes/users/4178764/http://code.activestate.com/recipes/577819-deprecated-decorator/ <p style="color: grey"> Python recipe 577819 by <a href="/recipes/users/4178764/">Giampaolo Rodolà</a> (<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/deprecated/">deprecated</a>, <a href="/recipes/tags/python/">python</a>). Revision 3. </p> <p>A decorator to deprecate a function and provide a new one as replacement.</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> Pure Python implementation of PEP 380 (yield from) (Python) 2009-04-25T09:03:42-07:00profjimhttp://code.activestate.com/recipes/users/4169991/http://code.activestate.com/recipes/576727-pure-python-implementation-of-pep-380-yield-from/ <p style="color: grey"> Python recipe 576727 by <a href="/recipes/users/4169991/">profjim</a> (<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/generators/">generators</a>). Revision 7. </p> <p>Decorator-based implementation of PEP 380 (yield from). This is the simple version (no special handling of nested "yield _from"s).</p> Method signature type checking decorator for Python 3 (Python) 2015-05-15T09:25:08-07:00Dmitry Dvoinikovhttp://code.activestate.com/recipes/users/2475216/http://code.activestate.com/recipes/572161-method-signature-type-checking-decorator-for-pytho/ <p style="color: grey"> Python recipe 572161 by <a href="/recipes/users/2475216/">Dmitry Dvoinikov</a> (<a href="/recipes/tags/checking/">checking</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/interface/">interface</a>, <a href="/recipes/tags/method/">method</a>, <a href="/recipes/tags/signature/">signature</a>, <a href="/recipes/tags/sysadmin/">sysadmin</a>, <a href="/recipes/tags/type/">type</a>). Revision 8. </p> <p>This recipe allows nice and clean validation for method parameters/return values. It uses function annotations available in Python 3 for the actual signature specification.</p> Keyword-only arguments in Python 2.x (Python) 2015-09-22T18:16:40-07:00Carahttp://code.activestate.com/recipes/users/4191397/http://code.activestate.com/recipes/578993-keyword-only-arguments-in-python-2x/ <p style="color: grey"> Python recipe 578993 by <a href="/recipes/users/4191397/">Cara</a> (<a href="/recipes/tags/arguments/">arguments</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/keyword/">keyword</a>, <a href="/recipes/tags/keyword_only/">keyword_only</a>). Revision 10. </p> <p>This is a decorator that makes some of a function's or method's arguments into keyword-only arguments. As much as it possible, it emulates the Python 3 handling of keyword-only arguments, including messages for TypeErrors. It's compatible with Python 3 so it can be used in code bases that support both versions.</p> Create and apply filters to lists of file paths (Python) 2012-03-20T21:17:26-07:00eysihttp://code.activestate.com/recipes/users/4177096/http://code.activestate.com/recipes/578084-create-and-apply-filters-to-lists-of-file-paths/ <p style="color: grey"> Python recipe 578084 by <a href="/recipes/users/4177096/">eysi</a> (<a href="/recipes/tags/blacklist/">blacklist</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/filter/">filter</a>, <a href="/recipes/tags/walk/">walk</a>, <a href="/recipes/tags/whitelist/">whitelist</a>, <a href="/recipes/tags/yield/">yield</a>). Revision 2. </p> <p>An experiment with filtering lists of files, documentation is written directly into the code.</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> Item Properties (Python) 2012-05-09T23:24:55-07:00Ian Kellyhttp://code.activestate.com/recipes/users/4178016/http://code.activestate.com/recipes/577703-item-properties/ <p style="color: grey"> Python recipe 577703 by <a href="/recipes/users/4178016/">Ian Kelly</a> (<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/descriptor/">descriptor</a>, <a href="/recipes/tags/oop/">oop</a>, <a href="/recipes/tags/property/">property</a>). Revision 2. </p> <p>A property variation that allows property access using an index or key.</p> Abstract method decorator (Python) 2011-04-20T21:50:23-07:00jimmy2timeshttp://code.activestate.com/recipes/users/4177690/http://code.activestate.com/recipes/577666-abstract-method-decorator/ <p style="color: grey"> Python recipe 577666 by <a href="/recipes/users/4177690/">jimmy2times</a> (<a href="/recipes/tags/abstract/">abstract</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/exception/">exception</a>, <a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/method/">method</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/subclass/">subclass</a>). Revision 5. </p> <p>A simple decorator that helps define abstract methods: when such a method is called, an appropriate exception is raised.</p> Keyword Argument Injection with Python Decorators (Python) 2010-09-05T17:06:04-07:00Ahmet Emre Aladağhttp://code.activestate.com/recipes/users/4174877/http://code.activestate.com/recipes/577382-keyword-argument-injection-with-python-decorators/ <p style="color: grey"> Python recipe 577382 by <a href="/recipes/users/4174877/">Ahmet Emre Aladağ</a> (<a href="/recipes/tags/class/">class</a>, <a href="/recipes/tags/class_decorator/">class_decorator</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/injection/">injection</a>, <a href="/recipes/tags/variable/">variable</a>). Revision 2. </p> <p>In most of the object oriented codes we write, we need to set class attributes to the given argument values and this is a very line-consuming thing. To get over these redundant lines, I found a solution using decorators. </p>