Most viewed recipes by Eric Snow Code RecipesA Simple Namespace Class (Python) 2011-10-03T21:12:41-07:00Eric Snow <p style="color: grey"> Python recipe 577887 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/dict/">dict</a>, <a href="/recipes/tags/namespaces/">namespaces</a>). Revision 3. </p> <p>"Namespaces are one honking great idea -- let's do more of those!" -- <a href="">The Zen of Python</a></p> <p>For when you want a simple, easy namespace, but you don't want it cluttered up with Python's object machinery.</p> Inspect a PYC File (Python) 2011-09-29T20:07:10-07:00Eric Snow <p style="color: grey"> Python recipe 577880 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/bytecode/">bytecode</a>, <a href="/recipes/tags/inspection/">inspection</a>, <a href="/recipes/tags/pyc/">pyc</a>). Revision 3. </p> <p>This is a revamp of a recipe that Ned Batchelder <a href="">posted on his blog</a> a few years ago (thanks Ned!). That page is pretty insightful, recipe aside.</p> <p>This recipe works for all versions of Python back to 2.4 (at least). Warning: using one version of Python to inspect a pyc file from another Python version may not work too well.</p> Apply decorators to all functions in a module (Python) 2011-06-09T22:51:28-07:00Eric Snow <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="">recipe 577740</a>) to apply decorators to any number of modules, or even (nearly) all of them.</p> See What the Builtins Are (Python) 2011-11-04T22:44:09-07:00Eric Snow <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> Mixin and Overlay (Python) 2011-06-01T05:16:47-07:00Eric Snow <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="">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> Mixins by Inheritance vs. by Decorator...Let's Try Decorators (Python) 2011-08-12T15:29:21-07:00Eric Snow <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> A class decorator for creating named tuples (Python) 2013-02-14T19:58:13-08:00Eric Snow <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> Simple Lock Classes Based on threading.Lock() (Python) 2011-10-19T21:26:39-07:00Eric Snow <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> Simple Abstract "Constants" to Use When @abstractproperty is Overkill or Misleading (Python) 2011-08-12T23:35:45-07:00Eric Snow <p style="color: grey"> Python recipe 577761 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/attributes/">attributes</a>, <a href="/recipes/tags/classes/">classes</a>). </p> <p>Use these instead of abstract properties when you don't plan on the abstract attribute being implemented with a property. And you can still give your attribute a docstring!</p> Smarter Default Arguments (Python) 2011-08-12T23:06:57-07:00Eric Snow <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> Using Metaclasses and Class Decorators to Inherit Function Docstrings (Python) 2011-06-11T00:59:35-07:00Eric Snow <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="">recipe #577746</a> provides a function decorator that does not have this limitation.</p> Emulate Keyword-Only Arguments in Python 2 (Python) 2011-11-03T18:31:52-07:00Eric Snow <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> Class-only Methods (Python) 2013-03-08T17:00:53-08:00Eric Snow <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> 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> Validating classes and objects against an Abstract Base Class (Python) 2011-05-21T19:14:19-07:00Eric Snow <p style="color: grey"> Python recipe 577711 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/abc/">abc</a>, <a href="/recipes/tags/validation/">validation</a>). Revision 4. </p> <p>Abstract Bases Classes in Python provide great features for describing interfaces programmatically. By default a subclass is validated against all its ABC parents at instantiation time (in object.__new__). This recipe aims to provide for validation against an ABC of:</p> <ul> <li>any class at definition time (including subclasses and registered classes),</li> <li>any object at any time.</li> </ul> <p>I have included an example of the reason I did all this. It allows you to implement an ABC in the instance rather than the class.</p> <p>If the classes argument to validate is None then it tries to build the list of classes from the object's MRO. If the ABCMeta.register method facilitated an __implements__ list on classes, we could also use that to validate against the registered "base" classes.</p> <p>The code I have provided is for Python 3, but it should work in 2.7 with a little modification.</p> <p>This code borrows from Lib/ and objects/typeobject.c</p> A Protocol for Making Objects Immutable (Python) 2011-10-07T03:59:45-07:00Eric Snow <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> A MutableMapping that Can Use Unhashable Objects as Keys (Python) 2012-04-04T20:36:59-07:00Eric Snow <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> Simple but Powerful Grouped Constants -- A Step Toward NamedConstants (Python) 2011-08-12T23:21:32-07:00Eric Snow <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> Make a Class's Name Available in its Definition Body (Python) 2011-08-04T21:38:40-07:00Eric Snow <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> A Script that Adds a Directory to sys.path Permanently (Python) 2012-02-16T23:15:11-08:00Eric Snow <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="" rel="nofollow"></a></p> Turn a Function Into a Class (Python) 2011-10-05T18:38:43-07:00Eric Snow <p style="color: grey"> Python recipe 577822 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/classes/">classes</a>, <a href="/recipes/tags/functions/">functions</a>). </p> <p>The only catch is that the function has to return locals() at the end. And it doesn't do the __prepare__ part of 3.x metaclasses.</p>