Remove calls from the traceback

51 Views Asked by At

I have a small class in geometry.py:

class Pyramid():
    def __init__(self, sign, name):
        if len(sign.split(".")) != 2:
            raise ValueError("pyramid must have 1 apex and 1 base")
        ...

The constructor ensures that the pyramid has 1 apex and 1 base.

S = geometry.pyramid("S.ABCD", "S")

When something wrong with the if statement, such as:

S = geometry.pyramid("S.A.BCD", "S")

It will error and print a traceback:

Traceback (most recent call last):
  File "d:\Main\WORK STATION\PYTHON\GEOMETRY\main.py", line 13, in <module>
    S = geometry.pyramid("S.A.BCD", "S")
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "d:\Main\WORK STATION\PYTHON\GEOMETRY\geometry.py", line 46, in __init__
    raise ValueError("pyramid must have 1 apex and 1 base")
ValueError: pyramid must have 1 apex and 1 base

The traceback shows calls in 2 files, but i only want to keep the error description (last line) and the first call, and hide the most recent one. I would prefer if I only need to edit geometry.py, instead of main.py or both.

Expected traceback:

Traceback (most recent call last):
  File "d:\Main\WORK STATION\PYTHON\GEOMETRY\main.py", line 13, in <module>
    S = geometry.pyramid("S.A.BCD", "S")
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: pyramid must have 1 apex and 1 base

I tried to use sys.tracebacklimit, but it was not really helpful.

0

There are 0 best solutions below