Latest recipes by Alex Martelli http://code.activestate.com/recipes/users/97991/new/2003-10-17T15:57:11-07:00ActiveState Code Recipeshigh-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> 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> 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> 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> 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> 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> 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> 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> 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 &amp;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> 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> 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> 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> "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> 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> 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> "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> "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>