Tue, 24 Jun 2003
Handy debugging snippets

From Anthony, btw, for debugging purposes, I recommend the following snippet:

import traceback
def compact_stack():
    stack = traceback.extract_stack()
    stack = [ '%s:%d'%(x[0], x[1]) for x in stack ]
    return "Stack\n" + "\n".join(stack)

I've also used the following as a generic debug() call in Zope code (though any logging package could replace zLOG here):

def debug(*args):
    module, line, function, info = traceback.extract_stack()[-2]
    if len(args) == 1:
        s = pprint.pformat(args[0])
    else:
        s = pprint.pformat(args)
    file = os.path.split(module)
    LOG('\n%s\n in %s, line %s, debug info:'%(module, function, line),
        INFO, '\n'+s)

That traceback.extract_stack function is so useful :)

path: /python | permanent link |