Top-rated Python recipes http://code.activestate.com/recipes/langs/python/top/2013-01-17T09:28:24-08:00ActiveState Code RecipesDecorator for BindingConstants at compile time (Python)
2010-11-16T08:36:38-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/277940-decorator-for-bindingconstants-at-compile-time/
<p style="color: grey">
Python
recipe 277940
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/programs/">programs</a>).
Revision 9.
</p>
<p>Decorator for automatic code optimization. If a global is known at compile time, replace it with a constant. Fold tuples of constants into a single constant. Fold constant attribute lookups into a single constant.</p>
Infix operators (Python)
2005-02-17T11:12:04-08:00Ferdinand Jamitzkyhttp://code.activestate.com/recipes/users/98863/http://code.activestate.com/recipes/384122-infix-operators/
<p style="color: grey">
Python
recipe 384122
by <a href="/recipes/users/98863/">Ferdinand Jamitzky</a>
(<a href="/recipes/tags/oop/">oop</a>).
Revision 3.
</p>
<p>Python has the wonderful "in" operator and it would be nice to have additional infix operator like this. This recipe shows how (almost) arbitrary infix operators can be defined.</p>
Spreadsheet (Python)
2004-12-03T09:13:43-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/355045-spreadsheet/
<p style="color: grey">
Python
recipe 355045
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/programs/">programs</a>).
Revision 2.
</p>
<p>Use eval() to drive spreadsheet style logic. The sleeper feature of Py2.4 is the ability to use any object with a mapping interface as the locals argument to eval().</p>
Named Tuples (Python)
2009-05-26T22:44:39-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/500261-named-tuples/
<p style="color: grey">
Python
recipe 500261
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/shortcuts/">shortcuts</a>).
Revision 15.
</p>
<p>Fast, lightweight attribute-style access to tuples.</p>
Linear equations solver in 3 lines (Python)
2005-01-31T22:06:30-08:00Maxim Krikunhttp://code.activestate.com/recipes/users/1085177/http://code.activestate.com/recipes/365013-linear-equations-solver-in-3-lines/
<p style="color: grey">
Python
recipe 365013
by <a href="/recipes/users/1085177/">Maxim Krikun</a>
(<a href="/recipes/tags/programs/">programs</a>).
Revision 2.
</p>
<p>Just a little bit of hack: a linear equations solver using eval and built-in complex numbers:</p>
<pre class="prettyprint"><code>>>> solve("x - 2*x + 5*x - 46*(235-24) = x + 2")
3236.0
</code></pre>
Singleton? We don't need no stinkin' singleton: the Borg design pattern (Python)
2001-08-27T08:05:21-07:00Alex Martellihttp://code.activestate.com/recipes/users/97991/http://code.activestate.com/recipes/66531-singleton-we-dont-need-no-stinkin-singleton-the-bo/
<p style="color: grey">
Python
recipe 66531
by <a href="/recipes/users/97991/">Alex Martelli</a>
(<a href="/recipes/tags/oop/">oop</a>).
Revision 2.
</p>
<p>The Singleton design pattern (DP) has a catchy name, but the wrong focus -- on identity rather than on state. The Borg design pattern has all instances share state instead, and Python makes it, literally, a snap.</p>
Send an HTML email with embedded image and plain text alternate (Python)
2006-01-29T18:40:36-08:00darrin massenahttp://code.activestate.com/recipes/users/1987292/http://code.activestate.com/recipes/473810-send-an-html-email-with-embedded-image-and-plain-t/
<p style="color: grey">
Python
recipe 473810
by <a href="/recipes/users/1987292/">darrin massena</a>
(<a href="/recipes/tags/network/">network</a>).
</p>
<p>HTML is the method of choice for those wishing to send emails with rich text, layout and graphics. Often it is desirable to embed the graphics within the message so recipients can display the message directly, without further downloads.</p>
<p>Some mail agents don't support HTML or their users prefer to receive plain text messages. Senders of HTML messages should include a plain text message as an alternate for these users.</p>
<p>This recipe sends a short HTML message with a single embedded image and an alternate plain text message.</p>
Creating a daemon the Python way (Python)
2005-10-03T16:49:09-07:00Chad J. Schroederhttp://code.activestate.com/recipes/users/1760491/http://code.activestate.com/recipes/278731-creating-a-daemon-the-python-way/
<p style="color: grey">
Python
recipe 278731
by <a href="/recipes/users/1760491/">Chad J. Schroeder</a>
(<a href="/recipes/tags/threads/">threads</a>).
Revision 6.
</p>
<p>The Python way to detach a process from the controlling terminal and run it in the
background as a daemon.</p>
Groupby (Python)
2004-02-12T07:05:02-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/259173-groupby/
<p style="color: grey">
Python
recipe 259173
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 5.
</p>
<p>Guido inspired SQL-like GROUPBY class that also encapsulates the logic in a Unix-like "sort | uniq".</p>
Readable switch construction without lambdas or dictionaries (Python)
2005-04-26T10:51:04-07:00Brian Beckhttp://code.activestate.com/recipes/users/2425329/http://code.activestate.com/recipes/410692-readable-switch-construction-without-lambdas-or-di/
<p style="color: grey">
Python
recipe 410692
by <a href="/recipes/users/2425329/">Brian Beck</a>
(<a href="/recipes/tags/extending/">extending</a>).
Revision 8.
</p>
<p>Python's lack of a 'switch' statement has garnered much discussion and even a PEP. The most popular substitute uses dictionaries to map cases to functions, which requires lots of defs or lambdas. While the approach shown here may be O(n) for cases, it aims to duplicate C's original 'switch' functionality and structure with reasonable accuracy.</p>
Bloom Filter (Python)
2011-06-04T17:44:21-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577684-bloom-filter/
<p style="color: grey">
Python
recipe 577684
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/big_table/">big_table</a>, <a href="/recipes/tags/bloom_filter/">bloom_filter</a>, <a href="/recipes/tags/sets/">sets</a>, <a href="/recipes/tags/spelling_checker/">spelling_checker</a>, <a href="/recipes/tags/spell_checker/">spell_checker</a>).
Revision 18.
</p>
<p>Space efficient, probabilistic set membership tester. Has no False Negatives but allows a rare False Positive.</p>
Proof-of-concept for a more space-efficient, faster-looping dictionary (Python)
2013-01-17T09:28:24-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/578375-proof-of-concept-for-a-more-space-efficient-faster/
<p style="color: grey">
Python
recipe 578375
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/compact/">compact</a>, <a href="/recipes/tags/dictionary/">dictionary</a>).
Revision 20.
</p>
<p>Save space and improve iteration speed by moving the hash/key/value entries to a densely packed array keeping only a sparse array of indices. This eliminates wasted space without requiring any algorithmic changes.</p>
First Class Enums in Python (Python)
2005-05-09T16:53:44-07:00Zoran Isailovskihttp://code.activestate.com/recipes/users/2400454/http://code.activestate.com/recipes/413486-first-class-enums-in-python/
<p style="color: grey">
Python
recipe 413486
by <a href="/recipes/users/2400454/">Zoran Isailovski</a>
(<a href="/recipes/tags/programs/">programs</a>).
Revision 7.
</p>
<p>True immutable symbolic enumeration with qualified value access.</p>
Dependency Injection The Python Way (Python)
2007-03-10T11:26:16-08:00Zoran Isailovskihttp://code.activestate.com/recipes/users/2400454/http://code.activestate.com/recipes/413268-dependency-injection-the-python-way/
<p style="color: grey">
Python
recipe 413268
by <a href="/recipes/users/2400454/">Zoran Isailovski</a>
(<a href="/recipes/tags/oop/">oop</a>).
Revision 9.
</p>
<p>Sample Pythonic Inversion-of-Control Pseudo-Container.</p>
Dirt simple map/reduce (Python)
2011-05-15T16:46:55-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577676-dirt-simple-mapreduce/
<p style="color: grey">
Python
recipe 577676
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/analysis/">analysis</a>, <a href="/recipes/tags/crosstab/">crosstab</a>, <a href="/recipes/tags/functional/">functional</a>, <a href="/recipes/tags/map_reduce/">map_reduce</a>, <a href="/recipes/tags/pivot_table/">pivot_table</a>, <a href="/recipes/tags/statistics/">statistics</a>).
Revision 9.
</p>
<p>Simple tool for analyzing datasets.</p>
Read tabular data from Excel spreadsheets the fast and easy way (Python)
2005-12-14T20:36:30-08:00Nicolas Lehuenhttp://code.activestate.com/recipes/users/1599156/http://code.activestate.com/recipes/440661-read-tabular-data-from-excel-spreadsheets-the-fast/
<p style="color: grey">
Python
recipe 440661
by <a href="/recipes/users/1599156/">Nicolas Lehuen</a>
(<a href="/recipes/tags/database/">database</a>).
Revision 3.
</p>
<p>Sometimes you get an Excel spreadsheet (say, from the marketing departement) and you want to read tabular data from it (i.e. a line with column headers and lines of data). There are many ways to do this (including ODBC + mxODBC), but the easiest way I've found is this one : provide a file name and a sheet name, and read the data !</p>
File Tkinter dialogs (Python)
2005-07-22T08:23:58-07:00Sébastien Sauvagehttp://code.activestate.com/recipes/users/2503505/http://code.activestate.com/recipes/438123-file-tkinter-dialogs/
<p style="color: grey">
Python
recipe 438123
by <a href="/recipes/users/2503505/">Sébastien Sauvage</a>
.
</p>
<p>Basic Tkinter dialogs for directory selection, file selection and so on.
That's dirt simple (although the official documentation is not very explicit about these features).</p>
Generator for permutations, combinations, selections of a sequence (Python)
2003-03-20T10:54:20-08:00Ulrich Hoffmannhttp://code.activestate.com/recipes/users/1056747/http://code.activestate.com/recipes/190465-generator-for-permutations-combinations-selections/
<p style="color: grey">
Python
recipe 190465
by <a href="/recipes/users/1056747/">Ulrich Hoffmann</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
</p>
<p>Permutations and combinations are often required in algorithms that do a complete search of the solution space. They are typically rather large so it's best not to compute them entirely but better to lazily generate them.
This recipe uses Python 2.2 generators to create appropriate generator objects,
that can be use for example as ranges in for loops.</p>
Compare speeds of different kinds of access to variables (Python)
2011-08-10T23:54:12-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577834-compare-speeds-of-different-kinds-of-access-to-var/
<p style="color: grey">
Python
recipe 577834
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/optimization/">optimization</a>).
Revision 5.
</p>
<p>Compare speeds of locals, nested scopes, global, builtins, instance variables, and class variables.</p>
OrderedSet (Python)
2012-12-19T07:12:32-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/576694-orderedset/
<p style="color: grey">
Python
recipe 576694
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/ordered/">ordered</a>, <a href="/recipes/tags/set/">set</a>).
Revision 9.
</p>
<p>Set that remembers original insertion order.</p>