Seven Python libraries that make code more maintainable

  • 2020-12-10 00:47:50
  • OfStack

As software projects move into "maintenance mode," requirements for readability and coding standards can easily fall apart (even if those standards have not been established since 1). However, maintaining a consistent code style and testing standards in the code base can significantly reduce maintenance stress and ensure that new developers can quickly learn about the project while maintaining better application quality throughout.

Using an external library to check the quality of your code is a good way to protect your project's future maintainability. Here are some of our favorite libraries for checking code, including PEP 8 and other code style errors, to enforce code style adherence and to ensure an acceptable test coverage as the project matures.

Check your code style

PEP 8 is the Python code style specification, which specifies things like line length, indentation, multi-line 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 a 1-to-1 standard in the code base to make the code more readable and maintainable. The following three libraries can be used to help you beautify your code.

1, Pylint

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

perform pip install pylint Install Pylint. Then run pylint [options] path/to/dir or pylint [options] path/to/module.py You can use Pylint on the command line, which prints out code violations and errors to the console.

You can also use the pylintrc configuration file to customize Pylint to check for code errors.

2, Flake8

Flake8 is "an Python tool that integrates PEP 8, Pyflakes (like Pylint), McCabe (code complexity checker), and third party plug-ins into one to check Python code style and quality".

perform pip install flake8 Install flake8 and execute flake8 [options] path/to/dir or flake8 [options] path/to/module.py You can view the reported errors and warnings.

Like Pylint, Flake8 allows you to customize the content checked through configuration files. It has very clear documentation, including a number of useful submission hooks that can incorporate automated inspection code into the development workflow.

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

3, Isort

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

perform pip install isort Install isort, then execute isort path/to/module.py It's ready to run. Additional configuration items are provided in the documentation, such as configuring the.isort.cfg file to determine how isort handles multi-line imports for a library.

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

Share your code style

Manually checking the code from the command line after every file change is a pain, and you may not like running one of the plug-ins in IDE to do this. Likewise, your colleagues may check code differently, they may not have a plug-in in their editor, and you may not even be able to rigorously check your code and correct it with warnings. In short, the code base you share will gradually become cluttered and difficult to read.

A good solution is to use a library that automatically formats the code according to the PEP 8 specification. Each of the three libraries we recommend has a different level of customization to control how the code is formatted. One of these Settings is special, such as Pylint and Flake8, and you need to test first to see if there is a default configuration that you can't stand but can't change.

4, Autopep8

Autopep8 automatically formats code in a specified module, including reindentation, fixing indentation, removing excess white space, and refactoring common comparison errors (such as Boolean and None values). You can check out the full list of corrections in the documentation.

run pip install --upgrade autopep8 Install Autopep8. Then perform autopep8 --in-place --aggressive --aggressive <filename> You can 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 option here.

5, Yapf

Yapf is another tool for reformatting code that has its own list of configuration items. It differs from Autopep8 in that it not only indicates violations of the PEP 8 specification in the code, but also reformats the code where it does not violate PEP 8 but in a different style, in order to make the code more readable.

perform pylint [options] path/to/dir 0 Install Yapf, then execute yapf [options] path/to/dir or yapf [options] path/to/module.py You can reformat the code. The full list of customization options is here.

6, Black

Black is one of the newer code checking tools. It is similar to Autopep8 and Yapf, but is more restrictive and doesn't have many customization options. The advantage is that you don't have to decide what style of code to use, just let Black decide for you. You can check out the Black's limited customization options and how to set it in a configuration file here.

Black relies on Python 3.6+, but it can format code written with Python 2.

perform pip install black Install Black, then execute black path/to/dir or black path/to/module.py You can use Black to optimize your code.

Check your test coverage

If you are writing tests, you need to make sure that any new code that is submitted to the code base passes the tests and does not reduce test coverage. While test coverage is not the only measure of test effectiveness and adequacy, it is one way to ensure that the project complies with basic test criteria. For calculating test coverage, we recommend using the Coverage library.

7, Coverage

Coverage shows test coverage in several ways, including printing the results to the console or HTML page and indicating which specific areas are not covered. You can customize the Coverage check content through the configuration file, making it easier for you to use.

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

Continuous integration tool

Continuous integration (Continuous integration) (CI) is the process of automatically checking code style errors and minimum test coverage before merging and deploying code. Many free or paid tools can be used to do this, and I won't go into the details in this article, but the CI process is an important step in making your code more readable and maintainable. You can refer to Travis CI and Jenkins for this part.

conclusion


Related articles: