Lame opening remarks
For me, who is used to compiled languages like Java and used to dynamic languages like Python, errors can often only be found at execution time.
In addition, some errors are hidden deeply and can only be triggered by specific logic. As a result, it often takes a lot of time to find out grammar errors slowly. In fact, some errors are obvious, and if you can find them when writing a program, you can improve your work efficiency.
This is where the Python static syntax checker comes in.
This article uses the code in Python to help you fill out your gaokao application. Some of the output was too long and was intercepted.
1 pep8 / pycodestyle
You’ve all seen PEP 8 at some point, but what is PEP 8?
Actually the PEP 8 is a Python code specification guidelines, you can refer to the website: https://www.python.org/dev/peps/pep-0008/, its purpose is to keep the consistency and readability of the code.
A simple tool to check your code for compliance with the PEP 8 specification is pep8.
1.1 installation
$ pip install pep8
Copy the code
Pep8 is found to give a warning when used:
Pep8 has been replaced by PyCodeStyle! Pycodestyle = pyCodeStyle = pyCodeStyle
1.2 the use of
$ pycodestyle [file name or directory name]
-
— Statistics-QQ: Summarize the results
-
–show-source: more verbose output
-
–ignore: ignores the specified output
1.3 Description of Error Codes
-
E: * * * mistakes
-
W * * * : warning
-
10* : indentation problem
-
20* : space problem
-
30* : Blank line problem
-
40* : Import problem
-
50* : Line length problem
-
60* : deprecated
-
70* : Declaration problem
-
90* : syntax error
2 Pyflakes
A simple program to check Python source files for errors.
Pyflakes analyzes programs and checks for errors. It is implemented by parsing the source file without importing it, so it is safe to use in modules without any side effects.
-
Code style is not checked
-
Because it checks each file individually, it is also fairly fast, but the scope of detection is limited
2.1 installation
pip install pyflakes
Copy the code
2.1 the use of
$ pyflakes [ file name or directory name]
I like Pyflakes because MY own code has its own style and you just have to check me for errors
3 Pylint
PyLint is a Python source code analyzer that analyzes Python code for errors and looks for code that does not conform to code style standards or has potential problems. PyLint is a tool that can be used to validate modules and packages for multiple files.
By default, PyLint enables many rules. It is highly configurable, controlled from within the code by handlers. In addition, it is possible to write plug-ins to add to your own checks.
3.1 installation
$PIP install pylint$pylint -- versionPylint 2.0.0 AstroID 2.0.1Python 3.5.2 (default, Nov 23 2017, 16:37:01) [GCC 5.4.0 20160609]
Copy the code
3.2 the use of
pylint [options] module_or_package
Pylint also gives a score to your code as a whole, so you can follow the prompts to improve your score! Out of 10.
If you run Pylint twice, it displays both the current and last run results to see if the code quality has improved.
3.3 Meaning of Error Codes
-
C: Convention, violating coding style standards
-
R: Refactoring, the code is terrible
-
W: Warning, some Python-specific problems
-
E: Error, probably in the code
-
F: Fatal error, an error that prevents Pylint from running further
4 flake8
Flake8 is a tool released by Python to help check whether Python code is standardized. Compared with Pylint, Flake8 has flexible checking rules, supports the integration of additional plug-ins, and has strong scalability. Flake8 is a wrapper around the following three tools:
-
PyFlakes: tool for statically checking Python code for logic errors.
-
Pep8: Tool for static checking Pep8 coding style.
-
NedBatchelder’s McCabe: A tool for statically analyzing Python code complexity.
In addition to encapsulation of the above three tools, Flake8 also provides an extended development interface.
The official document: https://pypi.python.org/pypi/flake8/
4.1 installation
4.2 the use of
Flake8 [file name or directory name]
PyFlakes and Pep8 output will be returned together. You can see flake8 not only checks for code errors, but also for code specification errors, such as a line of code that is too long.
Flake8 provides an extension option: –max-complexity, which raises an alarm if the McCabe complexity of a function is higher than a given value. This feature is useful for finding overly complex code. According to Thomas J. McCabe, Sr, code complexity should not exceed 10, while Flake8’s official website recommends 12.
-
McCabe complexity is not output by default and is specified with –max-complexity:
-
–ignore ignores specified output:
-
–select displays only the specified output:
4.3 Error Code Meaning
Flake8 basic error return codes have three types:
-
E*** / W*** : Error and warning in PEP8.
-
F*** : Indicates errors that PyFlakes detect. PyFlakes do not provide error return codes. Flake8 classifies error messages returned by PyFlakes.
-
C9** : Code complexity detected by McCabe.
conclusion
There are more than a few Python static code checking tools, and you can choose which one you want to use. I have not carried out in-depth exploration, interested in advanced use can refer to the official website.
In addition, each tool should have a corresponding plug-in, such as vim, vscode, eclipse, pycharm should be integrated with the above tools, you can find online plug-ins suitable for your ide to install.
Some people might be annoyed to see so many exceptions, but after all, you can develop code specifications. After the follow-up work, there will be corresponding code specifications, so I suggest you form a habit.
That philosopher is me, by the way.
Share my Python learning resources
Are you still using format to format strings?
Python to spin the Excel
Python crawler entry (level 1)
Python crawler Entry (level 2)
Python crawler Entry (Level 3)