Limit Output Of Python Traceback
Aug 14, 2021
The aim of this playbook🏁 is to limit output in python’s stack trace.
1. steps/notes
- traceback is another name for stack trace
raise
keyword returns the whole trace, which is a bit too much for this instance
Enter New Section Name (e.g. 00 SnowBar): Snow
Traceback (most recent call last):
File ".\helpers\createSections.py", line 18, in <module>
createSections()
File ".\helpers\createSections.py", line 13, in createSections
raise ValueError(
ValueError: The first 2 characters need to be a sectionID, e.g. 66
- adding
sys.tracebacklimit = 0
, I am getting only the last line
ValueError: The first 2 characters need to be a sectionID, e.g. 66
- this is exactly right!
2. example
- just a simple input validation instructing to start a section name with a double-digit
def _validate_and_capitalize(name, sectionFolders):
sys.tracebacklimit = 0
if not re.match(r"\d\d\s.*", name):
raise ValueError(
f"The first 2 characters need to be a sectionID, e.g. 66")