Python5 kinds of code maintenance tool

Original Address: https://cloud.tencent.com/developer/article/1365969

With the software project into the "maintenance mode", the requirements of readability and coding standards can easily come to nothing (even from the outset, those standards have not been established). However, consistency in the code library code style and testing standards can significantly reduce the maintenance of pressure, but also to ensure that the new developers to quickly understand the project, while better able to keep the whole quality of the application.

A good way to future maintainability use external libraries to check the quality of the code after all conservation projects. The following will recommend some of our favorite checking code (including checking PEP 8 style errors and other code) of the library, use them to enforce consistent coding style, and to ensure that there is an acceptable test coverage as the project matures.

Check your coding style

PEP 8 is a Python style specification, which provides for a similar length of lines, indentation, multi-line expression, variable naming conventions and so on. Although your team itself may also have slightly different specifications PEP 8 style of code, but any objective standard coding style is to enforce consistent standards in the code base, makes the code more readable and easier to maintain . The following three libraries can be used to help you beautify code.

1、 Pylint

Pylint is a violation of PEP 8 inspection norms and common errors library. It has integrated in some of the popular editor and IDE, it can also be run separately from the command line.

Execution pip install pylint installation Pylint. Then run pylint [options] path / to / dir or pylint [options] path / to / module.py Pylint you can use the command line, it would violate the norms and the wrong place to appear console output code.

You can also use the configuration file to customize Pylint pylintrc which codes for error checking.

2, Flake8

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

Execution pip install flake8 installation flake8, and then perform flake8 [options] path / to / dir or flake8 [options] path / to / module.py can see the reported errors and warnings.

And Pylint Similarly, Flake8 allows content profile defined from examination. It has a very clear documentation, including some useful commit hook, automatically check code can be incorporated into development processes.

Flake8 can also be integrated into some of the popular among editors and IDE, but did not explain in detail in the document. To integrate Flake8 to your favorite editor or IDE, you can search plug-ins (such as Sublime Text of Flake8 plug-in).

3、 Isort

Isort this library you can import the library in the project, sorted in alphabetical order, and properly divided into different parts (such as standard libraries, third party libraries, self-built libraries, etc.). This improves the readability of the code, and various libraries can be easily found when importing large libraries.

Execution pip install isort installation isort, and then perform isort path / to / module.py can run. The document also provides more configuration options, such as isort to decide how to deal with a multi-line library by importing configuration files .isort.cfg.

And Flake8, as Pylint, isort also provided to integrate with popular editing and IDE plug-ins.

Share them with the command line to manually check the code after your code style changed each time a file is a painful thing, you might not like to achieve this by running a plug-in IDE. Likewise, your co-worker may use different codes to check the way, perhaps their editors nor the kind of plug-ins, you probably will not even check the code and follow strict warning to correct the code. In short, you share out the code base will gradually become cluttered and hard to read.

A good solution is to use a library, the code will be automatically formatted according to PEP 8 specifications. We recommend the library has three different levels to control how custom formatting code. Some of these settings are more specific, such as Pylint and Flake8, you need to first test to see if you can not stand there but can not modify the default configuration.

4, Autopep8

Autopep8 can automatically format the code specified in the module, including re-indented, indented repair, remove the extra spaces, and reconstructs common comparison errors (such as Boolean values ​​and None value). You can view the complete list of correct documents.

Run pip install --upgrade autopep8 installation Autopep8. Then perform autopep8 --in-place --aggressive --aggressive can reformat your code. The number of aggressive option indicates Auotopep8 have control over how much control on code style. Here you can learn more aggressive options.

5, Yapf

Yapf there is another tool to reformat the code of its own configuration item list. It differs from Autopep8 that it will not only point out the code violation PEP 8 standard place, there will be no violation of the PEP 8 style place but inconsistent code reformatting, designed to make the code more readable.

Performing pip install yapf installation Yapf, then perform yapf [options] path / to / dir or yapf [options] path / to / module.py reformat code. For a complete list of customization options here.

Guess you like

Origin www.cnblogs.com/boonya/p/11826394.html