Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the seven weapons that make Python code more maintainable?

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about the seven weapons that make Python code easier to maintain. The article is rich in content and analyzes and describes it from a professional point of view. I hope you can get something after reading this article.

As software projects enter "maintenance mode", it is easy to fail the requirements for readability and coding standards (even those standards were not established in the first place). However, maintaining a consistent code style and testing standards in the code library can significantly reduce the pressure on maintenance and ensure that new developers can quickly understand the project while better maintaining the quality of the application throughout.

Using external libraries to check the quality of the code is a good way to protect the future maintainability of the project. The following will recommend some of our favorite libraries for checking code (including checking for PEP 8 and other code style errors) to force code consistency and ensure acceptable test coverage when the project matures.

Check your code style

PEP 8 is a Python code style specification that specifies things like line length, indentation, multiline expressions, variable naming conventions, and so on. Although your team may have its own code style specification that is slightly different from PEP 8, the goal of any code style specification is to enforce consistent standards in the code base, making the code more readable and easier to maintain. The following three libraries can be used to help you beautify your code.

1 、 Pylint

Pylint is a library that checks for violations of the PEP 8 specification and common errors. It is integrated in some popular editors and IDE, and can be run separately from the command line.

Execute pip install pylint to install Pylint. Then run pylint [options] path/to/dir or pylint [options] path/to/module.py to use Pylint on the command line, which outputs code violations and errors to the console.

You can also use the pylintrc configuration file to customize which code errors are checked by Pylint.

2 、 Flake8

Flake8 is "a Python tool that integrates PEP 8, Pyflakes (similar to Pylint), McCabe (code complexity checker), and third-party plug-ins to check the style and quality of Python code."

Execute pip install flake8 to install flake8, and then execute flake8 [options] path/to/dir or flake8 [options] path/to/module.py to view reported errors and warnings.

Similar to Pylint, Flake8 allows you to customize the contents of the check through configuration files. It has very clear documentation, including some useful submission hooks that incorporate automatic checking code into the development workflow.

Flake8 can also be integrated into some popular editors and IDE, but it is not detailed in the documentation. To integrate Flake8 into your favorite editor or IDE, you can search for plug-ins (such as Sublime Text's Flake8 plug-in).

3 、 Isort

Isort this library sorts the libraries you import into the project alphabetically and correctly divides them into different parts (such as standard libraries, third-party libraries, self-built libraries, etc.). This improves the readability of the code, and makes it easy to find each library when there are many imported libraries.

Execute pip install isort to install isort, then execute isort path/to/module.py to run. More configuration items are also provided in the documentation, such as configuring the .isort.cfg file to determine how isort handles the multi-line import of a library.

Like Flake8 and Pylint, isort provides plug-ins that integrate with popular editors and IDE.

Share your code style

It's painful to manually check the code on the command line every time the file changes, and you may not like running a plug-in in IDE to do this. Similarly, your colleagues may use different code reviews, they may not have that kind of plug-in in their editor, and even you may not strictly review the code and correct the code according to the warnings. In short, the code base you share will gradually become messy and difficult to read.

A good solution is to use a library that automatically formats the code according to the PEP 8 specification. All three of our recommended libraries have different levels of customization to control how the code is formatted. Some of these settings are special, such as Pylint and Flake8, and you need to test first to see if there are default configurations that you can't stand but cannot modify.

4 、 Autopep8

Autopep8 can automatically format code in specified modules, including reindenting, fixing indentation, removing extra spaces, and refactoring common comparison errors (such as Boolean and None values). You can view the complete list of corrections in the document.

Run pip install-- upgrade autopep8 to install Autopep8. Then execute autopep8-in-place-aggressive-aggressive to reformat your code. The number of aggressive options indicates how much control Auotopep8 has over code style control. You can learn more about the aggressive options here.

5 、 Yapf

Yapf is another tool for reformatting code with its own list of configuration items. It differs from Autopep8 in that it not only points out violations of the PEP 8 specification in the code, but also reformats areas that do not violate PEP 8 but the code style is inconsistent, in order to make the code more readable.

Execute pip install yapf to install Yapf, and then execute yapf [options] path/to/dir or yapf [options] path/to/module.py to reformat the code. A complete list of customization options is here.

6 、 Black

Black is a relatively new code review tool. It is similar to Autopep8 and Yapf, but with more restrictions and few customization options. The advantage is that you don't have to decide what kind of code style to use, just let Black make the decision for you. You can check out Black's limited customization options here and how to set them in the configuration file.

Black relies on Python 3.6 formats, but it can format code written in Python 2. Execute pip install black to install Black, then execute black path/to/dir or black path/to/module.py to optimize your code using Black.

Check your test coverage

If you are writing tests, you need to make sure that the new code submitted to the code base has passed the test and does not reduce test coverage. Although test coverage is not a measure of test effectiveness and adequacy, it is a way to ensure that projects follow basic test standards. For calculating test coverage, we recommend using the Coverage library.

7 、 Coverage

Coverage has several ways to display test coverage, including outputting the results to the console or HTML page and pointing out which specific areas are not covered. You can customize the content of the Coverage check through the configuration file to make it easier for you to use.

Execute pip install coverage to install Converage. Then execute coverage [path/to/module.py] [args] to run the program and view the output. If you want to see which lines of code are not overwritten, execute coverage report-m.

Continuous integration tool

Continuous integration Continuous integration (CI) is the process of automatically checking for code style errors and minimum test coverage before merging and deploying code. Many free or paid tools can be used to do this, and the specific process is not covered in this article, but the CI process is an important step in making your code easier to read and maintain, and you can refer to Travis CI and Jenkins for this section.

These are the seven weapons that Xiaobian shared to make Python code easier to maintain. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report