The Voidspace Python Modules

Welcome to the home of the voidspace python modules. These are modules and
libraries that I've developed for use with Python. They are all fully open
source, with an OSI Approved License. I hope you find them useful, and I
welcome feedback. Check out the Python Index Page for links to my Python
CGIs, programs, and smaller "recipes".

A Simple Class for Mocking in Unit Tests

Version 1.0.0 7th October 2012

mock provides a core Mock class that is intended to reduce the need to create a host of trivial stubs throughout your test suite. After performing an action, you can make assertions about which methods / attributes were used and arguments they were called with. You can also specify return values and set needed attributes in the normal way.

In Python 2.7 and 3.2 a whole bunch of improvements to unittest arrived. unittest2 is a backport of the new features (and tests)
to work with Python 2.4, 2.5, 2.6 & 3.1. The major changes include new assert methods, clean up functions, assertRaises as a context manager, new command line features, test discovery and the load_tests protocol.

Reading and Writing Config Files

Version 4.7.2 27th February 2010

Note

Note that ConfigObj is not currently under development.

ConfigObj is a simple but powerful config file reader and writer: an
ini file round tripper. It's main feature is that it is very easy to use,
with a straightforward programmer's interface and a simple syntax for config
files. It has lots of other features though. This module is used in most
Voidspace projects. See the ConfigObj Home Page for full
documentation.

Create APIs that work as decorators and as context managers

A backport of a new feature from the contextlib module in Python 3.2. contextdecorator provides a ContextDecorator class that allows you to create APIs that work as both context managers and as decorators. It includes a version of contextlib.contextmanager that uses ContextDecorator.

contextdecorator works with Python 2.4+ including Python 3. See the contextdecorator page on PyPI for full details.

A XAML writer from reStructured Text source documents

rst2xaml is a script and set of libraries for writing out XAML text markup from ReStructured Text (docutils). It includes a Pygments writer to include highlighted sourcecode (for all the myriad languages Pygments supports) in the output.

XAML is the text markup used by Windows Presentation Foundation (WPF) user interfaces, both for the desktop and for Silverlight / Moonlight web applications. rst2xaml outputs two different varieties of XAML, FlowDocument XAML for WPF and a restricted subset for Silverlight.

rst2xaml itself runs under CPython, but the generated XAML is intended for use from IronPython (or any other .NET language). There is an example IronPython script for displaying the generated XAML using a WPF FlowDocumentReader.

You'll need a Wordpress Key to use it. This script will allow you to plug akismet into any CGI script or web application. It's extremely easy to use, because the folks at akismet have implemented a nice and straightforward REST API.

Note

As of version 0.2.0 akismet.py can be used with Google AppEngine.

Here's an example of how to use it :

fromakismetimportAkismetapi=Akismet(agent='Test Script')# if apikey.txt is in place,# the key will automatically be set# or you can call ``api.setAPIKey()``#ifapi.keyisNone:print"No 'apikey.txt' file."elifnotapi.verify_key():print"The API key is invalid."else:# data should be a dictionary of values# They can all be filled in with defaults# from a CGI environmentifapi.comment_check(comment,data):print'This comment is spam.'else:print'This comment is ham.'

Prebuilt Python Binaries for Windows

As you probably know psyco is the Python
specializing compiler, created by Armin Rigo who is now working on PyPy. psyco speeds up the execution of most Python programs with a single extension module.

Development of psyco was recently taken over by Christian Tismer. Christian made many improvements to Psyco, but Windows binaries were never released. Available here are compiled binaries of psyco 2 for Windows for Python 2.4, 2.5 and 2.6.