Ben Godfrey

Archive for August, 2004

Python wrapper to take the pain out of the units command

#!/usr/bin/python

"""Wrap the standard Unix units command but do the calculation as well.
Arguments are the amount, the from-unit and the to-unit"""

import os, sys

a = sys.argv
if (len(a) < 4):
    print "usage: convert.py quantity from-units to-units"
    sys.exit()

cmd = "/usr/bin/units"
pipe = os.popen("%s -q %s %s" % (cmd, a[2], a[3]))

try:
    numberpart = lambda x: float(x.readline().strip()[2:])
    mult, div = numberpart(pipe), numberpart(pipe)
    print "%0.3f %s = %0.3f %s" % \
            (float(a[1]), a[2], float(a[1]) * mult, a[3])

except ValueError, e:
    message = str(e).split(":")
    unit = message[len(message) — 1].split(" ")
    print "Unknown unit: \"%s\"" % unit[len(unit) — 1].strip()[1:-1]

Ahhhh ha hahaha!

I’ve been hacking heavily on InfoCMS and I’m really starting to create cool stuff now:

  • I created a form representation system which describes a form spec in XML and then provides methods to allow generation of an HTML form with notes and hints and things, validate data entered into that form, redraw the form with friendly field-specific error messages and more.
  • I created a metadata system that allows forms to be concatenated and the metadata bits reduced to XML. Once you add a metadata field (with rich type options) to a form, the system takes care of gathering and letting users edit that data through all the usual web controls. This allows me to have a very simple layout for my user table, for example, but for hackers to be able to add as many fields as they like just by editing the metadata XML definition. A user’s metadata can then be queried by creating an object from the stored representation. Because I’m using DB_DataObject which supports overloading, hackers can add get and set functions to the DataObject class, and have those allow manipulation of the metadata fields exactly as if they were real columns.
  • I created a simple system for paging lists of records, built on top of a previous hack I made for Hype to integrate DB_Pager with DB_DataObject. It creates everything including the back and forward navigation.
  • And then, the crowning glory, I pulled it all together to create a class called ManagerView. This uses forms, metadata, database classes and InfoCMS’s view hierarchy to allow hackers to create database table management interfaces with a minimum of no code, but by overloading methods in ManagerView, they can control the flow of data as it is created and edited down to the smallest detail. The code for the interface for managing user roles in InfoCMS is now 15 lines. This includes paged listing, adding, editing and removing of records. The interfaces created by ManagerView are rich and friendly and are only going to get better as I distill more of my experience as a web hacker into the class and it’s friends.

I’m feeling pretty chuffed now.

ISP fun

Had an interesting couple of hours today when Gradwell installed PHP 5 on a box they thought wasn’t in the web cluster, but was. Hypothetical2 and InfoCMS emulate exceptions, so they have throw and catch functions and an Exception class. PHP 5 has real exceptions, so these words are sacred, the former being keywords and the latter being a built-in class. Needless to say some amount of chaos ensued. I rapidly updated all my live sites but in such way that I can be pretty sure I’ve introduced bugs.

Then later on this evening I was trying to do some stuff with the smtplib Python module and couldn’t get any mail to come through. After poking about for a bit I found that it was all jamming up in my MTA queue because my Mum’s ISP, Tiscali, wouldn’t return anything in reponse to an MX query. I guess this is their clever way of stopping worms from running riot, but it pissed me off, so now I’m using Zen‘s servers instead :-).