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 scratch.py 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 scratch.py --drop-into-debugger
> /home/matt/scratch.py(15)main()
-> 1/0
(Pdb)

When I don’t add the --drop-into-debugger option, then of course the uncaught exception just crashes the script:
$ python scratch.py
Traceback (most recent call last):
File "scratch.py", line 23, in
main()
File "scratch.py", line 15, in main
1/0
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

@decorator
def into_debugger(f, *args, **kwargs):
try:
return f(*args, **kwargs)
except:
pdb.post_mortem()

def main():
x = 99
1/0

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

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.