How does python implement code checking

  • 2021-07-03 00:43:05
  • OfStack

Preface

Usually, our python code follows the standardized format of PEP8, in order to maintain the uniformity and readability of the code. Here, we recommend several commonly used static code inspection tools, and you can choose to use them as appropriate

1. pylint

Pylint is an Python static code analysis tool that finds programming errors, helps enforce coding standards, sniffs code odors, and provides simple refactoring recommendations.

It is highly configurable, with special compilation instructions to control errors and warnings in code, and a wide range of configuration files. You can also write your own plug-in to add your own checks or extend pylint in some way.

By default, PyLint enables many rules. It is highly configurable, and it is controlled from the internal handler of the code. In addition, it is possible to write plug-ins to add to your own checks.

Installation


pip install pylint
# If you are using Python 3.6+ Upgrade to get full support for your version: 
pip install pylint --upgrade
# If you want to install from the source distribution, unzip tarball And run the following command 
python setup.py install

Document

Pylint comes with the following additional commands:

pyreverse: 1 UML Graph Generator symilar: 1 Independent Similarity Checker epylint: Pylint compatible with Emacs and Flymake

Use

pylint all kinds of references and usage can go to the document search, here only introduces a simple example of comparison foundation


pylint [options] modules_or_packages

Command line options

参数 含义
--version 显示程序的版本号并退出
-h, --help 显示有关命令行选项的帮助
--ignore=<file[,file...]> 将文件或目录添加到黑名单。它们应该是基本名称,而不是路径。
--output-format=<format> 选择输出格式(文本,json,自定义)。
--list-msgs 生成pylint的消息。

Error code meaning

C: Convention, violation of coding style standard R: Refactoring, the code is very bad W: Warning, some Python-specific issues E: Error, most likely in code F: Fatal error that prevents Pylint from going one step further

For more pylint related documents, please refer to the documentation

2. yapf:

Most of the current formatters for Python--for example, autopep8 and pep8ify--are used to remove lint errors from your code. This has one obvious limitation. For example, code that conforms to the PEP 8 guidelines may not be reformatted. But that doesn't mean the code looks good.

YAPF takes a different approach. It is based on 'clang-format' developed by Daniel Jasper. Essentially, the algorithm takes the code and reformats it to the best format that conforms to the style guidelines, even if the original code does not violate the style guidelines. The idea is similar to the 'gofmt' tool of the Go programming language: End all sacred wars about formatting

If the entire code base of the project is modified only through YAPF, as long as the modification is made, the style remains the same throughout the project, and there is no need to argue about the style in every code review.

The ultimate goal is that the code generated by YAPF is as good as the code programmers write when following the style guidelines. It eliminates the chore of maintaining code.
Installation


pip install yapf

Use


yapf [-h] [-v] [-d | -i] [-r | -l START-END] [-e PATTERN]
[--style STYLE] [--style-help] [--no-local-style] [-p]
[-vv]
[files [files ...]]

Same as above, sample 1 can also be directly operated with files

参数 参数说明
-h, --help 显示此帮助消息并退出
-v, --version 显示版本号并退出
-d, --diff 比较差异
-i, --in-place 对文件进行更改
-r, --recursive 以递归方式运行目录

Example


# Follow the file name directly (does not modify the file) 
yapf <python file>
# Contrast before and after formatting 
yapf -d <python file>
# Modify the source file directly 
yapf -i <python file>
# Export Configuration File 
yapf --style-help > style.cfg
# Formatting multiple files concurrently requires futures Module support 
yapf -pi *.py

For more information about yapf, please refer to yapf

Other such tools are Pyflakes, flake8 and so on, this site directly out to these two commonly used, more content we should have more contact in actual combat projects


Related articles: