Python Programming, news on the Voidspace Python Projects and all things techie.
Movable Python for Python 2.5 Final
Movable Python is now available for Python 2.5 Final.
Updated distributions have been uploaded for both Movable Python 2.5 and the Mega-Pack.
These are available for download from the usual place :
As Python 2.5 is now out, the price for Movable Python 2.5 and the mega-pack will both go up by one pound on Monday. This really is your last chance...
The Great Voidspace Computer Sale
Well, not exactly, but I've finally got round to selling a couple of unneeded items on ebay. The selling procedure was pleasantly straightforward. In order to retain PayPal seller protection (and minimise the possibility of fraud) I can only sell to buyers in the UK, and only send to a registered PayPal address.
A genuine copy of Windows XP, never used and with a Certificate of Authenticity.
This is a lovely handheld computer which is a cross between a laptop and a PDA.
It runs Windows CE, but has a 70% sized keyboard and a 640x480 screen resolution. It also has a built in modem and 32mb memory.
It also comes with a host of accessories, like Clik drive and leather case. Great for taking notes or carrying around documents.
Sometimes there is a legitimate reason to send the same email to lots of people. In my case it was to let those who have paid for Movable Python know that a new version was available.
The following little script takes a ReStructured Text source file (email.txt) and renders it into HTML. It then inserts the HTML into a template (template.html) (which has the ReST CSS embedded in it).
The resulting HTML and the text source are then turned into an email, using the createhtmlmail and mailme functions from cgiutils.
The email addresses are taken from a text list, with lines in the format Mr Someone firstname.lastname@example.org.
from docutils import core, examples
from cgiutils import createhtmlmail, mailme
email_body = 'email.txt'
email_list = 'list.txt'
done = 'done.txt'
template = 'template.html'
from_email = 'email@example.com'
subject = 'New Release of Movable Python (and the Mega-Pack)'
server = 'smtp.example.com'
username = None
password = None
Currently just a wrapper round html_parts.
We set initial_header_level to 2 rather than 1
We also set doctitle to 0
encoding = 'cp1252'
if not text.startswith('\n'):
text = '\n' + text
# Would be nice to add
# 'toc_backlinks : 0'
# to the overrides dictionary
parts = examples.html_parts(text, initial_header_level=2, doctitle=0,
# returns string not unicode
# assuming encoding is valid :-)
email_txt = open(email_body).read()
template_html = open(template).read()
the_list = [l.strip() for l in open(email_list)]
done_list = 
done_list = [l.strip() for l in open(done)]
line = the_list.pop().strip().replace('\t', ' ')
if not line:
if ' ' in line:
name, email = line.rsplit(' ', 1)
name = line.split('@')
email = line
if email in done_list:
print count, name, email
txt_msg = email_txt % name
html = template_html % (subject, subject, rest(txt_msg))
msg = createhtmlmail(subject, html, txt_msg)
to_email = [email]
mailme(to_email, msg, subject, from_email, host=server, username=username,
if __name__ == '__main__':
It prints the email addresses that it sends to, and keeps a track of those it has sent in done.txt. It is obviously dependent on docutils.
It also inserts the name into the text, and the name and subject into the html template.
Oh, and don't worry. This has to be the least efficient spam tool there is.
Decorators that Take Arguments
I'd not really used decorators until I started work at Resolver Systems. For most of my projects I wanted to retain Python 2.3 compatibility, which ruled out decorators.
At Resolver, we use IronPython; where the base version is Python 2.4. That means I had an excuse to explore decorators, set as a built in, generator expressions and all the other new features that Python 2.4 brought in.
Decorators are straightforward (and after all the wrangling, I actually like the syntax). I've seen examples of decorators that take arguments, and today I needed to write one.
It turns out that you use closures to implement decorators that take arguments. So you write a function, that returns a decorator that takes a function.
Another way of saying the same thing, is that their is no syntax support for decorators that take arguments.
Take this simple example :
Here, 'decorator' is called with 'arg1' and 'arg2'. The return value is used to decorate 'function'.
With PLY you use docstrings to decorate token functions with regular expressions that define that token. (Tokens defined as functions have a higher precedence than tokens defined as strings.) We build these regular expressions from some standard, ones.
So we need to poke docstrings onto our token functions, using code like this (which is kind of ugly) :
t_TOKEN.__doc__ = r'SOMETHING' + WHITESPACE
So instead we came up with a decorator, which doesn't save any lines of code but looks nicer :
f.__doc__ = token
@TokenRule(r'SOMETHING' + WHITESPACE)
def t_TOKEN(t): return t
sigh another simple blog entry which went into too much detail and took too much time...
|||And feel free to point out if it is, but I failed to find it.|
|||Including an example of a class decorator !?|
IronPython Samples & Pyc the Compiler
There are now several sets of IronPython Samples available for download. These are sets of examples, showing how to use various aspects of IronPython and related technologies.
So far the samples available include (available from the download page :
This tutorial, written by Martin Schray, consists of a set of IronPython programs that show how to use the Windows controls found within System.Windows.Forms. These IronPython tutorial programs vary greatly in functionality and include:
- Several “Hello World” applications showing how to leverage core Windows forms classes
- An application with an embedded web browser
- A small application based on Windows MapPoint Web Services that provides a map, given a location
- Another application based on MapPoint that lets the user get a route map given start and end points
This utility written by Jocelyn Turcotte is an IronPython program that leverages Windows Presentation Foundation (WPF) and System.Windows.Forms to create a handy utility that graphically displays disk usage information.
This sample written by Ravi Chugh is an IronPython program that builds on top of System.Windows.Forms to create a fun slide puzzle game using maps
This sample, written by Matt Gruskin, is a frequency modulation synthesizer that uses DirectX and System.Windows.Forms.
This sample, written by Brad Dodson, is a music player written in IronPython that uses DirectShow, Windows Presentation Foundation, and COM Interop.
By utilizing managed PowerShell API(s) from IronPython, it becomes downright easy to perform a runtime inspection of the system. As an example, you can determine how much memory your video card has installed on it using only a couple lines of IronPython code. To make working with PowerShell even easier, the “powershell.py” module (included with this sample) allows you to run PowerShell cmdlets in a very elegant and object-oriented manner.
IronPython’s Hosting APIs can be used to compile Python scripts into DLLs, console executables, or Windows executables. The pyc.py script included in this tutorial leverages these hosting APIs and can be used to compile other Python scripts. It provides a variety of flags such as the ability to specify the target platform of the .NET assembly (e.g., x64).
This sample, written by Lee Culver, provides a step-by-step, comprehensive tutorial on creating graphical applications using managed DirectX API(s) from IronPython. It will teach you how to:
- Load and render DirectX meshes
- Position, rotate, and move objects
- Auto-track objects with cameras
Of particular interest to me is the Pyc sample. It shows how to use the built-in compiler features of IronPython to create executables and dlls . These will be dependent on the IronPython dlls, but make a great way to distribute applications. In fact embedding the IronPython engine in C#, and creating your own custom executable is also easy. But that's another story.
Here's the full low-down from the readme :
Python Command Line Compiler
The prerequisites to successfully run this application are:
- Microsoft .NET Framework Version 2.0 Redistributable Package (x86)
- IronPython 1.0 distribution
Navigate to the Pyc sample directory and copy the IronPython DLLs (IronPython.dll and IronMath.dll) here. The compiled executables require the presence of IronPython.dll and IronMath.dll in the current directory. If not found by the .NET runtime, the compiled executables will fail with "System.IO.FileNotFoundException: Could not load file or assembly 'IronPython, Version, ...".
Please make sure the folder structure in the Pyc directory is:
.\console_hw.py .\IronMath.dll .\IronPython.dll .\other_hw.py .\pyc.py .\readme.htm .\winforms_hw.py
This sample demonstrates the use of the Python Hosting APIs to compile Python files into a .NET executable. The behavior is essentially a command line equivalent of the IronPython Visual Studio integration sample released with the Visual Studio SDK. (The complete Visual Studio SDK integration sample can be downloaded from here.)
For the list of command line options, run:
Command Line Options
The format of the command line for running pyc.py is as follows:
ipy.exe pyc.py [options] file [file ...]
The available options are:
/? or /h
Command line help
The name of the output file. If omitted, the name of the last input file specified (with extension dll or exe) will be used for the output file.
The main module of the executable. This is the module whose code will be executed first in the compiled executable. If /out is omitted, the name of the output assembly will be: main_file.<extension>
The target assembly type. DLL, console executable (exe), or windows executable (winexe). Default is "exe".
/r:assembly or /reference:assembly
Adds a reference to the .NET assembly to the compiled code. This allows the Python code to reference .NET assemblies without ever using the "AddReference" function of the clr module. Example: /r:System.Windows.Forms
Optional argument to specify/restrict the target platform of the .NET assembly. Specifying x86 will set assembly requirement to 32bit execution only. Default setting is to produce a platform independent .NET assembly.
Include public or private resource file (*.resource) in the assembly. The resource type can be set to public or private with the optional modifier following a comma.
ipy.exe pyc.py /main:console_hw.py
Compiles console_hw.py into console executable console_hw.exe.
ipy.exe pyc.py other_hw.py /main:console_hw.py
Compiles two input files (other_hw.py and console_hw.py) into console_hw.exe. Contents of console_hw.py will be executed directly.
ipy.exe pyc.py /main:winforms_hw.py /r:System.Windows.Forms /target:winexe
Compiles winforms_hw.py into winforms_hw.exe and includes a reference to System.Windows.Forms. winforms_hw.py can then include code such as:
import System.Windows.Forms form = System.Windows.Forms.Form(Text = "Hello") form.ShowDialog()
The compiled executable requires the presence of IronPython.dll and IronMath.dll in the same directory from which the compiled assembly is being executed. If IronPython.dll is not present in the current directory, the following exception will be raised:
Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'IronPython, ..."
|||While the assemblies produced by the IronPython Hosting APIs are true .NET assemblies, the dynamic nature of the Python language makes it difficult to use these from other .NET languages. In short, this means that attempting to import Python types into other .NET languages such as C# is not recommended.|
Upgrade to the Mega-Pack
A few days ago I launched the Movable Python Mega-Pack.
This bundles four Python versions of CPython, plus IronPython into a single package. It lets you choose which interpreter to launch programs with, and you can launch interactive interpreter sessions for any of the versions of Python.
This is particularly useful for testing programs with multiple versions of Python. It costs £11.99 (less than twenty five dollars) for the Mega-Pack 
For users who have already paid for a distribution of Movable Python, you can get a discount on the Mega-Pack of the full price of the distribution you have already bought.
Alternatively, you can buy a distribution of Movable Python and later update to the Mega-Pack with the same deal.
|||But now that Python 2.5 is out, this will rise to £12.99 in a couple of days.|
Language Wars, Passion and IQ
It suggests that brilliance, whether academic or in music or sport or anything else, is not innate. Instead it is partly based on support and environment in the early years, but mainly on plain hard work.
This is a reassuring message for most of us, who are not yet geniuses, by applying the 99% perspiration we have lacked.
Apparently the majority of real achievers in almost any field have an only moderately high IQ. Conversely, those who have an exceptionally high IQ tend to only live moderately achieving lives.
The article concludes that almost anyone can become a real achiever, a genius in their field, if they are prepared to put in the hard work.
This sounds at least partly true, but I don't think its the whole story. Personality still determines success or achievement (however you want to define success and achievement). I think the missing factor is passion. Passion is the intellectual or emotional force that drives people to achieve, and IQ is probably much less of a factor than we suppose.
It is this fact about the human soul, and the soul  is much more relevant to human endeavours than the mind, that causes language wars and the like. We get passionate about the tools of our creativity.
It's also why I find the blog Creating Passionate Users so interesting. Good software isn't just about cold rational choices...
|||The article is by David Dobbs, and although it cites 'studies', a quick scan of the article didn't reveal any specific references.|
|||Perhaps a controversial word, but a word that encompasses human emotions and subconscious drives as well as the rational mind.|
Movable Python Mega-Pack
One of the most important new features of Movable Python is that it can work with multiple interpreters. That can be other distributions of Movable Python, an installed version of Python, IronPython, or in fact any executable (or batch file or a Python script) .
If you have multiple Movable Python distributions, you can test programs with any version of Python from a single interface. You can also use the same interface to launch interactive interpreter sessions for any of the interpreters.
The Movable Python Mega-Pack comes with all four distributions of Movable Python (Python 2.2, 2.3, 2.4 and 2.5). It also comes with IronPython 1.0 Final. It is pre-configured with these interpreters (the default is Python 2.4).
The Movable Python Mega-Pack costs £11.99 , around the cost of two and a half distributions. You also get a couple of freebies. You can obtain the Movable Python Mega-Pack from :
Nanagram the anagram maker is a fun program for making anagrams. It's especially fun to make anagrams of your friends names.
This normally costs £5 for the full version.
The other freebie is MovablePython.NET. This is an experimental build of Movable Python, written in C#. It isn't a stand-alone interpreter, but a GUI launcher.
It lacks many of the features of the 'standard' Movable Python, but has a nicer GUI and a few extra features.
These include :
- Auto detecting any installed Python
- Assigning system wide hot keys to scripts
- Drag and drop
This version will probably provide the basis of a GUI update for the next version of Movable Python. If you purchase the Mega-Pack, you have full access to all the individual distributions (including free updates for a year), Nanagram and MovablePython.NET and a single package containing all the distributions. This is pre-configured, and weighs in at 80mb.
|||So Movable Python can act as a GUI for any program, including passing command line arguments. You may never need to use the command line again.|
|||Once Python 2.5 final is released, this will also be raised: so be quick.|
Movable Python 2.0.0 Beta 1
There is a new version of Movable Python available.
This is available for Movable Python for Python 2.2.3, 2.3.5, 2.4.3 and 2.5rc2 from :
The new release includes several minor bugfixes and new features :
'Console normal' didn't work for the Python 2.5 distribution (and possibly others, although it seemed to work with Movable Python for Python 2.4). This is now fixed.
Added console handling for IronPython (if the interpreter is 'ipy.exe' or 'ipyw.exe' then the appropriate one will be used when 'Console handling Normal' is selected.)
The IPOFF (IPython off) option wasn't acknowledged when launching the interactive interpreter from the GUI.
A bug in py2exe 0.6.5 (an incompatibility with Python 2.5) would cause the interactive interpreter to exit after an exception, and possibly other problems. This is fixed.
Got rid of the error dialog in movpyw and the 'movpyw.exe.log' file.
Changed logging to 'Log Mode' in main menu.
Fixed configuration for IDE for Python 2.5 version.
Movable Python now exits with the same exit code as any script it runs.
Improvements in traceback handling.
It's still not too late to buy Movable python for Python 2.5 at the reduced price, before the release of Python 2.5 final.
There are still some issues with Pythonwin to be fixed, and the documentation update, before Movable Python 2.0.0 Final can be released.
This work is licensed under a Creative Commons Attribution-Share Alike 2.0 License.