trepca's notes

Bad Error Messages

I’m surprised programmers don’t talk more about error messages. We encounter them every day, yet you rarely see any constructive discussion about them, maybe

error: no error

Let’s take Python for example:

IndexError and ValueError

index = 10
empty_list = [1,2,3,4,5,6,7,8,9]
for x in range(1000):
     empty_list[index + x]

Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
IndexError: list index out of range

empty_list.index('hehe')

Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
ValueError: list.index(x): x not in list

Printing out which index was out of range would definitely save me a minute or more, not to mention how annoying this is for newbies.

ValueError: 10 not in list [1,2,3,4 ...]

Paranthesis of hell

>>> if 1:
    ...     test = ((1,2, 3)
    ...     test2 = None
File "<stdin>", line 3
    test2 = None
         ^
SyntaxError: invalid syntax

Python forums are full of these questions. Would it be so hard to add (“Maybe you’re missing an end parathesis?”)

In general, I try to suggest solutions and provide some context to the error, that way it’s much easier for other people to debug.

Jan 16, 2011

Complex data migrations with South

You’re in the middle of a project when you realise that your database model needs a refactoring to support latest changes. What to do? Meet South, a migration tool for Django applications. It will track changes in your models and help you migrate as your project develops. Most changes can be detected automatically, like adding and removing fields. Once you start renaming things and moving them it can get tricky.

WARNING: always make backups before doing any migrations

Let’s say we have a Todo application with these models:

But you later on figure out that it would actually be much better if you’d refactor to this:

Now, if you just change the models to this and run auto migration, South will remove all your data and create new models. Not really what you expected.

As South can only detect simple changes, we need to be smart, so what to do?

Lets first generate new models by appending them in our models.py, so we can use south normally.

This will detect new models and generate them. Sweet, but what about data? Meet data migrations.

./manage.py datamigration todolist move_to_submodels

This will create an empty 00NN_move_to_submodels.py file, ready to be used for data migration. Lets say that Todo instances with location or all_day field set are Events otherwise it’s a Task.

We add this code:

We now migrate again and check if data was migrated correctly. Once you’re sure everything is ok, you can delete Todo model from models.py and migrate to delete the table forever.

That’s it.

Oct 23, 2010

Quicksilver plugin for org-mode

Add this script:

to

~/Library/Application Support/Quicksilver/Actions/

Set your org-mode capture templates like this (lines 9-10):

Restart Quicksilver, write some text and then search for “Task” in second step. Press Enter and it should be in your orgmode inbox. Yay!

orgmode plugin for quicksilver in action

Sep 19, 2010

How to read Twitter effectively

I was really happy when Twitter lists came out. Before, you’d have to go through almost every update, to see if any of your favorites published something. You’re left with either spending a lot of time, reading your timeline or being really careful who you follow. None of them being the right solution. With lists, you just need a multi-view Twitter client like Tweetdeck or Seesmic and setup a few lists by topic and priority. In my case, I have personal, business, tech-news, fun, developers, london, slovenia and news. Ordered by priority. Now, whenever I’m in a hurry, I just check the first column to check for important updates. When I have more time, I check other ones. It’s just super easy and effective.

a great overview of your twitter streams

I know it’s a pain to categorise all your friends, but it’s really worth it and transforms how you consume Twitter.

Jun 23, 2010