Python Snippets and Recipes
|The Voidspace Recipebook|
|Python Object Model||Method Overloading|
|Piping Objects in Python||Painless Properties|
|Anonymous Code Blocks||The Selfless Metaclass|
|Simple SSI Server|
Fetching the docstring for an arbitrary member on a random Python object sounds like an easy task. Doing it correctly however requires a surprisingly deep knowledge of the Python object model; including the differences between new and old style classes, __slots__ and the descriptor protocol.
docstrings.py is the final result of this exploration and it accompanies the two blog entries where it is
Method overloading is a language feature typically found in statically typed languages, where which version of a method can be determined at compile time by the types and number of arguments. Of course we can do it in Python, but have to determine which overload to call at runtime. This example uses a metaclass to allow you to provide multiple versions of methods with different type signatures and automatically do the runtime dispatching to call the right version.
__metaclass__ = MethodOverloading
def __init1__(self, x=int, y=int, z=int):
self.test = (x, y, z)
def __init2__(self, string=str):
self.test = string
Functional languages and programming environments like Powershell often have first class syntax for piping objects between functions. Python doesn't have this natively, but we can add it to the language using operator overloading. This code sample shows how we can implement a Cmdlet class that uses the '>>' operator for piping objects between commandlets:
listdir('.') >> notolderthan('2/3/08') >> prettyprint
The syntax for creating properties with getters and setters in Python is more verbose than other languages (like C# for example). This blog entry suggests an alternative, through a metaclass that automatically creates properties from methods prefixed with 'get_', 'set_' and 'del_'. Simply inherit from the WithProperties class to use it:
self._test = 3
print 'Getting test'
def set_test(self, value):
print 'Setting test'
self._test = value
Reducing Boilerplate with a Mixin
Python rich comparison has always annoyed me, at least having to provide all the rich comparison methods has always annoyed me.
Python Rich Comparison, is an article showing how CPython differs from IronPython in this respect. It also implements a 'rich comparison mixin' class, so that you can have all of them just by providing two methods (instead of six).
You can download the mixin class and tests here:
pathutils 0.2.6 November 8th 2007
cgiutils 0.3.5 November 26th 2005
These two modules contain functions and constants for 'general' use. They are both included as part of the Voidspace Pythonutils Package .
Pathutils contains various functions for working with files and paths. This includes (amongst other things) :
- Easy ways to iterate over files and directories
- A function to calculate a relative path from one location to another
- Several 'convenience' file reading/writing functions
- py2exe directory support
- A function for pretty printing file sizes
- A simple solution to cross-platform file locking
- Import from a specific location
See the pathutils Homepage.
CGI utils has (guess what... ) various functions for use with CGI. It contains functions to do things that every CGI has to cope with. They are most useful for simple scripts, where a full blown framework would be too cumbersome.
It includes (amongst other things) :
- Functions for receiving form submissions
- Cross platform functions to send emails (including creating HTML emails)
- A basic templating system
- A DNS blacklist lookup
- A function to generate a menu line for pages with lots of results
See the cgiutils Homepage.
Download them here :
Exploring Code Objects and Bytecode
AnonymousCodeBlock is a function which transforms the body of a function into a code object which can be executed in the current scope, rather than creating a new scope.
This is a an approximation of Ruby's anonymous code blocks for Python.
It was created as part of the article Anonymous Code Blocks in Python, which is an exploration of code objects, byte-code and the Python scoping rules.
If you're interested in Python byte-code and the Python VM, this article is a good introduction to the subject. You will need the BytePlay Module to follow the article or use the AnonymousCodeBlock function.
Metaclasses have a reputation for being deep Python black magic. Actually you can understand the basics of them, and use them, quite straightforwardly. My article Metaclasses Made Easy explains the principles and takes you through a couple of simple metaclasses.
It ends up with a Metaclass factory function, which can automatically decorate every method in a class. This could be useful for profiling for example.
The actual example shown is used to remove the need to explicitly declare self. This is a silly use, but it can be adapted for other purposes (and the MetaClassFactory can be used directly).
If you want to use the Selfless example, you will need the Byteplay module again.
Responding to Error 401
Version 1.0.1 3rd December 2004
This code is actually a tutorial on http BASIC authentication. It demonstrates what basic authentication is, and shows two ways of handling it from python code. It is also a nice example of using urllib2. The code fills in quite a few gaps in the urllib2 documentation, particularly on handling errors.
Download it here :
This piece of example code has also been made into an article over at Article on Basic Authentication.
Handling Cookies When Fetching Web Pages
Version 1.0.2 14th February 2005
cookielib is a module new to Python 2.4. It is for client side handling of cookies when making requests for web pages (using urllib2).
Prior to Python 2.4 it existed as an extension module called ClientCookie. cookielib isn't a drop in replacement for ClientCookie though. cookielib is easier to use because it works directly with urllib2. This example demonstrates using cookielib and ClientCookie. It also shows code, that will work with either cookielib or ClientCookie (or even neither). This is useful if your script might be used on machines with different versions of Python.
cookielib itself will work with urllib2 to fetch webpages for you. Using CookieJar instances it will save cookies sent with the pages you fetch. Further fetches will result in the cookies being handled in the same way a browser does. The cookies can even be saved between sessions. For fetching pages from some servers, proper cookie handling is essential. cookielib makes this basically transparent to the programmer. This code acts as a useful intro to the ClientCookie module, which at the time of writing had gaps in the documentation regarding the LWPCookieJar class used to save cookies.
Download it here :
This piece of example code has also been made into an article over at Article on cookielib and ClientCookie.
A Simple Server with SSI Processing
Version 0.4.1 18th June 2009
This is another simple server implementation, this one based on CGIHTTPServer. It does everything that CGIHTTPServer does, with some SSI processing as well. So far it handles include and flastmod, but adding additional instructions would be easy.
Simply drop it in a directory and it will serve webpages, with that directory as the root directory. Any pages that end in .shtml or .shtm, it does SSI processing on.
It can serve CGI scripts from locations with spaces in the path, and can also serve CGI scripts from subdirectories of the cgi-bin folder (both of these features only work on Windows I'm afraid).
As another added bonus it can serve files from two locations. This means you can keep a single folder with your latest edits in, and have it check that folder first. It can also run in proxy mode. This is useful for offline testing - where you want it to ignore any requests except those for the localhost domain.
Download it here :
For buying techie books, science fiction, computer hardware or the latest gadgets: visit The Voidspace Amazon Store.
Last edited Tue Aug 2 00:51:34 2011.