Popular recipes tagged "namedtuple"http://code.activestate.com/recipes/tags/namedtuple/popular/2014-05-29T18:48:28-07:00ActiveState Code RecipesCreate on-the-fly class adapters with functools.partial (Python) 2014-05-29T18:48:28-07:00Christoph Schuelerhttp://code.activestate.com/recipes/users/4174094/http://code.activestate.com/recipes/578884-create-on-the-fly-class-adapters-with-functoolspar/ <p style="color: grey"> Python recipe 578884 by <a href="/recipes/users/4174094/">Christoph Schueler</a> (<a href="/recipes/tags/adapter/">adapter</a>, <a href="/recipes/tags/namedtuple/">namedtuple</a>, <a href="/recipes/tags/partial/">partial</a>, <a href="/recipes/tags/pattern/">pattern</a>). </p> <p>functools.partial could not only applied to functions it also works with classes. This opens some interesting perspectives, like on-the-fly creation of class adapters, as the following code illustrates.</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> PseudoStruct (Python) 2012-11-25T03:43:06-08:00Matthew Zipayhttp://code.activestate.com/recipes/users/4183355/http://code.activestate.com/recipes/578349-pseudostruct/ <p style="color: grey"> Python recipe 578349 by <a href="/recipes/users/4183355/">Matthew Zipay</a> (<a href="/recipes/tags/datastructures/">datastructures</a>, <a href="/recipes/tags/namedtuple/">namedtuple</a>, <a href="/recipes/tags/record/">record</a>). </p> <p>This is a recipe for a Python "data object." It is similar in function to namedtuple (<a href="http://code.activestate.com/recipes/500261/" rel="nofollow">http://code.activestate.com/recipes/500261/</a>) and recordtype (<a href="http://code.activestate.com/recipes/576555-records/" rel="nofollow">http://code.activestate.com/recipes/576555-records/</a>) in that it is a simple container for data, but is designed to meet three specific goals:</p> <ol> <li>Easy to subclass data objects.</li> <li>Get/set speed comparable to a simple class.</li> <li>Minimal memory consumption per instance.</li> </ol> namedtuple.abc - abstract base class + mix-in for named tuples (Python) 2011-04-02T02:07:00-07:00Jan Kaliszewskihttp://code.activestate.com/recipes/users/4172762/http://code.activestate.com/recipes/577629-namedtupleabc-abstract-base-class-mix-in-for-named/ <p style="color: grey"> Python recipe 577629 by <a href="/recipes/users/4172762/">Jan Kaliszewski</a> (<a href="/recipes/tags/abc/">abc</a>, <a href="/recipes/tags/collections/">collections</a>, <a href="/recipes/tags/dry/">dry</a>, <a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/namedtuple/">namedtuple</a>, <a href="/recipes/tags/shortcuts/">shortcuts</a>). Revision 7. </p> <p>If you need</p> <ul> <li>to define <strong>named tuple subclasses</strong> (including reusable abstract ones), adding/overriding some methods, in a convenient way;</li> <li>to have the named tuple ABC (abstract base class) for <strong>isinstance/issubclass</strong> tests;</li> <li>or simply would like to define your named tuple classes in a <strong>class-syntax-based and DRY way</strong> (without repeating type names...)</li> </ul> <p>-- <strong>this recipe is for you.</strong></p> typesafe records (Python) 2010-07-10T01:43:38-07:00andrew johnsonhttp://code.activestate.com/recipes/users/4174071/http://code.activestate.com/recipes/577249-typesafe-records/ <p style="color: grey"> Python recipe 577249 by <a href="/recipes/users/4174071/">andrew johnson</a> (<a href="/recipes/tags/namedtuple/">namedtuple</a>, <a href="/recipes/tags/record/">record</a>, <a href="/recipes/tags/type/">type</a>, <a href="/recipes/tags/typesafe/">typesafe</a>). Revision 4. </p> <p>A typesafe wrapper for namedtuple.</p> Records (Python) 2008-11-04T06:52:42-08:00George Sakkishttp://code.activestate.com/recipes/users/2591466/http://code.activestate.com/recipes/576555-records/ <p style="color: grey"> Python recipe 576555 by <a href="/recipes/users/2591466/">George Sakkis</a> (<a href="/recipes/tags/datastructures/">datastructures</a>, <a href="/recipes/tags/namedtuple/">namedtuple</a>, <a href="/recipes/tags/record/">record</a>). </p> <p>This is a recipe similar in functionality and exec-style optimized implementation to the very well received namedtuple (<a href="http://code.activestate.com/recipes/500261/" rel="nofollow">http://code.activestate.com/recipes/500261/</a>) that was included in Python 2.6. The main difference is that <strong>records</strong>, unlike named tuples, are mutable. In addition, fields can have a default value. Instead of subclassing tuple or list, the implementation create a regular class with __slots__.</p> Parse call function for Py2.6 and Py2.7 (Python) 2009-02-28T20:13:15-08:00Jervis Whitleyhttp://code.activestate.com/recipes/users/4169341/http://code.activestate.com/recipes/576671-parse-call-function-for-py26-and-py27/ <p style="color: grey"> Python recipe 576671 by <a href="/recipes/users/4169341/">Jervis Whitley</a> (<a href="/recipes/tags/ast/">ast</a>, <a href="/recipes/tags/call/">call</a>, <a href="/recipes/tags/function/">function</a>, <a href="/recipes/tags/namedtuple/">namedtuple</a>, <a href="/recipes/tags/nodevisitor/">nodevisitor</a>, <a href="/recipes/tags/parsing/">parsing</a>). Revision 14. </p> <p>In some cases it may be desirable to parse the string expression "f1(*args)" and return some of the key features of the represented function-like call. </p> <p>This recipe returns the key features in the form of a namedtuple. </p> <p>e.g. (for the above)</p> <pre class="prettyprint"><code>&gt;&gt;&gt; explain("f1(*args)") [ Call(func='f1', starargs='args') ] </code></pre> <p>The recipe will return a list of such namedtuples for <code>"f1(*args)\nf2(*args)"</code> Note that while the passed string expression must evaluate to valid python syntax, names needn't be declared in current scope.</p>