Popular recipes by Michael Foord http://code.activestate.com/recipes/users/2183852/2010-06-27T15:15:01-07:00ActiveState Code RecipesDecorator 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> Total Ordering: Class Decorator for Filling in Rich Comparison Methods When Only One is Implemented (Python) 2008-10-28T11:54:42-07:00Michael Foordhttp://code.activestate.com/recipes/users/2183852/http://code.activestate.com/recipes/576529-total-ordering-class-decorator-for-filling-in-rich/ <p style="color: grey"> Python recipe 576529 by <a href="/recipes/users/2183852/">Michael Foord</a> (<a href="/recipes/tags/class_decorator/">class_decorator</a>, <a href="/recipes/tags/comparison/">comparison</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/ordering/">ordering</a>, <a href="/recipes/tags/sorting/">sorting</a>). Revision 5. </p> <p><code>total_ordering</code> and <code>force_total_ordering</code> are class decorators for Python 2.6 &amp; Python 3.</p> <p>They provides <em>all</em> the rich comparison methods on a class by defining <em>any</em> one of '__lt__', '__gt__', '__le__', '__ge__'.</p> <p><code>total_ordering</code> fills in all unimplemented rich comparison methods, assuming at least one is implemented. <code>__lt__</code> is taken as the base comparison method on which the others are built, but if that is not available it will be constructed from the first one found.</p> <p><code>force_total_ordering</code> does the same, but having taken a comparison method as the base it fills in <em>all</em> the others - this overwrites additional comparison methods that may be implemented, guaranteeing consistent comparison semantics.</p>