Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use YAPF to make Python code change from ugly to beautiful in an instant

Shulou Source: shulou.com Published: 2022-06-02 03:17:18 10月03日 Update

This article is about how to use YAPF to make Python code from ugly to beautiful instantly, the editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.

It's painful to remember the PEP8 specification, but luckily Google has released a tool that automatically organizes Python code to conform to the PEP8 specification: YAPF.

Today I will teach you how to use this plug-in under vscode.

The first step is to install and configure YAPF.

Enter the command: pip install yapf, install yapf

Then in File- > Preferences- > Settings, type python.formatting.provider

Then select yapf to complete the configuration. This is shown in the following figure.

Step 2: in the Python code file that needs to be sorted out.

The code can be sorted out by using the shortcut key: Alt+Shift+F.

(pre-finishing code)

(code after Alt+Shift+F)

The ultimate goal of YAPF is to generate code that is the same as code written by programmers who follow the code specification, greatly reducing the pain of maintaining the code.

After the code of a project is optimized by YAPF, the code style is unified immediately, and there is no need to argue about the code style.

For example, it was previously reported that "due to irregular code, programmers shot four colleagues and one was in critical condition."

The above is how to use YAPF to make Python code from ugly to beautiful in an instant. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

Tags: Code beautiful ugly painful more knowledge article style work input configuration critical practical necessary things colleagues commands that is work meetings tools Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi NVidia OPPO Reno MariaDB Xiaomi