This post originated from an RSS feed registered with Python Buzz
by Ben Last.
Original Post: Take Off And Refactor The Script From Orbit
Feed Title: The Law Of Unintended Consequences
Feed URL: http://benlast.livejournal.com/data/rss
Feed Description: The Law Of Unintended Consequences
One of my more tortuous movie requotings, methinks. The massive network of objects linked by similarity that I rattled on about the other day (how tediously self-referential of me) has been bugging me, in the same way as a chocolate bar, as yet uneaten, can bug you from a cupboard three flights of stairs away. It's the code that compares objects, to be exact. Much clever nesting of comparisons so that only the necessary queries are made; excessively clever, in the way that a model cathedral made entirely of toothpicks is excessive. It's not elegant, it's baroque.
Or rather, it was, for it's all gone. In its place is a solution based on the excellent Python sets module. Every object is a member of one of around thirty different sets. Similarity is then calculated in one single loop that works on set intersections. The processing time is up by a factor of 1.1, but the similarity method is now just six lines as opposed to a pageful. I'll take that trade, since the attributes that define similarity grow in number every week as we discover yet more ways to describe them, but runtime for this script is, essentially, free.
I haven't really had any call to us the level of Python in which sets resides. I think of it as a sort of abstract mathematical zone, suspended above clouds in the dim and distant sky above me, where modules like cmath and mpz live. A realm visited only by those who take the mathematical approach to software; you know, the kind of people who will discuss np completion over beers. I belong to another set; the set of programmers who started with individual ones and zeroes (well, practically, my first computer was one of these) and tend to take a more mechanical attitude to development.
Naturally, like all sweeping generalizations, this is oversimplistic[1]; the sets of developers overlap considerably. Nevertheless, I think there's validity in drawing this distinction; the first set seeing algorithms in terms of the mathematics behind them, the other considering the execution, visualizing it on a sort on in-head virtual machine. Counting myself squarely amongst the second set, I've learned a great deal from the first, especially that O(n!) is a term of abuse...
Moving right along to another subject (the ADHD is a bitch), I found this little article on Game Scripting In Python of interest, as well as this on the same author. The PlayStation Project I'm involved with needs some form of scripting language, and whilst the choice of tools is affected by far more than my personal preference, there might be a role for Python on the console as well as the back-end servers that massage the database. I like the Humungous products; Daughter has had hours (days, even) of fun Pajama Samming and Freddy Fishing, so it's nice to know that there's more to Python and gaming than, um, PyGame.