Richard Jones' Log Richard Jones' Log: Python

Fri, 30 Jul 2010
More Cheese Shop goodness

Georg has created a plugin for Sphinx that handles all that dynamic JSON Cheese Shop stuff, and it's called sphinxcontrib-cheeseshop. It's pretty darned cool :-)

Also I made a couple of other cosmetic changes to the Cheese Shop which you may have already noticed but should be apparent from this screenshot fragment:

category: Python | permanent link
Sat, 24 Jul 2010
The dynamic Cheese Shop: JSON package information

I don't get to work on the Cheese Shop very often these days. I've been wanting to add this feature for a while though: being able to dynamically include the latest information about a package on some other website. Now I can!

On the Roundup project website I have a "download" box. People have asked that I include the version information in that box, but I'm lazy and don't want to have to update it manually when I do a release. Now the page includes this HTML:

<span id="release_info" class="note">Download:
  <a href="http://pypi.python.org/pypi/roundup">latest</a></span>
<script type="text/javascript">
 $.getJSON('http://pypi.python.org/pypi/roundup/json?callback=?', function(data) {
     h = 'Download: ' + data.info.version;
     for (var i=0, url; url=data.urls[i]; ++i) {
       h += '<br><a href="' + url.url + '">' + url.filename + '</a>';
     }
     $('#release_info').html(h);
 });
</script>

This was actually generated in Sphinx, and the magic there is:

.. raw:: html

   <span id="release_info" class="note">Download: 
     <a href="http://pypi.python.org/pypi/roundup">latest</a></span>
   <script type="text/javascript">
    $.getJSON('http://pypi.python.org/pypi/roundup/json?callback=?', function(data) {
     h = 'Download: ' + data.info.version;
     for (var i=0, url; url=data.urls[i]; ++i) {
       h += '<br><a href="' + url.url + '">' + url.filename + '</a>';
     }
     $('#release_info').html(h);
    });
   </script>

That code uses jQuery which is already included in a Sphinx build so there's nothing more to do.

On the Cheese Shop side you have the option to request the JSON for the latest release or a specific release. The following are (currently, until I do a new Roundup release) equivalent:

http://pypi.python.org/pypi/roundup/1.4.15/json
http://pypi.python.org/pypi/roundup/json

Note in the jQuery the callback guff - that's part of some JSONP thing I don't care to understand but took me the longest of all of the implementation to work around. Boo for very average documentation, guys...

category: Python | permanent link
Tue, 08 Jun 2010
PyCon Australia 2010

A reminder: registration for PyCon Australia 2010 will close on the 22nd of June - several days before the event. Register now!

We've got a great line-up of talks and keynotes. We've got two social events organised - a CodeWar the night before and the conference dinner on Saturday night.

It's only a couple of weeks away now and I'm getting fairly excited ... also having some fun working on my presentation :-)

category: Python | permanent link
Thu, 06 May 2010
Melbourne Python Users Group

The next meeting of the Melbourne Python Users Group (MPUG) will be Monday the 10th of May at 6:30PM at Horse Bazaar.

This meeting will see a number of presentations on development and deployment including tools like fabric, pip, virtualenv and coverage during testing. We'll also hear about load-balancing xmlrpclib/jsonrpclib for robust distributed applications.

Full info on the meeting including directions and talks at the MPUG wiki page.

See you there!

category: Python | permanent link
Wed, 28 Apr 2010
PyCon Australia CFP: One Day Left!

The Call For Proposals for PyCon Australia 2010 finishes tomorrow!

Presentation subjects may range from reports on open source, academic and commercial projects to tutorials and case studies. As long as a presentation is interesting and potentially useful to the Python community, it will be considered for inclusion in the program.

We're especially interested in short presentations that will teach conference-goers something new and useful. Can you show attendees how to use a module? Explore a Python language feature? Package an application?

Submit your proposal here: http://pycon-au.org/cfp

As always, please pass this message on to people you feel will find it interesting.

category: Python | permanent link
Sun, 18 Apr 2010
10th Python game programming challenge wrapped up

Another PyWeek is over; congratulations to our winners Alex (for The Street Performer) and team Calipygian (for Oscilliscape).

The theme this time, "Wibbly-Wobbly" was a tough, but the range of different games that were submitted shows how the Python game programmers were quite up to the challenge.

Well done, everyone! See you next time!

category: Python | permanent link
Thu, 08 Apr 2010
PyCon Australia 2010 Call For Proposals

I'm happy to announce that on the 26th and 27th of June, 2010 we are running PyCon Australia in Sydney!

We are looking for proposals for Talks on all aspects of Python programming from novice to advanced levels; applications and frameworks, or how you have been involved in introducing Python into your organisation.

We welcome first-time speakers; we are a community conference and we are eager to hear about your experience. If you have friends or colleagues who have something valuable to contribute, twist their arms to tell us about it! Please also forward this Call for Proposals to anyone that you feel may be interested.

To find out more go to the official Call for Proposals page.

The deadline for proposal submission is the 29th of April. Proposal acceptance will be announced on the 12th of May.

See you in Sydney in June!

category: Python | permanent link
Sun, 04 Apr 2010
Things I did this weekend

This weekend marked the end of the 10th Python Game Programming Challenge. As usual I participated (don't tell anyone but I mostly just run it as an excuse to write a game every 6 months) and produced a game I'm actually pretty happy with. This is a little surprising since I had no idea what the game was going to be until about Friday.

My game, Endless Path, is all about curves (wibbly-wobbly curves). Here's a sample of the gameplay:

PyWeek has been great, again. There's something like 45 games uploaded (there may be some more uploaded before the time runs out). I've already had a look at some and there's some good competition. There's also been a bunch of Python newbies turning in games, and a couple of people mentioned they entered because of the Little Bit Of Python interview, which is nice.

Abbey and I also finished the massive Millenium Falcon build. It's taken three months and it's just amazing. Here's the result of one month:

Millenium Falcon

And two months:

Lego Millenium Falcon build

And finally, after three months:

Lego Millenium Falcon built

Thankyou Dougal for lending me this kit - it's an awesome privilege to be able to make it. You rock!

category: Python | permanent link
Sun, 28 Mar 2010
A Bit Of Cheese

I'm starting a new experiment: the Bit Of Cheese blog. It's going to highlight one package in the Cheese Shop (aka. Python Package Index) per week. Well, it might be more frequent, it might be less. Depends on whether anything takes my fancy :)

http://bitofcheese.blogspot.com/

The first module implements infix regular expressions. It's way more fun than that sounds.

category: Python | permanent link
Fri, 26 Mar 2010
More on html.py and Python 3 compatibility

On the train home I realised that I can make html.py compatible with Python 2 and 3 without needing 2to3 translation. Out goes setuptools and the other yucky code :-)

category: Python | permanent link
On packages supporting Python 2 and 3

My html.py package (whose syntax "really could not be any better" in the words of one user) now supports unicode under Python 2. To achieve this I now need to produce a translated version of the module for Python 3 compatibility (unicode strings are u'text' in Python 2 which is a syntax error in Python 3).

So I'd heard that distutils somehow automagically supported running lib2to3 for Python 3. That's close to the truth. The distribute package supports installing to both Python 2 and Python 3 using lib2to3 to create the Python 3 version.

The trick is to use "from setuptools import setup" instead of "from distutils.core import setup" and then include "use_2to3 = True" in your setup() arguments. Well, that's most of the trick. As it turns out I also had to:

  1. Add "setuptools.use_2to3_on_doctests = False" because my docstrings have "print" statements in them (they're not actually doctests but the 2to3bot doesn't know that). Without that variable being set I would get a strange error deep in the 2to3 code.
  2. Run "python3 setup.py build install" otherwise the Python 2 code would still be installed. It seems "build" isn't actually run for a simple Python-only install. Took me a while to figure that one out.

The upshot is it works in the end and I hope that maybe this post can help others.

category: Python | permanent link
Sun, 21 Mar 2010
PyWeek 10 theme voting open!

PyWeek theme voting has commenced! If you haven't already done so you have one week to re-organise your calendar so you can write a game during the 10th Python Game Programming Challenge. The potential themes this time are:

  • Eleventh
  • Canine
  • Wibbly-wobbly
  • Rose
  • Screwdriver

(As always, there's a meta-theme...)

As an aside I just discovered (via piman in the #pyweek IRC channel) Doryen, a fairly awesome library for writing console games which seems to have quite good Python support and active developers using it.

category: Python | permanent link
Mon, 15 Mar 2010
A simple asyncore "echo server" example

Tonight I wanted a simple asyncore echo server example for my lecture notes and couldn't immediately find one. Doug Helman has a more complex one in his asyncore Module Of The Week post, but I wanted Just The Basics. So I read up a little and wrote one :-)

First the server:

import asyncore, socket

class Server(asyncore.dispatcher):
    def __init__(self, host, port):
        asyncore.dispatcher.__init__(self)
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.bind(('', port))
        self.listen(1)

    def handle_accept(self):
        # when we get a client connection start a dispatcher for that
        # client
        socket, address = self.accept()
        print 'Connection by', address
        EchoHandler(socket)

class EchoHandler(asyncore.dispatcher_with_send):
    # dispatcher_with_send extends the basic dispatcher to have an output
    # buffer that it writes whenever there's content
    def handle_read(self):
        self.out_buffer = self.recv(1024)
        if not self.out_buffer:
            self.close()

s = Server('', 5007)
asyncore.loop()

And the client:

import asyncore, socket

class Client(asyncore.dispatcher_with_send):
    def __init__(self, host, port, message):
        asyncore.dispatcher.__init__(self)
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.connect((host, port))
        self.out_buffer = message

    def handle_close(self):
        self.close()

    def handle_read(self):
        print 'Received', self.recv(1024)
        self.close()

c = Client('', 5007, 'Hello, world')
asyncore.loop()

When run the server should display:

Connected by ('127.0.0.1', 56757)

(or similar - the port number will vary) and the client should display:

Received 'Hello, world'

It was actually pretty easy to write, and kinda fun.

category: Python | permanent link
Fri, 05 Mar 2010
10th Python Game Programming Challenge in three weeks

The 10th Python Game Programming Challenge (PyWeek) is coming. It'll run from the 28th of March to the 4th of April.

The PyWeek challenge:

  1. Invites entrants to write a game in one week from scratch either as an individual or in a team,
  2. Is intended to be challenging and fun,
  3. Will hopefully increase the public body of game tools, code and expertise,
  4. Will let a lot of people actually finish a game, and
  5. May inspire new projects (with ready made teams!)

In honor of the 10th challenge I've finally brought the website out of the 80s :-) ...

BeforeAfter
category: Python | permanent link
Mon, 22 Feb 2010
Melbourne Python Users Group

The next meeting of the Melbourne Python Users Group (MPUG) will be next week, Monday the 1st of March at 6:30PM at Horse Bazaar.

Full info on the meeting including directions and talks at the MPUG wiki page.

See you there!

category: Python | permanent link
Fri, 05 Feb 2010
PyWeek 10 (10!) is coming

The 10th Python Game Programming Challenge (PyWeek) is coming. It'll run from the 28th of March to the 4th of April.

The PyWeek challenge:

  1. Invites entrants to write a game in one week from scratch either as an individual or in a team,
  2. Is intended to be challenging and fun,
  3. Will hopefully increase the public body of game tools, code and expertise,
  4. Will let a lot of people actually finish a game, and
  5. May inspire new projects (with ready made teams!)

If you've never written a game before and would like to try things out then perhaps you could try either:

  1. The tutorial I presented at LCA 2010, Introduction to Game Programming, or
  2. The book Invent Your Own Computer Games With Python
category: Python | permanent link
Sun, 24 Jan 2010
Linux.Conf.Au 2010 is done

What a week. Unlike other week-long conferences I've been to this one had talks every day. Combined with the usual events every night I'm quite tired now :)

I gave two presentations: a State of Python talk which was a little rambling but otherwise well-received.

I did the haka. Much fun!

Even better-received was my tutorial Introduction to Game Programming which was attended by both programmers and non-programmers. Pia Waugh and Pamela Fox* both have good writeups. A couple of the attendees have come to me afterwards to say again how they enjoyed the tutorial and are continuing to develop their games. Quite a good outcome, I think!

On Wednesday night I got my solderin' on and put together a Pebble board for my Arduino. It's a neat invention of Luke Weston and the builds were made possible by the folk from the Melbourne Connected Community HackerSpace.

During the week I set up the PyCon Au website with the help of Benno Rice, Tim Ansell and Michael Kedzierski. It's running the PyCon-Tech code which Malcolm Tedinnick has offered to bring into the modern age of Django post-1.0 :)

Finally on Saturday I organised a Python booth at the Open Day in Wellington Town Hall. I had a lot of great chats with a diverse range of people including some younger boys and girls who were excited by the game programming aspect of Python. Following advice (I've lost a link to) I had a donated external monitor (thanks Nic) which made showing things off much easier. I had a nice 1.8m banner printed (thanks PSF) which looked cool and gave out about 250 stickers :)

* Google Wave account required

category: Python | permanent link
Fri, 22 Jan 2010
withgui finally made public!

On the heels of the release of the cool withhacks library, I've finally (thanks Robert Collins for helping) released withgui on Launchpad. withgui is my experiment in simple GUI creation as mentioned previously (see also.)

I'll be looking at incorporating withhacks to replace my less-elegant (and fragile) namespace hackery. Feel free to take the code, branch and run. Contact me if you're interested in contributing (no comments on this blog but you'll figure out how to.)

Thanks also to Brianna Laugher who tried to help me release to Launchpad a couple of months ago - the failure then was in my available time.

category: Python | permanent link
Fri, 15 Jan 2010
New "html" module release, 1.8

I just had a query regarding my Python html generation module, asking whether I could a) upload a ZIP file and b) whether it was Python 3 compatible. I made one change (map(...)list(map(...))) and have released version 1.8 on PyPI.

category: Python | permanent link
Python module statement ordering ... wart?

Can't call this one a "wart" because it's present in Python 3 and we all know Python 3 got rid of all the warts <wink>.

It has to do with the very specific ordering of statements at the top of your Python source. Specifically if you have any of the following they must appear in this order:

  1. Top-of-file comments (Typically "# /usr/bin/env python", file encoding or source control revision ID string), then
  2. Module documentation string, then
  3. "from __future__ import ..." statement(s), then
  4. The rest of your module.

Failure to order them precisely like this will result in one of a couple of things: either your docstring will not be created correctly (as the __doc__ attribute) or you'll get a SyntaxError because your "from __future__ import ..." statements aren't "at the beginning of the file" (even though they can't be at the beginning of the file.) If the docstring appears after the "from __future__" imports it won't be used as __doc__.

category: Python | permanent link