Top-rated recipes by Alex Martelli http://code.activestate.com/recipes/users/97991/top/2003-10-17T15:57:11-07:00ActiveState Code RecipesSingleton? 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>
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>
Determining Current Function Name (Python)
2001-07-17T03:24:20-07:00Alex Martellihttp://code.activestate.com/recipes/users/97991/http://code.activestate.com/recipes/66062-determining-current-function-name/
<p style="color: grey">
Python
recipe 66062
by <a href="/recipes/users/97991/">Alex Martelli</a>
.
</p>
<p>You want to determine the name of the currently running function, e.g. to create error messages that don't need to be changed when copied to other functions. Function _getframe of module sys does this and much more.</p>
"To sort a dictionary" (Python)
2001-04-08T19:35:01-07:00Alex Martellihttp://code.activestate.com/recipes/users/97991/http://code.activestate.com/recipes/52306-to-sort-a-dictionary/
<p style="color: grey">
Python
recipe 52306
by <a href="/recipes/users/97991/">Alex Martelli</a>
(<a href="/recipes/tags/search/">search</a>).
Revision 2.
</p>
<p>Dictionaries can't be sorted -- a mapping has no ordering! -- so, when you feel the need to sort one, you no doubt want to sort its <em>keys</em> (in a separate list). Sorting (key,value) pairs (items) is simplest, but not fastest.</p>
Yet Another Python Templating Utility (YAPTU) (Python)
2001-08-31T15:17:32-07:00Alex Martellihttp://code.activestate.com/recipes/users/97991/http://code.activestate.com/recipes/52305-yet-another-python-templating-utility-yaptu/
<p style="color: grey">
Python
recipe 52305
by <a href="/recipes/users/97991/">Alex Martelli</a>
(<a href="/recipes/tags/text/">text</a>).
Revision 6.
</p>
<p>"Templating" (copying an input file to output, on the fly inserting Python expressions and statements) is a frequent need, and YAPTU is a small but complete Python module for that; expressions and statements are identified by arbitrary user-chosen regular-rexpressions.</p>
Add an entry to a dictionary, unless the entry is already there (Python)
2001-08-14T13:18:30-07:00Alex Martellihttp://code.activestate.com/recipes/users/97991/http://code.activestate.com/recipes/66516-add-an-entry-to-a-dictionary-unless-the-entry-is-a/
<p style="color: grey">
Python
recipe 66516
by <a href="/recipes/users/97991/">Alex Martelli</a>
(<a href="/recipes/tags/shortcuts/">shortcuts</a>).
Revision 2.
</p>
<p>Often, when working with a dictionary D, you need to use the entry D[k] if it's already present, or add a new D[k] if k wasn't a key into D. The setdefault method of dictionaries is a very handy shortcut for this task.</p>
"Static-methods" (aka "class-methods") in Python (Python)
2001-06-19T10:16:00-07:00Alex Martellihttp://code.activestate.com/recipes/users/97991/http://code.activestate.com/recipes/52304-static-methods-aka-class-methods-in-python/
<p style="color: grey">
Python
recipe 52304
by <a href="/recipes/users/97991/">Alex Martelli</a>
(<a href="/recipes/tags/oop/">oop</a>).
Revision 3.
</p>
<p>An attribute of a class-object is implicitly mutated into an unbound-method object if it starts out as a Python-coded function; thus, such functions must be wrapped as other callables if "just calling them" (without an instance-argument) is desired.</p>
Constants in Python (Python)
2001-08-20T07:12:14-07:00Alex Martellihttp://code.activestate.com/recipes/users/97991/http://code.activestate.com/recipes/65207-constants-in-python/
<p style="color: grey">
Python
recipe 65207
by <a href="/recipes/users/97991/">Alex Martelli</a>
(<a href="/recipes/tags/oop/">oop</a>).
Revision 2.
</p>
<p>In Python, any variable can be re-bound at will -- and modules don't let you define special methods such as an instance's __setattr__ to stop attribute re-binding. Easy solution (in Python 2.1 and up): use an instance as "module"...</p>
Expanding and Compressing Tabs (Python)
2001-06-28T15:42:20-07:00Alex Martellihttp://code.activestate.com/recipes/users/97991/http://code.activestate.com/recipes/65226-expanding-and-compressing-tabs/
<p style="color: grey">
Python
recipe 65226
by <a href="/recipes/users/97991/">Alex Martelli</a>
(<a href="/recipes/tags/text/">text</a>).
Revision 2.
</p>
<p>You want to convert tabs in a string to the appropriate number of spaces, or vice versa.</p>
high-performance currying with instancemethod (Python)
2003-10-17T15:57:11-07:00Alex Martellihttp://code.activestate.com/recipes/users/97991/http://code.activestate.com/recipes/229472-high-performance-currying-with-instancemethod/
<p style="color: grey">
Python
recipe 229472
by <a href="/recipes/users/97991/">Alex Martelli</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
</p>
<p>instancemethod provides a way to perform currying such that the curried function runs much faster than one produced by closure (the second-fastest way). [Currying is explained and covered in detail in other recipes]</p>
IP address conversion functions with the builtin socket module (Python)
2001-08-14T09:14:39-07:00Alex Martellihttp://code.activestate.com/recipes/users/97991/http://code.activestate.com/recipes/66517-ip-address-conversion-functions-with-the-builtin-s/
<p style="color: grey">
Python
recipe 66517
by <a href="/recipes/users/97991/">Alex Martelli</a>
(<a href="/recipes/tags/network/">network</a>).
</p>
<p>Convert dotted-quad IP addresses to long integer and back, get network and host portions from an IP address, all nice and fast thanks to the builtin socket module (with a little help from the builtin struct module, too).</p>
A simple extension type for Python (Python)
2001-08-15T16:35:12-07:00Alex Martellihttp://code.activestate.com/recipes/users/97991/http://code.activestate.com/recipes/66509-a-simple-extension-type-for-python/
<p style="color: grey">
Python
recipe 66509
by <a href="/recipes/users/97991/">Alex Martelli</a>
(<a href="/recipes/tags/extending/">extending</a>).
Revision 2.
</p>
<p>C-coded Python extension types have an aura of mystery and difficulty they don't really deserve. Sure, it's a lot of work to implement every possible nicety, but a fundamental useful type doesn't take much.</p>
Reversing a String by Words or Characters (Python)
2001-06-18T05:28:27-07:00Alex Martellihttp://code.activestate.com/recipes/users/97991/http://code.activestate.com/recipes/65225-reversing-a-string-by-words-or-characters/
<p style="color: grey">
Python
recipe 65225
by <a href="/recipes/users/97991/">Alex Martelli</a>
(<a href="/recipes/tags/text/">text</a>).
</p>
<p>You want to reverse the characters or words of a string.</p>
Accessing Substrings (Python)
2001-06-18T05:01:58-07:00Alex Martellihttp://code.activestate.com/recipes/users/97991/http://code.activestate.com/recipes/65224-accessing-substrings/
<p style="color: grey">
Python
recipe 65224
by <a href="/recipes/users/97991/">Alex Martelli</a>
(<a href="/recipes/tags/text/">text</a>).
</p>
<p>You want to access portions of a string. For example, you've read a fixed-width record and want to extract the fields.</p>
"conditionals" in expressions (Python)
2001-04-08T14:57:10-07:00Alex Martellihttp://code.activestate.com/recipes/users/97991/http://code.activestate.com/recipes/52310-conditionals-in-expressions/
<p style="color: grey">
Python
recipe 52310
by <a href="/recipes/users/97991/">Alex Martelli</a>
(<a href="/recipes/tags/algorithms/">algorithms</a>).
Revision 2.
</p>
<p>Python's "if" is a _statement_, and there is no conditional _operator_ (like C's "a?b:c" ternary) that we could use where expressions are needed (lambdas, etc); however, with some due care, equivalents can easily be coded.</p>
Automatic delegation as an alternative to inheritance (Python)
2001-04-08T21:03:15-07:00Alex Martellihttp://code.activestate.com/recipes/users/97991/http://code.activestate.com/recipes/52295-automatic-delegation-as-an-alternative-to-inherita/
<p style="color: grey">
Python
recipe 52295
by <a href="/recipes/users/97991/">Alex Martelli</a>
(<a href="/recipes/tags/oop/">oop</a>).
Revision 2.
</p>
<p>Python classes cannot inherit from any type, just from other classes. However, automatic delegation (via __getattr__ and __setattr__) can provide pretty much the same functionality as inheritance (without such limits, and with finer-grained control).</p>
various idioms for "call my superclass's __init__ IF it has one" (Python)
2001-04-27T14:48:19-07:00Alex Martellihttp://code.activestate.com/recipes/users/97991/http://code.activestate.com/recipes/52236-various-idioms-for-call-my-superclasss-__init__-if/
<p style="color: grey">
Python
recipe 52236
by <a href="/recipes/users/97991/">Alex Martelli</a>
(<a href="/recipes/tags/oop/">oop</a>).
Revision 2.
</p>
<p>Python does not automatically call the __init__ (and __del__) methods of superclasses if subclasses define their own; explicit calling is needed, and it may be advisable to use a call-if-it-exists idiom.</p>
Parallel loop on index and sequence-item (Python)
2001-07-04T08:05:36-07:00Alex Martellihttp://code.activestate.com/recipes/users/97991/http://code.activestate.com/recipes/52233-parallel-loop-on-index-and-sequence-item/
<p style="color: grey">
Python
recipe 52233
by <a href="/recipes/users/97991/">Alex Martelli</a>
.
Revision 3.
</p>
<p>zip() stops zipping at the shortest of its sequence-arguments and thus also allows unbounded-sequence arguments. This affords, for example, a very spare idiom for the frequent need of a parallel loop on index and sequence-item.</p>
Fast copy of an object having a slow __init__ (Python)
2001-08-10T04:03:41-07:00Alex Martellihttp://code.activestate.com/recipes/users/97991/http://code.activestate.com/recipes/66507-fast-copy-of-an-object-having-a-slow-__init__/
<p style="color: grey">
Python
recipe 66507
by <a href="/recipes/users/97991/">Alex Martelli</a>
(<a href="/recipes/tags/oop/">oop</a>).
</p>
<p>Special method __copy__ is the easiest way for an object to cooperate with the copy.copy function, but how do you bypass the object's __init__, if it's slow, to get an 'empty' object of this class? Easy -- here's how.</p>
Reading Lines with Continuation Characters (Python)
2001-07-17T06:01:13-07:00Alex Martellihttp://code.activestate.com/recipes/users/97991/http://code.activestate.com/recipes/66064-reading-lines-with-continuation-characters/
<p style="color: grey">
Python
recipe 66064
by <a href="/recipes/users/97991/">Alex Martelli</a>
(<a href="/recipes/tags/files/">files</a>).
</p>
<p>You have a file with long lines split over two or more lines, with backslashes to indicate that a continuation line follows. You want to rejoin those split lines.</p>