legume 0.1 released: easy_install ‘legume’

It’s been a few months in the making, but now legume has gotten to a point that I thought it worthy of a 0.1 release. The basic feature-set is complete, so it’s in a usable state, that is if anybody can be be faced with using it yet, as at the moment there’s very little (read: nothing) in the way of documentation or user guides.

The release is available via pypi or google code, and I think that softpedia has already snapped it up.

As for the name: “It’s not a nut, it’s a legume! phew!” (Ricky Gervais, Animals).


Python distutils

A guide to creating a simplistic .exe python package installer:

  1. Create a setup.py file in the directory above your package directory:
  2. Copy the following in setup.py:
    from distutils.core import setup
    import setuptools
    import sys
        description='My Amazing Library',
        author='My Name',
        package_dir = {'':'.'},
  3. Open a command prompt in the directory containing the setup.py and execute
    python setup.py bdist_wininst
  4. Observe how the installer appears in the newly created dist directory.

Additional Notes

  • To specify that the installer is only for a specific Python install use the –target-version option with setup.py, eg:
    python setup.py bdist_wininst --target-version=2.5
  • Swap bdist_wininst to bdist_msi to create a Windows Installer for your python package.

Event handling in python

Update: for the latest code please check nevent.py in the legume google code repository

Typically, event handling in python either involves storing a function pointer and invoking it at some pointer later-on, or inheriting a base-class and overriding event handler stubs. Storing a single function pointer does not permit event handler chaining, but is the easiest method to expand upon, and acts as a replacement for event handler stubs in classes.

Expected usage of the Event handler:

def example_handler(param):
    print "1", param

example_event= Event()
example_event += example_handler

And the output:

1 Hello!
2 Hello!

A basic structure to the event handler class is shown below:

class EventError(Exception): pass

class Event(object):
    def __init__(self):
        self._handlers = []

    def __iadd__(self, other):
        if other not in self._handlers:
            raise EventError, \
                   'Event %s error: Handler %s is already bound' \
                   % (self, other)
        return self

    def __isub__(self, other):
        except IndexError, e:
            raise EventError, \
                   'Event %s error: Handler %s is not bound' \
                   % (self, other)
        return self

    def __call__(self, sender, args):
        result = None
        for handler in self._handlers:
            result = handler(sender, args)
        return result

    def is_handled_by(self, handler):
        return handler in self._handlers

Note how overriding __iadd__ provides a syntax identical to C# for adding extra handlers onto the event handler chain – marvelous.

This code is actually based off the event handling used in the legume network library, see
nevent.py in the legume google code repository for the code in use, and to see any further changes.


A unittest green bar for the console

One thing the unittest module is missing for me is a green bar of success (or a red bar of fail), so here’s one I cooked up to scratch my itch (windows only at the mo’, sorry):

import sys
import ctypes
import unittest

class GreenBarRunner(unittest.TextTestRunner):

    def __init__(self, verbosity=2):
        unittest.TextTestRunner.__init__(self, verbosity=verbosity)
        STD_OUTPUT_HANDLE = -11
        self.std_out_handle = ctypes.windll.kernel32.GetStdHandle(

    def set_color(self, color):
        bool = ctypes.windll.kernel32.SetConsoleTextAttribute(
            self.std_out_handle, color)
        return bool

    def run(self, test):
        r = unittest.TextTestRunner.run(self, test)

        failed_count = len(r.failures)
        errored_count = len(r.errors)
        total = r.testsRun
        ok_count = total - (failed_count + errored_count)

        sys.stdout.write('#' * errored_count)
        sys.stdout.write('#' * failed_count)
        sys.stdout.write('#' * ok_count)


suite = unittest.TestLoader().loadTestsFromModule(example_module)

It’ll execute the TextTestRunner so you won’t lose the test names and stack traces and afterwards display a red/yellow/green bar:





Hey folks, yup first blog post here! (and hopefully not the last)

Two days ago I began work on a zombie shoot-em-up top-down game – basically you get chased down by unending hordes of zombies and you’ve got to shoot them all. At the moment it’s pretty basic, but I’m happy with the progress I’ve made so far:

The player currently moves around using WSAD for forward, backpedal and strafe, and uses the mouse to aim/face in a particular direction.

Specifics of stuff implemented:

  • Map scrolling
  • Zombies pathfind using A*, and adjust their route so every couple of seconds. They have no maximum range, and can track the player across the map. With more than two dozen zombies the game crawls to a halt (unless the player stands still).
  • Blood splats and spray appear with each shot. The splats will stay on the ground for a little while, and the spray will stick to walls.
  • Ammo counter and ammo crates.
  • Huge compensation cannon pulse rifle.