Top-rated recipes by Eric Snow http://code.activestate.com/recipes/users/4177816/top/2013-03-08T17:00:53-08:00ActiveState Code RecipesSmarter Default Arguments (Python) 2011-08-12T23:06:57-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577786-smarter-default-arguments/ <p style="color: grey"> Python recipe 577786 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/default_arguments/">default_arguments</a>, <a href="/recipes/tags/deferred/">deferred</a>, <a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/functions/">functions</a>). Revision 3. </p> <p>Improved handling of mutable and deferred default arguments. After the recipe you'll find an explanation of what that means.</p> <p>Works for 2.7 with minor tweaks (getfullargspec --> getargspec).</p> A class decorator for creating named tuples (Python) 2013-02-14T19:58:13-08:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/578456-a-class-decorator-for-creating-named-tuples/ <p style="color: grey"> Python recipe 578456 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/metaprogramming/">metaprogramming</a>, <a href="/recipes/tags/namedtuple/">namedtuple</a>). Revision 3. </p> <p>This class decorator factory is useful for replacing the following:</p> <pre class="prettyprint"><code>class MyTuple(namedtuple('MyTuple', "a b c")): """Something special.""" @classmethod def from_defaults(cls, a, b=None, c=5): return cls(a, b, c) </code></pre> <p>or even:</p> <pre class="prettyprint"><code>class MyTuple(namedtuple('MyTuple', "a b c")): """Something special.""" def __new__(cls, a, b=None, c=5): return super().__new__(cls, a, b, c) </code></pre> <p>with this:</p> <pre class="prettyprint"><code>@as_namedtuple("a b c", None, c=5) class MyTuple: """Something special.""" </code></pre> <p>I found that I often subclass named tuples to add on some functionality or even just a nice docstring. Plus with the class syntax there's no missing that a class is bound to the name (and it's a little easier to search for the definition). When you subclass a named tuple the boilerplate involved really jumps out.</p> <p>One of the main reasons Adding support for defaults to namedtuple would mitigate the need for that functionality here, but I'm not going to hold my breath on that.</p> <p>One nice (though minor) thing is that you don't have to repeat the name when defining the namedtuple.</p> A Simpler Namespace Class (Python) 2013-02-14T17:42:24-08:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/578141-a-simpler-namespace-class/ <p style="color: grey"> Python recipe 578141 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/namespaces/">namespaces</a>, <a href="/recipes/tags/object/">object</a>). Revision 3. </p> <p>A very simple, attribute-based namespace type (and one offspring). Everyone's written one of these...</p> Make a Class Available in its Own Definition Body! (Python) 2011-09-15T05:36:36-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577867-make-a-class-available-in-its-own-definition-body/ <p style="color: grey"> Python recipe 577867 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/class/">class</a>, <a href="/recipes/tags/metaclass/">metaclass</a>). </p> <p>This may sound too good to be true, but see for yourself. No time machines involved. Just plug in this metaclass and look for __newclass__ in your class body. It's not perfect, but the problematic corner cases are more bark than bite.</p> Make a Class's Name Available in its Definition Body (Python) 2011-08-04T21:38:40-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577813-make-a-classs-name-available-in-its-definition-bod/ <p style="color: grey"> Python recipe 577813 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/classes/">classes</a>). </p> <p>Since a class object is created <em>after</em> the body is executed, it can't be available to the class body. Even the name is unavailable, at least by default. However, you can use the <code>__prepare__()</code> method in a metaclass to stick it in there. This recipe is a simple demonstration of how.</p> Simple but Powerful Grouped Constants -- A Step Toward NamedConstants (Python) 2011-08-12T23:21:32-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577755-simple-but-powerful-grouped-constants-a-step-towar/ <p style="color: grey"> Python recipe 577755 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/constants/">constants</a>). Revision 6. </p> <p>The recipe provides an easy-to-use class for a group of "constants". It provides helper functions for getting the constant values just right. It also exposes the mechanism it uses to bind the contents of an iterable into the namespace of another object.</p> <p>For this binding and for the grouped constants, this recipe makes it easy to dynamically generate the mapped values you want to expose.</p> <p>The next step is to make the values aware of the context in which they are bound and to strengthen their association with the group they are in. And that is one of the recipes I'm working on next!</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> Mixin and Overlay (Python) 2011-06-01T05:16:47-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577730-mixin-and-overlay/ <p style="color: grey"> Python recipe 577730 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/classes/">classes</a>, <a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/mixins/">mixins</a>). </p> <h4 id="the-two-sides-of-classes">The Two Sides of Classes</h4> <p>Class inheritance either adds new attributes to a class, or changes existing attributes. However, this distinction is not made in Python's class system. This recipe is a stab at providing that separation. It takes advantage of metaclasses and class decorators to do it.</p> <p>For this recipe the two sides of class inheritance are called mixins and overlays. Mixins add new attributes to a class. Overlays change existing attributes, which in the case of methods means changing the behavior of the methods. However, overlays do not add any attributes.</p> <h4 id="separation-of-concerns">Separation of Concerns</h4> <p>Another issue with class inheritance is that abstraction rarely breaks down into perfect trees with a clear separation of concerns. Python allows multiple inheritance, which can help, but requires cooperation between classes in the diamond hierarchy. Mixins and overlays help with this problem. Hopefully, that will be evidenced by the recipe and subsequent examples.</p> <p>With this approach, the main single inheritance line can focus on the core abstraction and mixins/overlays can be used to extend the classes in other directions. This does not solve all the problems regarding separation of concerns, but it solves some. I hope to address the rest in another recipe that centers around delegation through a component architecture on instances.</p> <h4 id="interfaces">Interfaces</h4> <p>Since Python 2.6 we have had Abstract Base Classes providing a mechanism for promising what interfaces an object provides. I'll show in one of the examples how an ABC can be split into a interface portion and a mixin portion. The recipe takes advantage of the ABC functionality in the Python type system. I expect that mixins and overlays would be a good fit with one of my other recipes (<a href="http://code.activestate.com/recipes/577711/">Recipe 577711</a>).</p> <h4 id="the-recipe-classes">The Recipe Classes</h4> <p>This recipe provides a standard approach to applying mixin classes without using inheritance. This is done through a metaclass. The metaclass builds a __mixins__ attribute on the class and provides a mixes_in class decorator. Applying that decorator to the class will add the attributes in __mixins__ to the decorated class. However, if any of those names are already bound on the name then it will fail. The decorator returns the modified class.</p> <p><strong>Note</strong>: Traditional mixins are typically done through multiple inheritance, as opposed to class decorators.</p> <p>This recipe also provides a companion to mixin classes, called overlays. This is done through a metaclass in exactly the same way as the mixins, but provides an __overlays__ attribute and a class decorator called overlays. In contrast to mixins, if an overlay attribute is missing on the decorated class, it will fail. This is because the decorator will return a new class that inherits from the decorated class, and overrides the attributes in __overlays__.</p> <p><strong>Note</strong>: Metaclasses are used here because we need to pull from the class namespace there. A class decorator does not afford us the same functionality without more complexity.</p> <p><strong>Note</strong>: This recipe should work fine in 2.7 with a switch to the __metaclass__ syntax.</p> Class-only Methods (Python) 2013-03-08T17:00:53-08:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/578486-class-only-methods/ <p style="color: grey"> Python recipe 578486 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/metaprogramming/">metaprogramming</a>). </p> <p>We use the classmethod builtin to tie methods to the class rather than the instance. This is really useful for a variety of things, like alternate contructors. However, sometimes you don't want to expose the method on the instances, just on the class.</p> <p>For instance, it usually doesn't make a lot of sense to have direct access to alternate constructors from instances; the main constructor, via the __call__ method, is only available on the class. Why? So that instances call implement their own call semantics. This is similar to why the methods (and other class attributes) on namedtuples all have names starting with an underscore.</p> <p>To restrict a method just to the class, currently you must use a metaclass. Most people consider this black magic so they just use @classmethod. Furthermore, implementing a metaclass is simply more verbose than decorating a method with classmethod. Plus you have to deal with things like metaclass conflicts and the fact that metaclasses are inherited.</p> <p>So...here is a sibling to classmethod that makes a method class-only without introducing metaclass complexity. You use it the same way as you would classmethod.</p> A Class Decorator that Adds a copy() Method (Python) 2013-02-14T20:43:20-08:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/578457-a-class-decorator-that-adds-a-copy-method/ <p style="color: grey"> Python recipe 578457 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/metaprogramming/">metaprogramming</a>). </p> <p>Here's a class decorator that adds a rudimentary copy() method onto the decorated class. Use it like this:</p> <pre class="prettyprint"><code>@copiable class SomethingDifferent: def __init__(self, a, b, c): self.a = a self.b = b self.c = c </code></pre> <p>or like this:</p> <pre class="prettyprint"><code>@copiable("a b c") class SomethingDifferent: def __init__(self, a, b, c): self.a = a self.b = b self.c = c s = SomethingDifferent(1,2,3) sc = s.copy() assert vars(s) == vars(sc) </code></pre> <p>(Python 3.3)</p> A simple enum type (Python) 2013-02-13T06:53:38-08:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/578455-a-simple-enum-type/ <p style="color: grey"> Python recipe 578455 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/enum/">enum</a>, <a href="/recipes/tags/metaclass/">metaclass</a>). Revision 2. </p> <p>Inspired by various threads[1] on the python-ideas list, here's a simple enum type. Enums are generated either via the class syntax or via Enum.make().</p> <pre class="prettyprint"><code>class Breakfast(Enum): SPAM, HAM, EGGS BACON, SAUSAGE Breakfast = Enum.make("SPAM HAM EGGS BACON SAUSAGE") </code></pre> <p>Here are some of the features:</p> <p>Enum:</p> <ul> <li>inheriting from an enum inherits copies of its values.</li> <li>the export() method allows for exposing an enum's values in another namespace.</li> </ul> <p>Enum Values:</p> <ul> <li>the underlying values within an enum are essentially useless, diminishing the temptation to rely on them.</li> <li>identity is equality, like with None, True, and False.</li> <li>enum values support bitwise operations within the same enum.</li> <li>the result of a bitwise operation is always the same object given the same inputs.</li> </ul> <p>[1] see <a href="http://mail.python.org/pipermail/python-ideas/2013-January/019003.html" rel="nofollow">http://mail.python.org/pipermail/python-ideas/2013-January/019003.html</a></p> ChainedList and ChainedListView: Exposing Multiple Lists as a Single Sequence (Python) 2012-07-03T21:00:02-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/578190-chainedlist-and-chainedlistview-exposing-multiple-/ <p style="color: grey"> Python recipe 578190 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/lists/">lists</a>, <a href="/recipes/tags/wrapper/">wrapper</a>). </p> <p>Handy for composing lists without losing the underlying indepedence. The "lists" don't actually have to be lists, though for ChainedList they must be mutable.</p> A generic factory factory. (Python) 2012-06-11T23:31:11-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/578165-a-generic-factory-factory/ <p style="color: grey"> Python recipe 578165 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/factory/">factory</a>). </p> <p>Factories are useful for producing objects without having to store certain data on the objects' type. Instances of this factory class are factories that can produce </p> Tracking and Manipulating the Python Import State (Python) 2012-04-22T07:27:29-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/578107-tracking-and-manipulating-the-python-import-state/ <p style="color: grey"> Python recipe 578107 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/import/">import</a>). </p> <p>This is a rough analog to the import engine described in PEP 406. Here I've called it ImportState.The focus here is on using it as a context manager to limit changes to the import state to a block of code (in a with statement). Differences from PEP 406 are described below.</p> A MutableMapping that Can Use Unhashable Objects as Keys (Python) 2012-04-04T20:36:59-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/578096-a-mutablemapping-that-can-use-unhashable-objects-a/ <p style="color: grey"> Python recipe 578096 by <a href="/recipes/users/4177816/">Eric Snow</a> . </p> <p>The catch is that the unhashable objects aren't actually stored in the mapping. Only their IDs are. Thus you must also store the actual objects somewhere else.</p> A Script that Adds a Directory to sys.path Permanently (Python) 2012-02-16T23:15:11-08:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/578043-a-script-that-adds-a-directory-to-syspath-permanen/ <p style="color: grey"> Python recipe 578043 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/script/">script</a>, <a href="/recipes/tags/sys_path/">sys_path</a>). </p> <p>This script takes advantage of PEP 370, "Per user site-packages directory". It manages .pth files, which are are non-volatile (unlike manually adding to sys.path). See <a href="http://docs.python.org/library/site.html." rel="nofollow">http://docs.python.org/library/site.html.</a></p> Emulate Keyword-Only Arguments in Python 2 (Python) 2011-11-03T18:31:52-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577940-emulate-keyword-only-arguments-in-python-2/ <p style="color: grey"> Python recipe 577940 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/argument/">argument</a>, <a href="/recipes/tags/keyword_only/">keyword_only</a>, <a href="/recipes/tags/kwonly/">kwonly</a>, <a href="/recipes/tags/parameter/">parameter</a>). </p> <p>Python 3 introduced a useful feature: keyword-only arguments. In order to get the same effect in Python 2, you must use **kwargs in your parameter list. Then, at the beginning of your function body you must manually extract what would be the keyword-only arguments once you upgrade to 3.</p> <p>This recipe helps reduce the boilerplate to a single function call. You still don't get those parameters in your "def" clause (where they are more obvious), but at least it reduces the clutter.</p> Simple Lock Classes Based on threading.Lock() (Python) 2011-10-19T21:26:39-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577914-simple-lock-classes-based-on-threadinglock/ <p style="color: grey"> Python recipe 577914 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/lock/">lock</a>, <a href="/recipes/tags/locking/">locking</a>). </p> <p>Nothing fancy here. Locks can take many forms. Here are a few.</p> A Protocol for Making Objects Immutable (Python) 2011-10-07T03:59:45-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577895-a-protocol-for-making-objects-immutable/ <p style="color: grey"> Python recipe 577895 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/freeze/">freeze</a>, <a href="/recipes/tags/immutable/">immutable</a>, <a href="/recipes/tags/mutable/">mutable</a>, <a href="/recipes/tags/unfreeze/">unfreeze</a>). </p> <p>Python already provides immutable versions of many of the mutable built-in types. Dict is the notable exception. Regardless, here is a protocol that objects may implement that facilitates turning immutable object mutable and vice-versa.</p> See What the Builtins Are (Python) 2011-11-04T22:44:09-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577888-see-what-the-builtins-are/ <p style="color: grey"> Python recipe 577888 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/builtins/">builtins</a>). Revision 3. </p> <p>The built-ins are all those functions, constants, and types that are there when you need them most, like <code>list</code>, <code>any</code>, and <code>None</code>. This recipe is a simple way of looking at the built-ins. Check below for a thorough explanation.</p>