python pretty-errors模块

一行代码优化输出的异常信息

pip install pretty-errors
写一个函数测试:

def divided_zero():
    for i in range(10, -1, -1):
        print(10/i)


divided_zero()
在没有import这个pretty-errors前,输出的错误信息有些冗余:

Traceback (most recent call last):
  File "c:\Users\HUAWEI\.vscode\extensions\ms-python.python-2019.11.50794\pythonFiles\ptvsd_launcher.py", line 43, in <module>
    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 <module>
    divided_zero()
  File "d:\source\sorting-visualizer-master\sorting\debug_test.py", line 3, in divided_zero
    print(10/i)
ZeroDivisionError: division by zero

我们使用刚安装的pretty_errors,import下:
import pretty_errors def divided_zero(): for i in range(10, -1, -1): print(10/i) divided_zero()
此时看看输出的错误信息,非常精简只有2行,去那些冗余信息: ZeroDivisionError: division by zero

 

posted on 2020-04-02 13:07  不要挡着我晒太阳  阅读(989)  评论(0编辑  收藏  举报

导航