Home  >  Article  >  Backend Development  >  One line of code to make your Python bug unique

One line of code to make your Python bug unique

WBOY
WBOYforward
2023-04-14 09:16:02640browse

PrettyErrors is a tool for streamlining Python error messages. It is characterized by a very simple and friendly interface.

Its most significant function is to support color output in the terminal, mark out file stack traces, find error messages, filter out redundant information, extract key parts, and color mark them, thereby improving developers' s efficiency.

Writing code itself is not easy, especially if a bug appears in tens of thousands of lines of code, and you will not be able to find the problem for a while. At this time, you must be very crazy and irritable. Especially when there is an error in the Python code, the screen is full of error messages, making it even more difficult to locate the error.

One line of code to make your Python bug unique

Let’s take a look at the above traceback first

  • There is only one color, which cannot be used like code highlighting, and it is too unfriendly to the naked eye.
  • The error code cannot be directly displayed, troubleshooting is too slow, and the efficiency is too low

Is there a way to solve these problems?

Of course, in Python, there is no problem that a library cannot solve. Don’t mess with Python’s error output. One line of code can make the bug clearer.

The library we are going to introduce today is called pretty-errors. As you can tell from its name, its purpose is to beautify error messages.

One line of code to make your Python bug unique

The following describes how to install and use PrettyErrors.

With this command you can install it

linuxmi@linuxmi:~/www.linuxmi.com$ pip install pretty_errors

or

linuxmi@linuxmi:~/www.linuxmi.com$ pip3 install pretty-errors

One line of code to make your Python bug unique

Global configuration

if you want Each of your programs can make it clear and easy to read when reporting errors. Then run the following line of commands to configure it to be globally available.

linuxmi@linuxmi:~/www.linuxmi.com$ python3 -m pretty_errors

One line of code to make your Python bug unique

After the configuration is completed, if you run any script, the traceback will be automatically beautified.

Cancel global configuration

Run this command also

linuxmi@linuxmi:~/www.linuxmi.com$ python3 -m pretty_errors

One line of code to make your Python bug unique

Enter C to clear the global configuration.

Use

in a single file. After canceling global availability, you can import pretty_errors into the script file where you need to use pretty-errors according to your needs, and you can use

import pretty_errors

But in this way, the format of syntax error (SyntaxError) cannot be beautified. So in order to make the beautification more thorough, the official recommendation is that you use python -m pretty_errors

If you don’t like the default configuration, try these functions:

pretty_errors.configure()
pretty_errors.whitelist()
pretty_errors.blacklist()
pretty_errors.pathed_config()

For example, if you want to change the color of the output file name, the code It’s like this:

pretty_errors.configure(filename_color = pretty_errors.BRIGHT_YELLOW)

If you find that nothing has changed after the above operations, then check PYTHON_PRETTY_ERRORS and treat it as When the value is 0, PrettyErrors are disabled.

set PYTHON_PRETTY_ERRORS = 1

It should be noted that the terminal you are using has a color output function, so the exception information output will have different colors. If you happen to be used to a monochrome terminal, you can try the settings in pretty_errors.mono().

Custom configuration example

from pretty_errors import *
configure(filename_color=BRIGHT_BLUE)# 设置文件名为亮蓝色
def f():
return 1 / 0
if __name__ == "__main__":
f()

One line of code to make your Python bug unique

Conclusion

In general, this library is very powerful and the effect is very cool. , it is the same as the PEP8 specification. It is okay without it, but it will be better with it. For some people who want to customize error output scenarios, pretty_errors can be a good solution.

The above is the detailed content of One line of code to make your Python bug unique. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:51cto.com. If there is any infringement, please contact admin@php.cn delete