Skip to content

Instantly share code, notes, and snippets.

@miraculixx
Last active December 27, 2015 18:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miraculixx/eb2299bdede8c52b05e1 to your computer and use it in GitHub Desktop.
Save miraculixx/eb2299bdede8c52b05e1 to your computer and use it in GitHub Desktop.
a minimal traceback for ipython interactive shell

mini traceback

Install

%install_ext https://gist.githubusercontent.com/miraculixx/eb2299bdede8c52b05e1/raw/c582151f9d09a343b6e314983230590e6fe82796/minitb.py

Use

%minitb on

Why?

Usual stack traces in ipython are very long, even when using xmode plain. Consider this example:

%xmode plain
def foo(i=0):
    if i > 5:
        pd.DataFrame({'foo' : [0,1,2]}).apply(lambda s : s.bar)
        return
    # simulate deep call graph
    foo(i+1)
foo()

This results in this very long stack trace:

Exception reporting mode: Plain
Traceback (most recent call last):

  File "/Users/patrick/anaconda/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 3066, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)

  File "<ipython-input-57-c15c8485011b>", line 7, in <module>
    foo()

  File "<ipython-input-57-c15c8485011b>", line 6, in foo
    foo(i+1)

  File "<ipython-input-57-c15c8485011b>", line 6, in foo
    foo(i+1)

  File "<ipython-input-57-c15c8485011b>", line 6, in foo
    foo(i+1)

  File "<ipython-input-57-c15c8485011b>", line 6, in foo
    foo(i+1)

  File "<ipython-input-57-c15c8485011b>", line 6, in foo
    foo(i+1)

  File "<ipython-input-57-c15c8485011b>", line 6, in foo
    foo(i+1)

  File "<ipython-input-57-c15c8485011b>", line 4, in foo
    pd.DataFrame({'foo' : [0,1,2]}).apply(lambda s : s.bar)

  File "/Users/patrick/anaconda/lib/python2.7/site-packages/pandas/core/frame.py", line 3972, in apply
    return self._apply_standard(f, axis, reduce=reduce)

  File "/Users/patrick/anaconda/lib/python2.7/site-packages/pandas/core/frame.py", line 4064, in _apply_standard
    results[i] = func(v)

  File "<ipython-input-57-c15c8485011b>", line 4, in <lambda>
    pd.DataFrame({'foo' : [0,1,2]}).apply(lambda s : s.bar)

  File "/Users/patrick/anaconda/lib/python2.7/site-packages/pandas/core/generic.py", line 2360, in __getattr__
    (type(self).__name__, name))

AttributeError: ("'Series' object has no attribute 'bar'", u'occurred at index foo')

With minitb

%minitb
foo()

Traceback reduced to the max:

  File "<ipython-input-57-c15c8485011b>", line 4, in <lambda>
    pd.DataFrame({'foo' : [0,1,2]}).apply(lambda s : s.bar)

  File "/Users/patrick/anaconda/lib/python2.7/site-packages/pandas/core/generic.py", line 2360, in __getattr__
    (type(self).__name__, name))

AttributeError: ("'Series' object has no attribute 'bar'", u'occurred at index foo')

You can always get the full traceback

  • Using the %fulltb magic
%fulltb
(will print the full traceback in all its glory)
  • Switching back to the usual traceback formatter
%minitb off
# next tb will be formatted as usual
from IPython.core.ultratb import AutoFormattedTB
from IPython.core.magic import register_line_magic
# define TB formatted
class MinimalTB(AutoFormattedTB):
def structured_traceback(self, *args, **kwargs):
# get the standard TB and reduce to only the interesting parts,
# i.e. the calling and exception frames
tb = AutoFormattedTB.structured_traceback(self, *args, **kwargs)
interesting = filter(lambda s : 'ipython-input' in s, tb)
result = interesting[-1:]
result += tb[-2:] if not result[0] in tb[-2:] else []
return result
@register_line_magic
def minitb(line, cell=None):
# install
if line == 'off':
get_ipython().InteractiveTB = AutoFormattedTB()
else:
get_ipython().InteractiveTB = MinimalTB()
@register_line_magic
def fulltb(line, cell=None):
mgc = get_ipython().magic
mgc('%minitb off')
mgc('%tb')
mgc('%minitb')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment