Most viewed recipes by Alex Martelli http://code.activestate.com/recipes/users/97991/views/2001-08-31T15:17:32-07:00ActiveState Code RecipesConstants 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>
"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>
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>
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>
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>
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>
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>
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>
"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>
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>
Read a text file by-paragraph (Python)
2001-08-15T16:47:48-07:00Alex Martellihttp://code.activestate.com/recipes/users/97991/http://code.activestate.com/recipes/66063-read-a-text-file-by-paragraph/
<p style="color: grey">
Python
recipe 66063
by <a href="/recipes/users/97991/">Alex Martelli</a>
(<a href="/recipes/tags/files/">files</a>).
Revision 2.
</p>
<p>Text files are most often read by-line, with excellent direct Python support. Sometimes we need to use other units, such as the paragraph -- a sequence of non-empty lines separated by empty lines. Python doesn't support that directly, but, as usual, it's not too hard to add such functionality.</p>
Assign and test (Python)
2001-07-16T08:14:17-07:00Alex Martellihttp://code.activestate.com/recipes/users/97991/http://code.activestate.com/recipes/66061-assign-and-test/
<p style="color: grey">
Python
recipe 66061
by <a href="/recipes/users/97991/">Alex Martelli</a>
.
</p>
<p>When transliterating C, Perl &c to Python, one often misses idioms such as "if((x=foo())" or "while((x=foo())" -- but, it's easy to get them back, with one small utility class!</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>
To 'return None' from your Python-callable C function (Python)
2001-03-26T12:02:44-08:00Alex Martellihttp://code.activestate.com/recipes/users/97991/http://code.activestate.com/recipes/52309-to-return-none-from-your-python-callable-c-functio/
<p style="color: grey">
Python
recipe 52309
by <a href="/recipes/users/97991/">Alex Martelli</a>
(<a href="/recipes/tags/extending/">extending</a>).
</p>
<p>Often a function written in C for Python needs to return nothing in particular -- a "return None" in Python terms; but _don't_ just "return Py_None" from C, as that will mess up reference counts!</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>
guaranteed-stable sort with the decorate-sort-undecorate idiom (aka Schwartzian transform) (Python)
2001-06-04T18:47:59-07:00Alex Martellihttp://code.activestate.com/recipes/users/97991/http://code.activestate.com/recipes/52234-guaranteed-stable-sort-with-the-decorate-sort-unde/
<p style="color: grey">
Python
recipe 52234
by <a href="/recipes/users/97991/">Alex Martelli</a>
(<a href="/recipes/tags/search/">search</a>).
Revision 2.
</p>
<p>Python lists' .sort method is not guaranteed stable -- items that compare equal may or may not be in unchanged order. Ensuring stability is easy as one of the many application of the commom idiom decorate-sort-undecorate (aka "Schwartzian transform").</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>
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>
calling a superclass's implementation of a method (Python)
2001-03-12T08:06:55-08:00Alex Martellihttp://code.activestate.com/recipes/users/97991/http://code.activestate.com/recipes/52235-calling-a-superclasss-implementation-of-a-method/
<p style="color: grey">
Python
recipe 52235
by <a href="/recipes/users/97991/">Alex Martelli</a>
(<a href="/recipes/tags/oop/">oop</a>).
</p>
<p>Python has no equivalent to Java's "super" keyword (so that some part of a method can be delegated to the superclass), but it's easy to get similar convenience despite Python's multiple-inheritance generality.</p>