Top-rated recipes not tagged "batch" and "programs"http://code.activestate.com/recipes/tags/-batch-programs/top/2013-01-17T09:28:24-08:00ActiveState Code RecipesInfix 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>
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>
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>
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>
LRU and LFU cache decorators (Python)
2010-08-01T01:19:23-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/498245-lru-and-lfu-cache-decorators/
<p style="color: grey">
Python
recipe 498245
by <a href="/recipes/users/178123/">Raymond Hettinger</a>
(<a href="/recipes/tags/shortcuts/">shortcuts</a>).
Revision 6.
</p>
<p>One-line decorator call adds caching to functions with hashable arguments and no keyword arguments. When the maximum size is reached, the least recently used entry or least frequently used entry is discarded -- appropriate for long-running processes which cannot allow caches to grow without bound. Includes built-in performance instrumentation.</p>
Sorting big files the Python 2.4 way (Python)
2006-04-13T10:43:13-07:00Nicolas Lehuenhttp://code.activestate.com/recipes/users/1599156/http://code.activestate.com/recipes/466302-sorting-big-files-the-python-24-way/
<p style="color: grey">
Python
recipe 466302
by <a href="/recipes/users/1599156/">Nicolas Lehuen</a>
(<a href="/recipes/tags/files/">files</a>).
Revision 6.
</p>
<p>This recipe can be used to sort big files (much bigger than the available RAM) according to a key. The sort is guaranteed to be stable on Python 2.3.</p>
Module to allow Asynchronous subprocess use on Windows and Posix platforms (Python)
2006-12-01T17:30:02-08:00Josiah Carlsonhttp://code.activestate.com/recipes/users/1241800/http://code.activestate.com/recipes/440554-module-to-allow-asynchronous-subprocess-use-on-win/
<p style="color: grey">
Python
recipe 440554
by <a href="/recipes/users/1241800/">Josiah Carlson</a>
(<a href="/recipes/tags/sysadmin/">sysadmin</a>).
Revision 10.
</p>
<p>The 'subprocess' module in Python 2.4 has made creating and accessing subprocess streams in Python relatively convenient for all supported platforms, but what if you want to interact with the started subprocess? That is, what if you want to send a command, read the response, and send a new command based on that response?</p>
<p>Now there is a solution. The included subprocess.Popen subclass adds three new commonly used methods: recv(maxsize=None), recv_err(maxsize=None), and send(input), along with a utility method: send_recv(input='', maxsize=None).</p>
<p>recv() and recv_err() both read at most maxsize bytes from the started subprocess.
send() sends strings to the started subprocess.
send_recv() will send the provided input, and read up to maxsize bytes from both stdout and stderr.</p>
<p>If any of the pipes are closed, the attributes for those pipes will be set to None, and the methods will return None.</p>
<p>v. 1.3 fixed a few bugs relating to *nix support
v. 1.4,5 fixed initialization on all platforms, a few bugs relating to Windows support, added two utility functions, and added an example of how to use this module.
v. 1.6 fixed linux _recv() and test initialization thanks to Yuri Takhteyev at Stanford.
v. 1.7 removed _setup() and __init__() and fixed subprocess unittests thanks to Antonio Valentino. Added 4th argument 'tr' to recv_some(), which is, approximately, the number of times it will attempt to recieve data. Added 5th argument 'stderr' to recv_some(), where when true, will recieve from stderr. Cleaned up some pipe closing.
v. 1.8 Fixed missing self. parameter in non-windows _recv method thanks to comment.
v. 1.9 Fixed fcntl calls for closed handles.</p>
The simple but handy "collector of a bunch of named stuff" class (Python)
2001-04-08T19:08:56-07:00Alex Martellihttp://code.activestate.com/recipes/users/97991/http://code.activestate.com/recipes/52308-the-simple-but-handy-collector-of-a-bunch-of-named/
<p style="color: grey">
Python
recipe 52308
by <a href="/recipes/users/97991/">Alex Martelli</a>
(<a href="/recipes/tags/oop/">oop</a>).
Revision 2.
</p>
<p>Often we want to just collect a bunch of stuff together, naming each item of the bunch; a dictionary's OK for that, but a small do-nothing class is even handier, and prettier to use.</p>