Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show only first and last line from traceback

Tags:

python

dsl

I've build a little internal DSL with python. I'm using assert vor validation. If the end user types a wrong parameter the dsl should report whats wrong. At the moment this looks like this:

Traceback (most recent call last):
  File "tests/maskedtimefilter_test/FilterDSL_test.py", line 63, in test_dsl_validation
    input(0): self.regular
  File "/Users/sh/reetz/pythonpath/maskedtimedata/maskedtimefilter.py", line 885, in __lshift__
    kwargs = self.dsl_validation(kwargs)
  File "/Users/sh/reetz/pythonpath/maskedtimedata/maskedtimefilter.py", line 1483, in dsl_validation
    check_if_valid(parameter)
  File "/Users/sh/reetz/pythonpath/maskedtimedata/dsl.py", line 47, in kernel_a
    def kernel_a (x): assert isinstance(x, (list, tuple, np.ndarray)), "kernel must be a list."
AssertionError: kernel must be a list.

But the end users are engineers and no computer scientists. Therefore a minimal Traceback is handy. Is it possible to shrink the Traceback to the essential information (where is the failure and what's the cause) like this?:

Traceback (most recent call last):
  File "tests/maskedtimefilter_test/FilterDSL_test.py", line 63, in test_dsl_validation
    input(0): self.regular
AssertionError: kernel must be a list.

Reluctantly I'd like to use normal prints!

like image 204
Themerius Avatar asked Jan 02 '14 14:01

Themerius


3 Answers

Why not return the traceback data as an array and just work back from that?

import traceback
try:
     codethatwillthrowanexception()
except:
     exceptiondata = traceback.format_exc().splitlines()
     exceptionarray = [exceptiondata[-1]] + exceptiondata[1:-1]
like image 65
Lorcan O'Neill Avatar answered Oct 16 '22 18:10

Lorcan O'Neill


Somewhere in your call stack you can do this:

try:
    my_function_that_can_raise_exception()
except Exception: #or AssertionError
    import traceback
    traceback.print_exc(limit=1)

limit is the depth of how many stack trace entries you want to show. (in your case, 1)

demo:

In [21]: def f():
    ...:     assert False == True, 'Assertion!'
    ...:     

In [22]: try:
    ...:     f()
    ...: except Exception:
    ...:     import traceback
    ...:     traceback.print_exc(limit=1)
    ...:     
Traceback (most recent call last):
  File "<ipython-input-22-78f9db8d5188>", line 2, in <module>
    f()
AssertionError: Assertion!

read more at traceback docs.

like image 26
roippi Avatar answered Oct 16 '22 20:10

roippi


As the first part of the traceback is just what you called, you could so something simple, like:

try: 
    input(0): self.regular # base call
except AssertionError as ae: # catch the exception
    print(ae) # print out the message

You can print out whatever you think would be useful in the except block.

Alternatively, you could do something more complex with the traceback module.

like image 1
jonrsharpe Avatar answered Oct 16 '22 18:10

jonrsharpe