Getting terse tracebacks?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Roy Smith

    Getting terse tracebacks?

    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 :-)
  • skip@pobox.com

    #2
    Re: Getting terse tracebacks?


    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_traceb ack). I used
    it as the basis for a compact stack printer:

    import os
    import sys

    # adapted from asyncore.compac t_traceback()
    def compact_stack(s tart=0, size=100):
    """build a compact stack trace from frame start to end"""
    f = sys._getframe(s tart+1) # don't include this frame
    info = []
    while f:
    fn = os.path.normpat h(os.path.abspa th(f.f_code.co_ filename))
    info.append((fn , f.f_code.co_nam e, 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_s tack(f=sys.stde rr, start=0, size=100):
    print >> f, compact_stack(s tart+1, size)

    Skip

    Comment

    Working...