Popular recipes tagged "collections"http://code.activestate.com/recipes/tags/collections/2011-08-13T04:00:46-07:00ActiveState Code Recipesnamedtuple.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>
Dynamic Class Construction a la DSLs (Python)
2011-08-13T04:00:46-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577718-dynamic-class-construction-a-la-dsls/
<p style="color: grey">
Python
recipe 577718
by <a href="/recipes/users/4177816/">Eric Snow</a>
(<a href="/recipes/tags/collections/">collections</a>).
</p>
<p>So maybe it's not quite a mutable namedtuple. However, I borrowed heavily against the namedtuple implementation for this one.</p>
<p>I wanted to have a collection of classes that described data types without any functionality on them, sort of state holders. However, I wanted to use defaults and I wanted it to be mutable. </p>
<p>I noticed, as I started building my classes, that each was following the same pattern, so I extracted it out into this recipe. Wrapping namedtuple to get the same result would probably be feasible, but I enjoyed doing this too.</p>
<p>The distinction between parameters and properties is mostly one I was maintaining between single objects and collections. Realistically everything could have been parameters.</p>
Yet another roundrobin (Python)
2010-07-19T13:53:41-07:00Daniel Cohnhttp://code.activestate.com/recipes/users/4172918/http://code.activestate.com/recipes/577309-yet-another-roundrobin/
<p style="color: grey">
Python
recipe 577309
by <a href="/recipes/users/4172918/">Daniel Cohn</a>
(<a href="/recipes/tags/collections/">collections</a>, <a href="/recipes/tags/itertools/">itertools</a>, <a href="/recipes/tags/roundrobin/">roundrobin</a>).
Revision 2.
</p>
<p>This recipe provides a decently simple implementation of a roundrobin using itertools and deque.</p>
Virtual collections (Python)
2010-03-09T03:43:13-08:00Chris Leonghttp://code.activestate.com/recipes/users/4173277/http://code.activestate.com/recipes/577092-virtual-collections/
<p style="color: grey">
Python
recipe 577092
by <a href="/recipes/users/4173277/">Chris Leong</a>
(<a href="/recipes/tags/collections/">collections</a>).
</p>
<p>This recipe creates a collection proxy which allows several lists to be treated as one. When the collection proxy is accessed/modified, it is as though the operation was performed one the underlying lists.</p>
Sorted dictionary (Python)
2010-01-20T17:11:59-08:00Jan Kaliszewskihttp://code.activestate.com/recipes/users/4172762/http://code.activestate.com/recipes/576998-sorted-dictionary/
<p style="color: grey">
Python
recipe 576998
by <a href="/recipes/users/4172762/">Jan Kaliszewski</a>
(<a href="/recipes/tags/collections/">collections</a>, <a href="/recipes/tags/dict/">dict</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/mapping/">mapping</a>, <a href="/recipes/tags/sorted/">sorted</a>, <a href="/recipes/tags/sorteddict/">sorteddict</a>).
Revision 29.
</p>
<p>A simple implementation of a dictionary which always (when applicable) returns keys, values, items (key-value pairs) sorted by keys (inserting/removing order doesn't matter and only keys are important; so please note that it is something different than OrderedDict in Python 3.1/2.7 or Django's SortedDict).</p>