Python Programming, news on the Voidspace Python Projects and all things techie.

Anonymous Code Blocks

emoticon:lighton A while ago I promised you that I would implement an approximation of Ruby's anonymous code blocks, by transforming bytecodes. Smile

It's really an exercise in experimenting with Python code objects, bytecode and the Python scoping rules. It uses the excellent Byteplay module and results in a function called AnonymousCodeBlock. This can take a function and return a code object representing the function body. Local variables used by the function you passed in, and names bound by it, will be looked up and bound in the scope in which you execute the code.

Like this post? Digg it or it.

Posted by Fuzzyman on 2006-12-15 13:38:20 | |

Categories: ,

Akismet 0.1.4

emoticon:baldguy I've just updated my Python Akismet Module.

Akismet is a web service for recognising spam comments. They say that currently 81% of all comments submitted to them are spam.

The Akismet class provides methods for verifying your API key, checking comments against spam, and telling Akismet that a comment is ham or spam.

This is version 0.1.4.

Akismet now traps errors in connections. If there is a network error it raises an AkismetError.

This can happen when the Akismet service or domain goes down temporarily.

Your code should trap this and handle it appropriately (either let the comment through or push it onto a moderation queue).

Like this post? Digg it or it.

Posted by Fuzzyman on 2006-12-13 23:27:02 | |

Categories: ,

IronPython 1.1 Alpha

emoticon:speaker IronPython 1.1 Alpha has just been released. The team are still working hard fixing new bugs.

In the announcement email they also revealed that they already have a Version 2 branch in their SCC.

Important features of the new release (along with a host of bugfixes) include :

  • Support for Python 2.5 unified try/except/finally
  • Support for Python 2.5 yield from finally block
  • help supports documentation on reflected fields, properties, and events
  • Nt module implements spawnle, spawnv, spawnve functions
  • Socket implements makefile function
  • Support for picking exception instances
  • Implement select module
  • Implement sha module
  • Implement md5 module

In other surprising news [1], Guido van Rossum just commented that YouTube is almost entirely written in Python. Wow !

[1]In a Python-Dev thread about why the number of Windows users downloading Python seems to have jumped dramatically: both in absolute terms and as a proportion of total downloads.

Like this post? Digg it or it.

Posted by Fuzzyman on 2006-12-13 20:49:05 | |

Categories: ,

PyCon: IronPython & Windows Forms Talk

emoticon:cat Andrzej and I have been thinking about our PyCon talk. We're trying to focus the talk so that we can deliver as much information as possible in half hour.

The two aims of the talk are:

  • To show how easy it is to use .NET classes with IronPython
  • To demonstrate how good Windows Forms is as a GUI API

The proposal (so far) follows. It is slightly different from the description on the PyCon site and may change more.

This talk (half an hour including questions, with slides available) will provide a very brief overview of IronPython itself, followed by a demonstration of programming with IronPython and Windows Forms.

The talk assumes a basic familiarity with Python and working with GUI frameworks. The actual level of Python used in the talk will be very simple, so medium level programmers with experience of other languages should be able to follow.

The .NET integration offered by IronPython means that using .NET classes from IronPython is extremely easy. The talk will demonstrate the windows forms application loop, creating controls and adding event handlers. This illustrates how to create an IronPython application, instantiating .NET classes and using them from IronPython. It also shows how to use delegates and demonstrates how IronPython types are also .NET types.

In demonstrating this we wills show how to read the MSDN documentation from an IronPython point of view. Equipped with this knowledge (and google) the IronPython developer is able to use any .NET classes. Smile

Subjects covered will include :

  • What is IronPython (a very brief overview)
  • Why use IronPython (and when not to use it)
  • An overview of .NET including Windows Forms [1]
  • The clr module (loading assemblies)
  • The application loop (mentioning the .NET threading model)
  • The Form, reading from the MSDN docs
  • Adding controls (using .NET classes)
  • Setting attributes (IronPython types are .NET types)
  • Handling events
  • Basic GUI layout (dockstyles and anchor, the Z-order, etc)
  • A simple example application [2]
  • Questions

If there is time it may be possible to demonstrate extending IronPython from C#. (This only takes a couple of minutes but it is likely that there won't be time in thirty minutes.)

It would also be nice to cover some of the cases where extra information (beyond normal Python experience and common sense) is needed, like out and ref parameters, the syntax for multi-dimensional array access, instantiating typed arrays and using overloaded methods. We'll see.

[1]Mentioning System.Xml, System.Management and other common assemblies.
[2]Splitter container, ListBox and TabControl plus TabPages, ToolBar, Menus and Context Menus: all in amazingly little code. Smile

Like this post? Digg it or it.

Posted by Fuzzyman on 2006-12-13 00:04:07 | |

Categories: ,

Robots and VoIP

emoticon:dove My boss has recently been blogging again. (I've tried telling him not to, but he pays no attention.)

He's decided that the best way of getting us all direct-to-desk phones is to build a VoIP system using Asterisk. In order to avoid embarrassing experiments with a live work system, he's building one at home first. He's now done two blog entries covering the hardware and software he's using.

He's also been experimenting with building robots, inspired by the Make Magazine:

I hope these aren't for work too, things are scary enough round here...

Like this post? Digg it or it.

Posted by Fuzzyman on 2006-12-12 16:45:44 | |

Categories: , ,

Anthony, Bruce and IronPython

emoticon:Lithium I recently ran Bruce, a presentation tool that uses Pygame, whilst looking for presentation tools for PyCon.

It is great. It produces fantastic looking presentations, including embedding an interpreter into the presentation so that you can show live examples.

Anthony Baxter has just done some presentations as part of the OSDC. His second talk was called Future Python on IronPython and Python 3000. His new blog has details of an IronPython port of Bruce called IronKant [1] which he used to do the presentation. IronKant uses SdlDotNet instead of PyGame.

His demo was done using Linux and Mono. This means that it doesn't run 'out of the box' on Windows.

With a little bit of hacking I got it working.

Here's how:

  • First of all, if you have two monitors disable one. Having two monitors screws up the demo in various ways.

  • You will need IronPython of course, you will also need Python 2.4 installed.

  • I recommend adding the directory containing ipy.exe to your PATH.

  • Set the environment variable IRONPYTHONPATH with the location of your Python 2.4 library. If Python 2.4 is installed in the default location this will be C:\Python24\Lib.

    To set the environment variable at the command line (for that session only), type set IRONPYTHONPATH=C:\Python24\Lib.

    To set a global environment variable (user or system), follow the instructions at setting environment variables.

  • Checkout IronKant / futurepython from Anthony's repository. TortoiseSVN makes this easy. The location of the IronKant repository is

  • You will need to replace all references to /usr/local/share/fonts/TrueType/ with C:\WINDOWS\Fonts\.

    There are three in test.kant. (Replacing these may be optional.)

    There are five in kant/

  • You will need to modify kant/

    Line 15 is (currently) os.spawnl(True, "/bin/sh", "-c", self.command).

    Change it to os.spawnl(True, "ipy", self.command). (This is why you need ipy.exe on your PATH.)

    You could also change it to os.spawnl(True,"cmd", "/c", "ipy", self.command) which more closely matches the original, but doesn't seem to make any difference. Smile

  • Download SdlDotNet. From the distribution zip file I put the contents of the bin/assembles/ and bin/win32deps/ directories into the 'futurepython' directory.

  • From the command line you can now run: ipy futurepy.kant.

Nice presentation. Smile

It does leave me with a dilemma though. I had settled on using S5 / Docutils [2] which produces great web ready slideshows. Hmmm...

[1]He says this name is in the 'naming tradition' of Bruce. I don't get it.
[2]For our IronPython talk at PyCon.

Like this post? Digg it or it.

Posted by Fuzzyman on 2006-12-10 21:36:42 | |

Categories: , ,

Windows Mobile 5.0 Console

emoticon:pen_book News thanks to a gentleman called Bandung. It is now possible to load and run Microsoft's console, as well as Simbolic's PocketConsole, and use them with PythonCE.

Here are the steps :

  1. Get and install the Pocket Console
  2. Get and install the Pocket CMD
  3. You can also install Microsoft's cmd program in parallel with the Open Source one. There is a pocket cmd console buried within PowerToys 2003. I tried to install it but it didn't work properly. If you can extract the following three files from the CAB then you can get it working. All you need is the cmd.exe and shell.exe files placed anywhere in one directory. Then use the console.dll that symbolic installs within the Windows directory rather than the Microsoft console.dll and you will be able to run Microsoft's cmd dos utility.
I have a stand alone install for Microsoft's Cmd utility version 4.2 as well as the older one called 3.0 These work quite well.
  1. The most important step! Change the following registry entry:


    set this value to 0

What is beautiful about this set of programs is that I can now launch other apps from a dos console. I am able to launch the pocket ssh port created by Rainer (it supports tunnelling) and other apps that have command line arguments and switches.

For example:

ssh  -l bandung -L 25:

When calling Python, it needs to have the exact path given, the relative path doesn't work. So if I enter the following:

\> python \

Then it works! What this means is I can now write bat files which call various python modules. I can also execute python modules from the cmd console by typing

\>    python \path\sample\

Like this post? Digg it or it.

Posted by Fuzzyman on 2006-12-09 22:47:15 | |

Categories: ,

ConfigObj Changes in Subversion

emoticon:men ConfigObj is finally getting some attention.

Nicola Larosa has just checked some changes to ConfigObj, the docs and the tests :

Changes (so far) include :

  • The tests now all pass for versions of Python from 2.2 to 2.5.
  • String interpolation will now search the current section before looking in the DEFAULT sections.
  • Added Template-style interpolation (substitution).
  • Tests for the new features.

Many thanks to Robin Munn for the patches he provided and to Nicola for his hard work.

There are a couple of other changes to go. After this there will be a new release of ConfigObj and Pythonutils.

Like this post? Digg it or it.

Posted by Fuzzyman on 2006-12-09 17:42:39 | |

Categories: ,

Turbogears and Movable Python

emoticon:movpy2 Mickey Hadick has been using Turbogears with Movable Python.

This is now the second time that Mickey has brought Movable Python and TurboGears together. He mentions them both in a short film, which he is hoping will be selected for the East Lansing Film Festival.

This entry charts how he got Turbogears working with Movable Python.

I used the Movable Python 2.0.0 beta 2 for Python 2.4.4 for this exercise. I ended up installing TG's 1.0b2 version.

The first trick is getting easy_install installed. I downloaded the script to my Movable Python's lib subdirectory, and ran it using the Movable Python GUI. I specified the installation directory with the --install-dir parameter, as follows...


This installed the setuptools egg in the lib directory. I tried to run the new utility from TurboGears, and which I had copied into the movpylib directory, but it failed, saying it couldn't find That module happens to be in the setuptools egg directory, so I added an entry for that ("setuptools-0.6c3-py2.4.egg") into the syspaths.pth file in the lib directory.

I then executed I again used...

--install-dir=\movpy-2.0.0beta2-py2.4\movpy\lib --verbose

arguments, though without being entirely sure it would work. It did.

However, running tg-admin fails, reporting that it can't find the sqlobject module. Fine and dandy. It just so happens that easy_install updates its own "easy-install.pth" file with a reference to every egg just installed. So I copied all of those entries into the "syspaths.pth" file. There may be a more clever way to do this, but this way worked.

Then I used the Movable Python GUI to select, and, providing the quickstart parameter, it prompted for all the project settings, and created the TurboGears project structure. It pauses during this, and the first few times I had tried this I thought it failed; but, hitting enter (in desperation) the script continues and builds the project.

I (finally) used the Movable Python GUI to start that very same project bootstrap script, and it served up the default website on port 8080. It is important to select the GUI's "f" option in order to execute the script from its own directory.

Like this post? Digg it or it.

Posted by Fuzzyman on 2006-12-09 13:58:50 | |

Categories: , ,

Hosted by Webfaction