Monthly Archives: December 2009

Add an option for your script to drop into the debugger

I like using the python debugger to, umm, debug stuff. Just recently I wanted to use pdb to poke around in a script that would intermittently crash. I wanted to add an option to my script that would drop me into pdb when the script dies.

This turned out to be trivial to write. The script doesn’t do much, but if you add the --drop-into-debugger option, you’ll get dumped into a pdb session, where you can study the problem more closely, like this:

$ python --drop-into-debugger
> /home/matt/
-> 1/0

When I don’t add the --drop-into-debugger option, then of course the uncaught exception just crashes the script:

$ python
Traceback (most recent call last):
  File "", line 23, in <module>
  File "", line 15, in main
ZeroDivisionError: integer division or modulo by zero

The code involved was really easy to write. I wrote a decorator called into_decorator with the decorator module, but that’s not strictly necessary. Here’s the code:

# vim: set expandtab ts=4 sw=4 filetype=python:

import pdb, sys
from decorator import decorator

def into_debugger(f, *args, **kwargs):
        return f(*args, **kwargs)

def main():
    x = 99
if __name__ == '__main__':
    if len(sys.argv) > 1 and sys.argv[1] == '--drop-into-debugger':

That pdb.post_mortem function does the interesting work of inspecting the most recently caught exception. The pdb documentation is pretty dang sparse, but in this case, I got what I needed.

Anyhow, I hope this helps somebody out. I’ll probably be adding a –drop-into-debugger option on every script I write from now on.

The postgreSQL crosstab function is really powerful

I’m just beginning to wrap my brain around it. I’ve got some data that looks like this:

select rr as row_name, dt, c from mm limit 3;
| row_name |    dt    |  c   |
| xxx      | 2009 mar |  552 |
| xxx      | 2009 nov | 2179 |
| xxx      | 2009 jun | 1101 |
(3 rows)

And now I’m going to transpose it like this:

select * from crosstab('select rr as row_name, dt, c from mm limit 3')
as foo(row_name text, dt_1 bigint, dt_2 bigint);
| row_name | dt_1 | dt_2 |
| xxx      |  552 | 2179 |
(1 row)

Neat, right?

What’s wrong with using somebody else’s framework

“I wish that you, oh exalted one, would not be angry with me,” said the
young man. “I have not spoken to you like this to argue with you, to
argue about words. You are truly right, there is little to opinions.
But let me say this one more thing: I have not doubted in you for a
single moment. I have not doubted for a single moment that you are
Buddha, that you have reached the goal, the highest goal towards which
so many thousands of Brahmans and sons of Brahmans are on their way.
You have found salvation from death. It has come to you in the course
of your own search, on your own path, through thoughts, through
meditation, through realizations, through enlightenment. It has not
come to you by means of teachings! And–thus is my thought, oh exalted
one,–nobody will obtain salvation by means of teachings! You will not
be able to convey and say to anybody, oh venerable one, in words and
through teachings what has happened to you in the hour of enlightenment!
The teachings of the enlightened Buddha contain much, it teaches many to
live righteously, to avoid evil. But there is one thing which these so
clear, these so venerable teachings do not contain: they do not contain
the mystery of what the exalted one has experienced for himself, he
alone among hundreds of thousands. This is what I have thought and
realized, when I have heard the teachings. This is why I am continuing
my travels–not to seek other, better teachings, for I know there are
none, but to depart from all teachings and all teachers and to reach my
goal by myself or to die. But often, I’ll think of this day, oh exalted
one, and of this hour, when my eyes beheld a holy man.”

From Siddhartha by Herman Hesse.