Posts tagged: PYTHON

Django as superego

I built a toy site using Django’s “generic views” last night. Basically this means that for the first time I created an app without writing any real code – I defined a model, wrote some rules mapping URLs to Django’s generic view functions, and made some templates that get called by that view code (I spent most of my time fussing with the templates!).

This would have been pretty easy to hack up in PHP, too, but there are lots of things that would have been just as easy to not do the “right” way – things like clean URLs everywhere, redirect-after-POST (to avoid multiple submissions), custom error pages, a polished admin for inspecting and editing the data, nicely modular templates. Django made it easier to do it right than to do it wrong.

Enforcing Style

Browsing some programming blogs this evening I came across Ken Arnold’s provocative “Style is Substance” post from October 2004. In it, he argues that coding style variants should be eliminated by including style in the language definition:

…the only way to get from where we are to a place where we stop worrying about style is to enforce it as part of the language.

This isn’t that shocking to Python programmers (perhaps that’s why he mentions Python twice in his list of “mature” languages?). Python enforces whitespace style. To me this is one of the great joys of working with Python – my code from two years ago, or somebody else’s code in an open-source project I’m looking at, uses exactly the same indentation “conventions” that I do – because they’re not conventions, they’re requirements.

Random crufty open source release of the day

Last year a client asked for help moving his website to a new host from XO.com. The tricky part was that his 200 pages of content were locked into an obsolescent proprietary tool called “Site Builder” that offered no exporting options. The file format was a flatfile that looked like this:

#Page-Type "html"
#UID "1000"
#Access-PublicRead "on"
#Access-PublicWrite "off"
#Page-Links-Style "links_outline.nhtml"
#addbrs "off"
#hidenav "off"
#HTML ...

The file structure went like this: a parent directory named nss-objects; child directories bearing page names (or slugs, really); and inside each, an empty directory named !data and a text file named !object with the page content as described above. Weird. (I suppose every proprietary one-off system is weird in its own way, so there’s nothing to be gained from dwelling on the specifics, but at least including them in the post raises the chances that somebody who actually needs this thing and searches for it will find it.)

Moving the blog to Django

The long-awaited (by me) conversion of this blog to Django is underway. After a couple hours’ work I have a full set of models and a functioning admin, and working index and detail views of postings and comments. Searching, posting comments, and tags are the major pieces remaining. Because of my busy schedule I’ll only be able to work on it in fits and starts, but I expect the total labor in the end to be about five hours.

Neat new stuff in Python 2.5

I’m looking forward to Python 2.5. In particular:

  • Partial function application. One of my favorite things about Python is how amenable it is to a mixture of procedural, functional, and object-oriented styles. Learning a bit of Scheme last year put me in an especially functional mindset. Can wait to see what else gets rolled into the functional module in the future.

  • pysqlite in the standard library. The incorporation of SQLite into PHP5 (which goes a step further than Python, since it includes the SQLite binary itself) has been a great convenience. Many apps just don’t need a full relational database – and if they eventually do, much better to have started with SQLite than with BDB or some homegrown flat-file solution.