Archive for January 30th, 2008

Sometimes you just gotta monkeypatch…

Recently Guido offered up a couple of interesting recipes for cleaner monkey_patch implementations via decorators and metaclasses.

I think monkeypatching can be a very useful tool in the same way that a jackhammer or a shotgun can be useful tools. When you need them, nothing else can do the job as well. Of course, you can always smash the cement by hand with heavy iron rods — as is sometimes done in India, where labor is often cheaper than tools… Feel free to make your own analogy to using static languages and the trend towards code generation, rather than having the right tool for the job. But don’t expect me to do it, I’m trying to take the high road today ;)

Anyway, some of the responses have been along the lines of “I like my monkeypatching to look ugly, because it deters people from doing it.” And while I have a lot of respect for some of the folks saying this, I’ve never been a fan of this kind of “syntactic vinegar” argument. It seems to me that it’s better to make complex things seem easy than it is to make simple things seem hard. And it’s kind of paternalistic, and not consistent with the “we’re all consenting adults here” philosophy behind Python. Sure people could do stupid things with these tools, but I think it’s a bad idea to spend your time trying to stop hypothetical bad programmers from doing stupid things.

In other words, we shouldn’t make jackhammers more ugly and complicated just because you would smash your living room wall to pieces if you tried to hang a picture with one. ;)

And I think these patterns are useful. The first decorator syntax is particularly valuable for stubbing interface points out for testing purposes. Hey, a it’s a “mock framework” in 5 lines of code. The second looks like a nice thing to have around for testing purposes too. I’m sure there are uses beyond testing, but I know that’s where I’m likely to start applying these monkeypatching helpers.

What can you do in 600 lines of python?

Over on Coding Horror, Jeff Attword asks, what you can do in 600 lines of code? Apparently the first product from 37 signals had less than 600 lines of Ruby code. Apparently they didn’t count templates, CSS, or javascript, so that actually seems like a lot of code ;) I can think of a lot of little TurboGears apps that I’ve written in less than 600 lines of python code.

And I’m looking forward to DBSprockets because it will make it even easier to create TurboGears applications very, very quickly, by making it easy to create automatic (but customizable) CRUD interfaces for your SQLAlchemy model objects.

For me it’s not the 600 lines of code goal that’s interesting, it’s the ability to try out lots of little things quickly and see what sticks.

JS + CSS minimization

Wow, somebody released a TurboGears widget to help automatically minify JS and CSS files. It certainly looks like a good idea to me, and was something that people were talking about adding to ToscaWidgets a few days ago. Unfortunately I haven not yet had a chance to look at it in any depth yet because I’m at a hotel with crappy internet connection all week so I haven’t been able to pull it down yet.

One thought I had when I saw this is that it would be extra cool if this only minified stuff when you are running a project in production mode.