Saturday, August 29, 2009

apple broke a lot of its developer reference links... again

Was just reading a link on the apple developer reference, and sent a link to someone to read...

Then I noticed that the link no longer worked... and instead was redirected by the apple website to the generic front page of their developer reference.

I wish someone would point them to Cool urls don't change, and help them understand the web.

This isn't the first time they've broken their reference urls... the web is full of old, bad links to their documentation.

They've updated their online documentation... however they haven't kept the old urls around.

stab. un-cool.

Wednesday, August 26, 2009

ipython and doc tests. Cutting and pasting doctests into your shell.

Doc tests are a kind of literate programming for python. Where you type tests like you're typing stuff into an interpreter. In fact, it's easy to just type stuff into the interpreter and once you've finished playing around, copy the results from your terminal into a test file. This is a great, fairly low effort way of writing tests.

However, what if you have written them in your source code file, and not in the interpreter? Ok, so you've written some doc tests and you want to paste doc tests into your interpreter?

With the normal python interpreter you can't paste doc tests in(I don't think).

However the advanced ipython interpreter can. It has a special mode from writing doc tests. The command is called '%doctest_mode'.

The %doctest_mode command allows you to paste in doc tests, into the interpreter... and have them run. The normal python shell fails at this, as the '>>>' is not valid python syntax. The ipython ignores the '>>>' and the '...' which are normally used for doc tests.

It also changes the ipython prompt to look like the normal python prompt. This is useful for when you're writing doc tests. As the '>>>' is required for doc test.

The other useful command with ipython, to extract the code from your ipython session, to paste back into a file, is the %history command. Using '%history -tn 100' will show you the last 100 lines without any prompt. So then you can just copy/paste your shell session into a text editor.

ipython and %doctest_mode makes writing and reading doctests possible in your shell and your text editor. It makes it a two way process, not a one way process. You can go from your editor into your interpreter... or the other way around from your interpreter into your editor.

The other major use case %doctest_mode allows is to paste examples from documentation, into your interpreter. This is one of the main use cases with doc tests... but the normal python interpreter fails at this. So much documentation uses doc tests these days... so making it easy for people to actually paste that code in is very useful.

Double cheek kiss for ipython!

Monday, August 24, 2009

cherrypy file upload

A common thing to do with web applications is to upload files. People want to upload their pdfs, upload their images, their word documents etc.

With cherrypy you can upload files using a few different ways...

This method for uploading files with cherrypy seems a pretty good way *1.

This tool lets you upload a file directly to disk... which is handy if you're uploading a very large file. This means the webserver doesn't need to write it all into memory, and that it doesn't need to copy the file when it's finished uploading.

*1 However there are some security, and usability issues with this cherrypy tool(as currently written). Can you spot what they are?

Saturday, August 22, 2009

zodb cherrypy and lxml - simple CRUD and OO database with python.

Below I have combined three python libraries to let you do a free-form CRUD document oriented database with a webserver. Where you can just pass it most any text based html form and it will be able CReate, Update, and Delete pages.

In the post I made about cherrypy + zodb and python I combined the best python object database 'zodb' with arguably the best python webserver 'cherrypy', to create a very simple key/value datastore. This time I've combined it with lxml, to give you a very simple free form CRUD document web application.

lxml is an xml library with a number of interesting features. Including a form filling library.

Below is a script which uses 'lxml' to fill in a custom form - specified in normal html - with data you have. lxml has many html/xml related stuff in it... including a css library, and is very fast. Well worth checking out if you need to do anything webby.

You can define your html form, and it'll save whatever fields you have into the database. Then you can edit what you saved later. I guess that's the nice thing here... just define your form, and it saves data as free form objects.

All this in very few lines... these are some nice python libraries.

Note: unfortunately blogger stuffs up the code formatting... so you can download the code here:

import cherrypy
import lxml.html
from ZODB import FileStorage, DB

import cgi, os, transaction

page_form = """

<form action='savepage' method='post'>
title:<input type='text' name='title'>
<input type='hidden' name='id'>
<textarea name='content'></textarea>
<input type='submit' name='submit'></form>


class MyZODB(object):
def __init__(self, path): = FileStorage.FileStorage(path)
self.db = DB(
self.connection =
self.dbroot = self.connection.root()

def close(self):

def ehtml(s):
return cgi.escape(s)

class HelloWorld(object):

def __init__(self):


def opendb(self):
self.db = MyZODB('./Data.fs')

def index(self):
# list everything passed, and allow adding more.
for k in sorted(self.get_pages()):
ek = ehtml(k)
r += "<a href="showpage?id=%s">%s</a>" % (ek,ek)

ek = ehtml( str( int(k)+1 ) )
r += "<a href="showpage?id=%s">create new page</a>" % (ek)

return r

def get_pages(self):
dbroot = self.db.dbroot
if not dbroot.has_key('pages'):
dbroot['pages'] = {}

from BTrees.OOBTree import OOBTree
dbroot['pages'] = OOBTree()
return dbroot['pages']

def savepage(self, **kwargs):
dbroot = self.db.dbroot
self.get_pages()[kwargs['id']] = kwargs

return "page saved <a href="index">index</a>"

def commit(self):
self.get_pages()._p_changed = True

def showpage(self, id):

dbroot = self.db.dbroot

p = self.get_pages().get(id, None)
if p is None:
p = dict(id=id, title='a', content='w', submit='submit')

form_page = lxml.html.fromstring(page_form)
form = form_page.forms[0]
form.fields = p
return lxml.html.tostring(form)

def put(self, **kwargs):
# store all the args passed into the zodb.
dbroot = self.db.dbroot
transaction.commit() = True = True = True = True

if __name__ == "__main__":

cherrypy.config.update({"engine.autoreload_on" : False})

a = HelloWorld()

Monday, August 17, 2009


So, here I am in London - having moved from Melbourne.

I've visited here a few times, but this time I'm going to live in London for a while. Maybe I'll like it and stay, perhaps I'll move on. Whatever the case I'll be living in London for a while.

Looking forward to meeting London people interested in art, programing, python, graphics, image processing, sound, music, video, games, multi media etc. I'll be at the 'August London Pyssup', Wednesday 7pm at The George.

Learning bits of the local language has been fun: in'it, cheers-ta, etc

Saturday, August 08, 2009

pyconuk unconference - september 5th.

'PyCon UK 2009 will be held on Saturday 5th September at the CBSO Centre, Birmingham.

Pycon UK 2009 is an UnConference. This means that the delegates arrange the schedule on the day. There's no submission process, and no schedule. You turn up with whatever material you wish to present, you choose which talks are given and which workshops are held on the day.'

It's only £10 if you pre-book. If you're planning on going, please pre-book now, so the organisers have a better idea of numbers.

More information can be found about the unconference at the website:

gvim cmdline OSX - macvim from the terminal

macvim doesn't seem to install a 'gvim' command. So opening files from the terminal with gvim doesn't work.

Add this to your .bash_profile
function gvim { /Applications/ -g $*; }

Then you'll need to source your shell, or open new shell tabs. Or paste that line into your current shell. Then you can open your files again with OSX gvim and the terminal.

aaaaaaah. Sigh of relief at being able to open files from the cmd line again :)

Saturday, August 01, 2009

pygame 1.9.0 released!

What's new (heaps of things since 1.8.1):

Takes the "pure" out of "Game Development"
An Open Source Community Project

Summary of changes:
- many, many fixes and improvements. The largest amount of changes has gone into this release than any other pygame release.
- bug fixes for backwards compatibility issues introduced in pygame 1.8.x series. old games like solarwolf and libraries like PGU work again.
- experimental camera webcam module (still in development).
- experimental midi module based on portmidi and pyportmidi (99% complete).
- experimental gfxdraw module based on SDL_gfx (including AA circles, textured polygons and other goodness).
- python3, and python3.1 support mostly completed. Some modules still remain to be completed - but mostly it's working.
- nokia mobile phone s60 support.
- improved OSX support (dropped pyobjc dependency, improved installer, sysfont now works on OSX).
- pygame.examples + pygame.tests included with pygame. This makes testing easier, and also makes learning pygame more fun and easy.
- cleanup of examples, and addition of new examples.
- new tools to aid in development of pygame itself, better compilation documentation.
- py2app, and py2exe support improved.

Files are up on the download page.