Latest recipes by Jürgen Hermann http://code.activestate.com/recipes/users/98061/new/2001-10-31T13:33:41-08:00ActiveState Code RecipesUsing the SAX2 LexicalHandler Interface (Python)
2001-10-31T13:33:41-08:00Jürgen Hermannhttp://code.activestate.com/recipes/users/98061/http://code.activestate.com/recipes/84516-using-the-sax2-lexicalhandler-interface/
<p style="color: grey">
Python
recipe 84516
by <a href="/recipes/users/98061/">Jürgen Hermann</a>
(<a href="/recipes/tags/xml/">xml</a>).
</p>
<p>This code show how to use the relatively unknown LexicalHandler
interface, which is an extension to the standard SAX2 interfaces like
ContentHandler (we assume you already have some SAX2 know-how).</p>
Fork a daemon process on Unix (Python)
2001-07-10T14:01:38-07:00Jürgen Hermannhttp://code.activestate.com/recipes/users/98061/http://code.activestate.com/recipes/66012-fork-a-daemon-process-on-unix/
<p style="color: grey">
Python
recipe 66012
by <a href="/recipes/users/98061/">Jürgen Hermann</a>
(<a href="/recipes/tags/threads/">threads</a>).
</p>
<p>Forking a daemon on Unix requires a certain sequence of system calls. Since Python exposes a full POSIX interface, this can be done in Python, too.</p>
Query whether an interface is up on UNIX (Python)
2001-07-05T16:24:30-07:00Jürgen Hermannhttp://code.activestate.com/recipes/users/98061/http://code.activestate.com/recipes/65449-query-whether-an-interface-is-up-on-unix/
<p style="color: grey">
Python
recipe 65449
by <a href="/recipes/users/98061/">Jürgen Hermann</a>
(<a href="/recipes/tags/network/">network</a>).
</p>
<p>This code shows how to call the low-level POSIX interface of Python, and handle the return values with the struct module.</p>
Decorate an output stream with print-like methods (Python)
2001-07-03T13:57:02-07:00Jürgen Hermannhttp://code.activestate.com/recipes/users/98061/http://code.activestate.com/recipes/65445-decorate-an-output-stream-with-print-like-methods/
<p style="color: grey">
Python
recipe 65445
by <a href="/recipes/users/98061/">Jürgen Hermann</a>
(<a href="/recipes/tags/oop/">oop</a>).
</p>
<p>The following code shows how to "decorate" objects with new functions, in this case any stream with two methods that work similar to the built-in "print" statement.</p>
Checking whether a string contains a set of chars (Python)
2001-07-03T21:00:41-07:00Jürgen Hermannhttp://code.activestate.com/recipes/users/98061/http://code.activestate.com/recipes/65441-checking-whether-a-string-contains-a-set-of-chars/
<p style="color: grey">
Python
recipe 65441
by <a href="/recipes/users/98061/">Jürgen Hermann</a>
(<a href="/recipes/tags/text/">text</a>).
Revision 2.
</p>
<p>While the find() and count() string functions can check for string occurences, there is no function to check on the occurence of a set of characters.</p>
Filter a string and only keep a given set of characters (Python)
2001-05-24T18:52:23-07:00Jürgen Hermannhttp://code.activestate.com/recipes/users/98061/http://code.activestate.com/recipes/59857-filter-a-string-and-only-keep-a-given-set-of-chara/
<p style="color: grey">
Python
recipe 59857
by <a href="/recipes/users/98061/">Jürgen Hermann</a>
(<a href="/recipes/tags/text/">text</a>).
</p>
<p>The following code creates a filtering functor for a given character set, that can then be used on any number of strings.</p>
Safely morph a file in-place (Python)
2001-05-24T03:25:36-07:00Jürgen Hermannhttp://code.activestate.com/recipes/users/98061/http://code.activestate.com/recipes/56037-safely-morph-a-file-in-place/
<p style="color: grey">
Python
recipe 56037
by <a href="/recipes/users/98061/">Jürgen Hermann</a>
(<a href="/recipes/tags/files/">files</a>).
</p>
<p>A class that enables a client to securely update an existing file,
including the ability to make an automated backup version.</p>
The Singleton Pattern implemented with Python (Python)
2001-04-05T21:21:30-07:00Jürgen Hermannhttp://code.activestate.com/recipes/users/98061/http://code.activestate.com/recipes/52558-the-singleton-pattern-implemented-with-python/
<p style="color: grey">
Python
recipe 52558
by <a href="/recipes/users/98061/">Jürgen Hermann</a>
(<a href="/recipes/tags/oop/">oop</a>).
Revision 2.
</p>
<p>The following class shows how to implement the singleton pattern[1] in Python. A singleton
is a class that makes sure only one instance of it is ever created. Typically such classes
are used to manage resources that by their very nature can only exist once.</p>
Colorize Python source using the built-in tokenizer (Python)
2001-04-06T23:05:53-07:00Jürgen Hermannhttp://code.activestate.com/recipes/users/98061/http://code.activestate.com/recipes/52298-colorize-python-source-using-the-built-in-tokenize/
<p style="color: grey">
Python
recipe 52298
by <a href="/recipes/users/98061/">Jürgen Hermann</a>
(<a href="/recipes/tags/programs/">programs</a>).
Revision 3.
</p>
<p>This code is part of MoinMoin (<a href="http://moin.sourceforge.net/" rel="nofollow">http://moin.sourceforge.net/</a>) and converts Python source code to HTML markup, rendering comments, keywords, operators, numeric and string literals in different colors.</p>
<p>It shows how to use the built-in keyword, token and tokenize modules to scan Python source code and re-emit it with no changes to its original formatting (which is the hard part).</p>
<p>The test code at the bottom of the module formats itself and launches a browser with the result.</p>
Getting a dictionary of all members of a class hierarchy (Python)
2001-03-19T16:29:45-08:00Jürgen Hermannhttp://code.activestate.com/recipes/users/98061/http://code.activestate.com/recipes/52288-getting-a-dictionary-of-all-members-of-a-class-hie/
<p style="color: grey">
Python
recipe 52288
by <a href="/recipes/users/98061/">Jürgen Hermann</a>
(<a href="/recipes/tags/oop/">oop</a>).
</p>
<p>The following function creates a map of all members of a class, navigating ALL of the base classes in the correct order. This can be used for various purposes, like checking whether a certain method is defined anywhere in a class hierarchy.</p>
Handling URLs within a CGI script (Python)
2001-03-19T15:29:32-08:00Jürgen Hermannhttp://code.activestate.com/recipes/users/98061/http://code.activestate.com/recipes/52287-handling-urls-within-a-cgi-script/
<p style="color: grey">
Python
recipe 52287
by <a href="/recipes/users/98061/">Jürgen Hermann</a>
(<a href="/recipes/tags/cgi/">cgi</a>).
</p>
<p>The following are some useful functions to build URLs within a CGI script, for example to send a HTTP redirection header (see example).</p>
Simulating the ternary operator in Python (Python)
2001-03-19T13:06:05-08:00Jürgen Hermannhttp://code.activestate.com/recipes/users/98061/http://code.activestate.com/recipes/52282-simulating-the-ternary-operator-in-python/
<p style="color: grey">
Python
recipe 52282
by <a href="/recipes/users/98061/">Jürgen Hermann</a>
(<a href="/recipes/tags/text/">text</a>).
</p>
<p>People coming from C, C++ or Perl might miss the so-called ternary operator ?: (condition ? then-expr : else-expr). It's most often used for avoiding several lines of code and temporary variables for very simple decisions, like printing the plural form of words after a counter (see example code).</p>
<p>There are two ways to get the same effect in Python: selecting one of two values from a tuple, or using the special behaviour of the "and" and "or" operators in Python. The second method has the advantage that only ONE of the two possible expressions is evaluated, and is thus more close to the behaviour of ?: as defined by C.</p>
Importing a name from a module with a computed name (Python)
2001-03-15T16:43:03-08:00Jürgen Hermannhttp://code.activestate.com/recipes/users/98061/http://code.activestate.com/recipes/52241-importing-a-name-from-a-module-with-a-computed-nam/
<p style="color: grey">
Python
recipe 52241
by <a href="/recipes/users/98061/">Jürgen Hermann</a>
(<a href="/recipes/tags/oop/">oop</a>).
</p>
<p>This function allows you to do "from module import name", where both "module" and "name" are dynamic values (i.e. expressions or variables). For example, this can be used to implement a "plugin" mechanism to extend an application by external modules that adhere to some common interface.</p>
<p>This pattern is used in MoinMoin (<a href="http://moin.sourceforge.net/" rel="nofollow">http://moin.sourceforge.net/</a>) to load extensions implementing variations of a common interface, like "action", "macro", and "formatter".</p>
Controlling XSLT stylesheet loading (Python)
2001-06-30T10:56:25-07:00Jürgen Hermannhttp://code.activestate.com/recipes/users/98061/http://code.activestate.com/recipes/52242-controlling-xslt-stylesheet-loading/
<p style="color: grey">
Python
recipe 52242
by <a href="/recipes/users/98061/">Jürgen Hermann</a>
(<a href="/recipes/tags/xml/">xml</a>).
Revision 6.
</p>
<p>This code shows how to load a stylesheet from "virtual" URLs, by detecting a special URL scheme.</p>