Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to optimize the python exception output

Shulou Source: shulou.com Published: 2022-05-31 14:12:20 09月29日 Update

This article mainly explains "how to optimize the python exception output". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to optimize the python exception output.

Abnormal information output by one line of code optimization

Pip install pretty-errors

Write a function test:

Def divided_zero ():

For i in range (10-1,-1):

Print (10Universe)

Divided_zero ()

Before the pretty-errors of import, the error message output was somewhat redundant:

Traceback (most recent call last):

File "c:\ Users\ HUAWEI\ .vscode\ extensions\ ms-python.python-2019.11.50794\ pythonFiles\ ptvsd_launcher.py", line 43, in

Main (ptvsdArgs)

File "c:\ Users\ HUAWEI\ .vscode\ extensions\ ms-python.python-2019.11.50794\ pythonFiles\ lib\ python\ old_ptvsd\ ptvsd\ _ _ main__.py"

Line 432, in main

Run ()

File "c:\ Users\ HUAWEI\ .vscode\ extensions\ ms-python.python-2019.11.50794\ pythonFiles\ lib\ python\ old_ptvsd\ ptvsd\ _ _ main__.py"

Line 316, in run_file

Runpy.run_path (target, run_name='__main__')

File "D:\ anaconda3\ lib\ runpy.py", line 263, in run_path

Pkg_name=pkg_name, script_name=fname)

File "D:\ anaconda3\ lib\ runpy.py", line 96, in _ run_module_code

Mod_name, mod_spec, pkg_name, script_name)

File "D:\ anaconda3\ lib\ runpy.py", line 85, in _ run_code

Exec (code, run_globals)

File "d:\ source\ sorting-visualizer-master\ sorting\ debug_test.py", line 6, in

Divided_zero ()

File "d:\ source\ sorting-visualizer-master\ sorting\ debug_test.py", line 3, in divided_zero

Print (10Universe)

ZeroDivisionError: division by zero

We use the pretty_errors,import we just installed:

Import pretty_errors

Def divided_zero ():

For i in range (10-1,-1):

Print (10Universe)

Divided_zero ()

At this point, take a look at the output error message, very concise only 2 lines, to those redundant information:

ZeroDivisionError:

Division by zero

The complete output is shown in the picture below, with a split line in the middle, and then output the stack of the exception, followed by a simplified exception name and prompt.

At this point, I believe that everyone on the "python exception output how to optimize" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Output information content redundancy error learning practical deeper one line one line code interest function split line only name picture stack practicality actual Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux MariaDB Microsoft Redmi vpn