skip to main bit
a man slumped on his desk, from 'The Sleep of Reason Produces



Archive for the ‘Hacks’ Category


python class Culture:

Every Friday at EFF, we have a Python class, where anyone in the org (and a few friends from outside) join up to learn a little Python, talk about coding and share what they’ve learnt. There’s a good mix of seasoned python hackers, coders who don’t know much python, casual programmers, and people for whom this is their first experience of programming.

The part i enjoy the most (apart from congratulating myself for reaching a level of maturity that means I don’t go I KNOW I KNOW whenever i know the answer) is the material that isn’t about the technicalities of programming, but of the culture. We often discuss, for instance, about the most aesthetically pleasing way of writing code. Watching smart coders attempt to verbalise those instincts is fascinating, especially when the instincts begin to spread through the group.

To give an example, we’ve been coding up a Python version of Conway’s Game of Life. We all spent a fair bit of time discussing that niggling problem with counting up how many neighbours a cell has. Do you do it “manually”:

or iteratively:

I think most coders would end up doing the first, but they would feel a bit dirty doing it, just as I always feel a bit dirty when I have x and y as attributes, instead of being able to treat them as different aspects of the same thing. It’s the right instinct to try and generalise, and it was fun seeing starter programmers expressing their mild discomfort.

After we’d got Life to work, Seth rewarded us by showing Golly, which is a great cross-platform Life simulator with many pre-programmed patterns. I really had no idea that they’d managed to code up a Turing machine in Life, let alone patterns that emulate a universal machine, running a program that runs the Game of Life.


django incoming!

I’ve spent today hacking with Django (well, to be accurate, I’ve been hacking on Django intermittently with eating pancakes, playing with kids in the park, and watching the whole Sarah Palin gossip trainwreck ricochet across the Internet: I honestly now have no idea what the hell is going to happen in this election).

I’ve been tracking Django since forever, but held off from doing anything too serious with it until 1.0. I’ve got a work project that fits the bill quite nicely, and I figured that a few days before 1.0’s September 2nd estimated deadline would be good enough to start hacking.

It’s been fascinating working under a codebase that is under heavy but stable development as you work. I hit bugs that I’d find in the bugtracker, filed just a few days ago, and then find them fixed while I slept. I’d also have occasional disconnects where something I’d been using in Django a few months ago had been completely reworked (mostly the admin feature), and I’d have to scramble a little to work out what had changed, and what had remained the same.

Overall, though, I’ve been really enjoying it. The changes that were made, like being able to subclass database models, seem esoteric but end up being really useful (I am uncomfortable with idea of refactoring a database schema, but it’s made much more sense to me now I can fiddle with inheritance instead of bang my head against SQL). The documentation remains first class, and has kept up with all the changes. I’m not far enough through the project to give you a realistic summary of how good a fit it is for my use, but so far, it’s been fun, at least — and it’s definitely good enough for 1.0 status.


the most useful simple script i have

Lee’s comment that mentions having a folder for items you’re about to delete reminds me of probably the script that has most contributed to sanity in my filing system. It’s pig simple, albeit a bit scary to write and enact. All it does is delete everything in a given folder that’s over a week old.

I’ve had bad experience with handing “delete file” powers to an automatic script before, so I’ll disclaim any warranty (“TO THE EXTENT PERMITTED BY APPLICABLE LAW” as the GPL shouts), but it’s pretty straightforward, and works for me: I have it in a cronjob. The tmp folder it cleans up is my default save folder on Firefox, and where I generally download everything. If I want to save anything longer than a week, I find it a place in the rest of my filing system. It’s sort of like having a cleaner come around every week: occasionally you go “Garr! Where’s that coffee-stained, have torn copy of last month’s New Yorker! I was going to eventually get around to reading that!”, but mostly your cruft just silently disappears without you noticing a thing.

You’ll need to replace /home/danny/tmp with your own dumping ground. If you run it like this:

It’ll tell you what it’s planning to delete. Run it without the -d and it really will delete those things, blam.


pomp; patry; gconf-watcher

Second Circuit Judge Pierre Leval once said that the best way to know you have a mind is to change it, and I have tried to live by that wisdom… There are positions I have taken in the past I no longer hold, and some that I continue to hold. I have tried to be honest with myself: if you are not genuinely honest with yourself, you can’t learn, and if you worry about what others think of you, you will be living their version of your life and not yours.

William Patry

I didn’t know of Bill Patry before he started blogging, but once he did, I started seeing his name everywhere. Mainly on huge multi-volume collections of hardback legal tomes, titled “PATRY ON COPYRIGHT”. He’s given up blogging because people would insist on quoting his blogging opinions as though they were an official pronouncement of his new employers, Google. Also, the current state of copyright law (and he actually contributed to drafting a chunk of it when he worked for Congress in the Nineties) depressed him too much.

Fortunately, I am never depressed by copyright, and I am confident you will never confuse my pronouncements here with any of my employers, because I have a little box down there that says so. So we are stuck with each other.

Today is column day, which means I have to save my most potentious stuff for one of said employers instead of you. It also means that I have been procrastinating all over the Net. Patry’s mum told him you must learn something new every day: today I learnt that the best way to poo-poo a fusion project is to say “Feh, you’ll never fix the Bremsstrahlung” (and the best way to help is to start a fusor in your home town). I read the best defence ever of a children’s book that has gay marriage in it, and added another Hari Seldon-style modern psychohistory attempt to my list. I also learnt that other far more esteemed columnists look exactly as bad as me on column day.

But if you wanted to know that stuff, you would have Googled for it. What you want to know is this: if I’m using GNOME, and I’m futzing about with my preferences, how can I easily note them down so that I can recreate what I’ve done when I accidentally delete my home directory (again)?

That’s what you were Googling for, my friend. Run this code in the background as you merrily click on Gnome options in most programs, and it’ll spit out a set of commands that if you run will recreate your clicking. Useful for finding where the hell Gnome is hiding certain preferences, or what exactly certain programs are changing behind your back. You’ll need to install python-gtk in Ubuntu and Debian. And probably a bunch of gotchas that I have pigheadedly ignored and which you will find in the comments below from smarter, friendlier people than me.


rag and bone scripts: switchto

So I feel a bit bad because I have two half-written long thoughtful posts, but this evening was games night at the EFF’s hollowed-out mountain headquarters, and after a bit too much Settlers of Cataan, beer, Rock Band, and beer, I strangely cannot be bothered to finish those entries. I will just repeat for the record that you have not seen rules lawyering until you have seen EFF’s litigation team play AD&D. I still admire Quinn for daring to DM them.

Anyway, instead, let me fob you off with a couple of tiny hacks I use pretty frequently on my Linux desktop.

They all gank their usefulness from wmctrl, which is a fantastic command line utility for almost any Linux desktop, and lets you control windows and focus from the command line. You can install it from that webpage, or just do apt-get install wmctrl on Debian/Ubuntu.

I save the above script as ‘switchto’, and use it in GNOME’s keyboard shortcuts like this:

That should set some options in the standard GNOME editor to run
switchto "Mozilla Firefox" "firefox"
whenever you hit Alt-F. It works with metacity and compiz, which is probably what you’re running with GNOME. Other desktops will have other ways to create keyboard shortcuts — I seem to remember that KDE’s one rocks.

Now, whenever I hit Alt-F on my keyboard, my desktop will switch focus to the first window it finds with “Mozilla Firefox” in its name. If it can’t find one (ie I’m not yet running Firefox), it starts it up for me. One key press gives me a Firefox window, either way. Here’s another one:

This is the poor man’s QuickSilver — it pops up a box, and switches your focus to a window that matches the text you type into it. It needs a program called Zenity which comes by default on most GNOME desktops.

Actually, I don’t actually use the second of those that much, but I do have a bunch of scripts like the first, setting up command keys to always switch to certain programs like the Terminal (try switchto "termi"), or IRC or Kontact. Your fingers quickly learn the motion, and finding your key applications even in a mass of desktops becomes instinctive.

I’ve always felt that that was the real power of QuickSilver as a window finder — Alt-Tab never lets you learn a consistent muscle memory to switch to a particular program, so you’re constantly derailing your thought by peering at icons to navigate around. I’d also encourage you to play around with wmctrl on the command line — you may be able to think of other desktop tricks that you can turn into simple keypresses.


petit disclaimer:
My employer has enough opinions of its own, without having to have mine too.