what can you do with django?

December 2nd, 2007

So I'm reading about Django, which is a really groovy web framework for Python. As the Django people themselves say, there have been *a lot* of web frameworks for Python alone, and it's clear that a lot of lessons have been learned along the way. Personally I've only looked briefly at Zope before, but I was a bit intimidated by how complicated it seemed to be. I also once inherited a project that used Webware for Python, which was pretty nice to work with, but radically different from what I'd known because pages are constructed using a sort of abstract html notation (basically every html tag is a function etc).

There is a lot to like about Django. The MVC pattern is executed quite elegantly (or as they call it in Djangoland, the MTV (model - template - view) pattern). Templates are written in what resembles a server side language, but it's actually a specialized, both restricted and enhanced, Python-like templating language. Templates can inherit from each other to fill in placeholders in a cascading manner, very interesting design choice.

Then there is a nice separation between objects and persistence. Data is declared in terms of Python classes (called "the model") which are then translated somehow on-the-fly to sql-du-jour depending on your database backend. You can even run a Python shell to interact with your model to de facto edit your database.

The model and the template mechanism are both tied together with "views", which is the application code you write for your webapp. A cool thing here is that urls are decoupled from execution code, and the two things are mapped via regular expressions, so you have something like a mod_rewrite declaration (but nicer) that dispatches to functions.

All the same, if I were to work with Django I have a couple of concerns up front:

  1. Templates are written in html with inline Python-line expressions. This makes them both simple and transparent, because the html code that will be served is written by hand. However, considering the present situation with html and browser support, it would be nice if Django could enforce/validate html. But this would probably imply an abstraction over html (to prevent raw html being written) and a layer of code generation for html (which may or may not be easy/possible to cache efficiently).
  2. And that brings me to the second point. I have seen no mention of CSS anywhere. It appears that Django leaves the fairly heavy burden of supplying styles to the designer, without any intelligent mechanisms to help.
  3. Data models are represented in Python code, which is very convenient and provides a much sought after quick access to the contents of your database. Django will instantiate tables for you based on a model, but only for creation. It doesn't seem to enforce or update the correspondence between the Python model and the database schema. The Django book has a mention of strategies for keeping things in check, but this particular section is missing.

There is no doubt Django embodies a lot of what we've learnt about building web applications, but currently I would say it seems a bit incomplete (also pre v1.0 still), if terribly elegant in some parts.

Another question concerns performance. Ruby on Rails has been known to suffer when pitted against the effective but ugly php. Does Django fall in the same bracket or does it have a lower overhead? In general Python is an older and more refined environment, so one might expect Django to scale better.

do all cooks leave a mess?

November 30th, 2007

The answer is obviously no. Or at least one can hope.

I've never enjoyed cooking. Somehow I've never seen the joy of manipulating food from a raw state to something edible. I don't know what it is people get so excited about. One of the reasons I don't like it is because of how messy it is. I really get no satisfaction out of immersing my hands into that dough only to have to wash it off for 10 minutes afterwards. In fact, the whole process is just a big mess. I stop to think about how much washing up there is going to be and that alone makes me appreciate someone other than me doing the cooking.

Are some people more aware of being clean than others? People say you just don't want to get your hands dirty, and it's true. There's no reason for my hands to be dirty, when they are I go wash them. Meanwhile there are people in this world whose hands are visibly greasy and they don't seem to even notice.

latex sucks! (for presentations)

November 28th, 2007

Hold your fire.

Got your attention there, didn't I? :D

Latex is good at what it was built to do - markup big documents. Fine for writing reports, articles, books. The problem is that we have no other tools nearly as good, so we end up using latex for things it wasn't meant for. Like presentation slides.

Why is this? Well, let's see. There's Powerpoint. It's a toy. It's one step up from drawing each slide separately in Paint: it has no structure, it makes you duplicate all common elements, no indexing, no outline. What does it offer you? Oh yeah, a spell checker. It sucks. Then there's latex. You reuse everything you already know about it, it has a ton of helpful packages, and with beamer you can actually write slides without hurting too much. Except.. it's still latex.

Here is the problem. Presentation slides are not the same as prose. Everything latex is good at has to do with handling text. Everything presentations are supposed to be is not centered on text. Giving a presentation is just as much about showing as it is about telling. And you're already telling when you're talking, so you should compose your slides so that you can show people what you mean.

We have horrid tool support for writing presentations. No wonder almost every talk you see is frame after frame of bullet points that no one will remember anyway. People actually paste parts of their text from articles into slides, as if that is supposed to produce a good visual device! :googly:

I have given 8 presentations in the last 12 months, and I'm set to do 4 more by the end of the year. Almost all of them written in latex with beamer. And you know what? I didn't enjoy myself. On the surface it looks promising. The beamer class has a ton of features, you use regular latex, and you output a pdf. But when you get down to the details...

The ratio of illustrations, diagrams, source code and other example materials is much higher than it is in prose. It's supposed to be that way. This is what latex is awkward at. Presentations are visual, so the first thing people think about is the slide design. Well guess what, if you have the perfect design already, good for you, but if don't, you're stuck editing latex styles, which no one understands. Strike one.

Then there is importing images into your slides. Raster images work fairly well, but obviously you have the resize problem - a raster image displayed at a non-native resolution will look jagged. You don't want that. So you hack up a vector image in something like dia. But you can't use that directly in latex, you have to convert it to either eps or pdf. I have found the latter least painful of late. So now not only do you have to know latex and dia, you have to author Makefiles to get a sane build process out of this. That is if you can remember to include just the right latex packages in your document, make sure they are installed both locally and any other place you may want to do a last minute recompile. Strike two.

Speaking of Makefiles, everyone knows you have to recompile a latex document to see the changes you've made. But with presentations these changes are much more often concerned with layout than they are with content. And it's a huge pain to recompile again and again to debug something as simple as a two column layout (use \minipage and set the width in absolute units just right so they fit next to each other). So now I have a compile loop running every second that invokes make and I see the updates in kpdf. All because I want to tweak the layout of my content. Speaking of compiling, have you seen those latex error messages lately? They're not meant for humans. And as far as languages go, latex isn't exactly the most... robust of them all. Make one tiny mistake here and your compile detonates. Strike three.

It is a pain. It is a pain to have to recheck old documents to remind myself of how I included a piece of source code. Or how I handled a particular image. Latex is extremely hackish. It makes it friggin' hard to generalize anything so that it becomes reusable. I find myself copy pasting code all the time from one document to another. There is nothing sensible about how things are done, it's just a matter of writing something that works, and having a copy of it somewhere. The more presentations I write the more difficult to remember which trick I used where. An unmaintainable mess.

And then there's time pressure. When you're writing a technical document, you have the time to work out the little quirks you will encounter. It's a permanent document, it will continue to exist for years after the fact, it's worth the effort. But presentation slides are not. You just want to write them fairly quick so that you can give the talk and move on. You're not going to be investing time in this, it's a one time thing. And yet writing slides requires a lot more patience with latex than writing prose does.

Nevermind that, writing slides is a creative process. It is not filling in predefined sections of a document, like an article for a conference is. You want to be able to experiment, to try things just for the hell of it. Latex is horrid for this. Let's see if I can fit this figure next to the text here. Okay, let me first look up how I do that. How do you actually find this out? I already covered that earlier. Then try it, debug the layout, finally I can see if I got what I wanted. No, that didn't work out, let's switch it back to the way it was. Compile error. Goddamn it, now what? Okay, let me gradually undo to a state that does compile and that way I can figure out what's causing it.

Not. how. it. was. meant. to. be. done.

The Recruit: very refreshing

November 23rd, 2007

A different kind of spy movie. James (Colin Farrell) is the college kid who gets recruited into the CIA. We see him go from a civilian to an operative-in-training, in boot camp, learning the basic skills, not to mention learning some basic psychological devices.

What I really like about this story is how raw the characters are. This is not a story about a veteran in the field à la Jason Bourne, who knows everything, feels nothing, never fails. It's about a guy you can relate to, someone who is learning to understand, but still living through the pain of being put through all these things that are happening to him. The plot isn't brilliant, it's neat and tidy, almost simple. But the appeal of the movie isn't in that, it's in the characters, how real they are and how unrefined their reasoning and emotional response is.

The idea is nothing is what it seems, but they don't take this very far, they hold back so that the characters can tag along at their own pace. And that's.. nice. Most other stories try to push the envelope and many of them fail to resolve well. This one doesn't, and you can appreciate that they care more about the character development than the goal to have a complicated plot.

Enjoyable as it is, I just have to point out a few technical things. Forgive me, it's too much to look the other way, downright embarrassing at times. Okay, what the bad guys are after is "a virus", a computer virus. It turns out, though, that this is a magical virus that you can deploy right into the power socket in your house and it will infest the nation's whole power grid. Okay, that's just beyond bad. And James is the MIT kid who is a computer wiz. And we are supposed to buy this. Had they said it had something to do with electricity or whatever, maybe if they'd tried hard enough I'd bought it. But a computer virus on the power grid? Seriously, do 15min of research before you put that in a movie.

Secondly, as the guy duly explains, the CIA headquarters are well guarded, you can't take anything out of there. That's why the computers don't have disk drives. Yes, that's a quote. What kind of disk drives? Floppies? CDs? Zip drives? And you also can't print anything because they don't have printers. Uh-huh. Well that still leaves about a dozen different media you could use, including usb drives. And what do you know, that's precisely how the super clever villain exports this "virus" out of the building, hiding the usb stick in a coffee mug. Now, your typical computer virus is probably less than 1mb of source code, I mean these things have to be small to go unnoticed. But this mastermind is smuggling it out... in pieces. What do you got there, Photoshop? :D Like the whole thing wouldn't fit on the usb stick, so you have to do it in turns. Okay cmon, I'm on candid camera, who the hell would believe this?

accused of stooping low for laughs

November 21st, 2007

It was a Friday night. A bunch of us were sitting around a table in a bar drinking beer. Two guys, two women, and myself. The conversation was passing through a quiet period and we were talking about beer or buying beer, something like that. Alluding to how the beer isn't all that cheap. That's when I decided to shoot in with a joke: Well when your glass is starting to empty you could always go to the bathroom for a refill. It will be a little diluted, but hey. [correct for loss of precision in translation]

It wasn't very funny, but hey who can deliver all zingers? Some jokes are mediocre, you either tell them anyway or say nothing.

One of the women smiled, the other one didn't look up from her cell phone, probably didn't even hear it. Just about right. But the guys had a totally different reaction. One stared at me as if in disbelief. The other made a derogatory comment about how classless jokes are part of modern culture.

I was incredulous at the criticism. I had no idea what he meant. I asked the woman who had smiled at it, she didn't get it either. It wasn't until the next day it dawned on me.

What I meant was obviously that if you're running out of beer, you can go to the bathroom, where there is a sink, and fill up your glass with tap water. It would then be diluted, the remaining amount of beer in the glass would mix with the water. This was a natural instinct for me, going back to when we were kids and drank tea, but the tea was hot, so we would fill up half the glass with hot water and the rest with tap water to get the right temperature. The two guys had a different interpretation of the joke. I can't bring myself to spill it out for you, but I think you'll get it soon enough. It was indeed classless.

When I realized the alternative interpretation I was insulted. I have never stooped that low to get a laugh and people who do this.. well let's just say they're "not my kind of people".

So why does this happen? It has to do with cultural background. The same picture shown to people in different parts of the world can trigger very different interpretations based on what kind of world they live in. And we understand why this is so. Just like we interpret or "understand" visual images by the fact that our brain compares them with all kinds of things we have seen in the past, searching for a match, we associate everything we perceive sensually, or intellectually, with things we already know.

So when you're misinterpreted, and someone accuses you of expressing something classless, who is really the embarrassed party?

Jon of ngedit.com recalls how a benign hand gesture he helped spread was discovered to be very similar to an obscene hand gesture, well known in some circles. His rationale is:

If someone does know about the obscene version, then whose fault is it really?

I think making this sort of accusation says two things about you.

  1. The fact that your mind connected you with the obscene alternative, and you didn't discard this and think there must be some other meaning I'm not getting reveals that you are well familiar with expressions of this nature. Whether it is the case that you immediately found the obscene version and stopped thinking about it, or you found both and decided on the obscene version, well neither detracts from the fact that you calculated the obscene version a more probable choice, which says something about what kind of statements are natural "in your world".
  2. The fact that you concluded I had made the obscene statement, taking into account that I have never told jokes like that in your presence (or for that matter at any other time) says something about what you think I'm capable of. It also shows a not so kind hearted willingness to lump me in with a group I don't exactly belong to. No benefit of the doubt given here.

But now in the wider context, what can be said about jokes like this? The woman who had smiled at the joke obviously got the intended message. So telling it to her was harmless. As far as the two guys are concerned, they have themselves to blame for associating what was said with what apparently they are used to hearing. And unintentionally also revealed something about themselves we wouldn't otherwise have known.

There is a choice to make here. Anything you say could potentially be misinterpreted. Sometimes you are aware of this, knowing what kind of material passes for humor. So you can censor yourself and rephrase everything you want to say so that the meaning cannot be twisted. But it's not exactly the most satisfying thing to be unable to express thought directly.

In the alternate case, you say what you wanted to say, and part of your audience may appreciate it, while you give the pathetic people what they want. He said balls hahahahaha *insert a 30 minute veritable battle of wits as your audience tells every joke about balls they can think of as you patiently wait for them to get it over with*. Of course, as a last resort you can shun the bad company. But then, of course, you're not socializing anymore, you outcast you.

However, it's really up to you as a receiver to make the judgment. Personally I like to give people the benefit of the doubt even when I'm fairly sure they meant to be classless. So I don't laugh, I offer an alternative interpretation. At this point you have two choices. You can either admit defeat and save face, or you can persist. Most people persist, and sort of shyly confirm their intentions by rephrasing the joke. Shyly because they hoped the first version would be clear enough without having said too much. But then when they've restated it and I still don't laugh, check mate. No coming back from the fact that you just told an unfunny, classless joke. But hey, I gave you the chance to recover.