Python Programming, news on the Voidspace Python Projects and all things techie.
Anonymous Code Blocks
A while ago I promised you that I would implement an approximation of Ruby's anonymous code blocks, by transforming bytecodes.
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.
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).
IronPython 1.1 Alpha
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 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.|
PyCon: IronPython & Windows Forms Talk
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.
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 
- 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 
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.
|||Mentioning System.Xml, System.Management and other common assemblies.|
|||Splitter container, ListBox and TabControl plus TabPages, ToolBar, Menus and Context Menus: all in amazingly little code.|
Robots and VoIP
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...
Anthony, Bruce and IronPython
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  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.
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 codingweasel.googlecode.com/svn/trunk/talks/2006/futurepython.
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/config.py.
You will need to modify kant/spawnpage.py.
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.
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 ironkant.py futurepy.kant.
|||He says this name is in the 'naming tradition' of Bruce. I don't get it.|
|||For our IronPython talk at PyCon.|
Windows Mobile 5.0 Console
Here are the steps :
- Get and install the Pocket Console
- Get and install the Pocket CMD
- 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.
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.
ssh mysite.com -l bandung -L 25:127.0.0.1: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 \sample.py
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\module.py
ConfigObj Changes in Subversion
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.
Turbogears and 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 ezsetup.py 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 tgsetup.py utility from TurboGears, and which I had copied into the movpylib directory, but it failed, saying it couldn't find pkg_resources.py. 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 tgsetup.py. I again used...
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 tg-admin-script.py, 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.
This work is licensed under a Creative Commons Attribution-Share Alike 2.0 License.