Getting terse tracebacks?

R

Roy Smith

Is there any way to make the traceback printer built into the
interpreter elide all the directories in pathnames (like strip_dirs()
does for the profiler)?

I'm working in a deep directory tree, and the full pathnames to my
python source files are pushing 150 characters. I either need a
laptop with a wider screen, or younger eyes so I can read a smaller
font :)
 
S

skip

Roy> Is there any way to make the traceback printer built into the
Roy> interpreter elide all the directories in pathnames (like
Roy> strip_dirs() does for the profiler)?

There's a compact traceback printer in asyncore (compact_traceback). I used
it as the basis for a compact stack printer:

import os
import sys

# adapted from asyncore.compact_traceback()
def compact_stack(start=0, size=100):
"""build a compact stack trace from frame start to end"""
f = sys._getframe(start+1) # don't include this frame
info = []
while f:
fn = os.path.normpath(os.path.abspath(f.f_code.co_filename))
info.append((fn, f.f_code.co_name, f.f_lineno))
f = f.f_back

# eliminate any common prefix the filenames share
info = [(f.split(os.sep)[-1], c, l) for (f, c, l) in info]

return '\n'.join(['[%s|%s|%d]' % x for x in info[:size]])

def print_compact_stack(f=sys.stderr, start=0, size=100):
print >> f, compact_stack(start+1, size)

Skip
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top