Python 经常使用静态代码检查工具简介

对于我这种习惯了 Java 这种编译型语言,在使用 Python 这种动态语言的时候,发现错误常常只能在执行的时候发现,总感受有点不放心。html

并且有一些错误因为隐藏的比较深,只有特定逻辑才会触发,每每致使须要花不少时间才能将语法错误慢慢排查出来。其实有一些错误是很明显的,假如能在写程序的时候发现这些错误,就能提升工做效率。python

这时候 Python 静态语法检查工具就出现了。git

本文使用以前文章Python 助你填写高考志愿中的代码做为测试代码。另外有些输出过长的,进行了截取。程序员

 

pep8/pycodestylegithub

相信你们多多少少都见过 PEP 8,那 PEP 8 究竟是个啥?json

其实 PEP 8 是一种 Python 代码规范指南,能够参阅官网:https://www.python.org/dev/peps/pep-0008/,其目的是为了保持代码的一致性、可读性。vim

检查本身代码是否符合 PEP 8 规范,一个简单的工具就是:pep8。安全

 

安装eclipse

$ pip install pep8

在使用时发现 pep8 给出了一个警告:ide

$ pep8 gkcx.py
/usr/local/lib/python3.5/dist-packages/pep8.py:2124: UserWarning:

pep8 has been renamed to pycodestyle (GitHub issue #466)
Use of the pep8 tool will be removed in a future release.
Please install and use `pycodestyle` instead.

$ pip install pycodestyle
$ pycodestyle ...

  '\n\n'

意思是 pep8 已被 pycodestyle 替代!

 

使用

基本使用方法:$ pycodestyle [file name or directory name]

$ pycodestyle gkcx.py
gkcx.py:11:80: E501 line too long (135 > 79 characters)
gkcx.py:14:1: E302 expected 2 blank lines, found 1
gkcx.py:15:80: E501 line too long (94 > 79 characters)
...省略部分
gkcx.py:67:1: E305 expected 2 blank lines after class or function definition, found 1
gkcx.py:71:80: E501 line too long (100 > 79 characters)
gkcx.py:82:25: E231 missing whitespace after ','
gkcx.py:82:29: E231 missing whitespace after ','
gkcx.py:84:1: W293 blank line contains whitespace
gkcx.py:84:1: W391 blank line at end of file
  • 参数 --statistics -qq :对结果进行汇总
$ pycodestyle gkcx.py --statistics -qq
3       E203 whitespace before ':'
1       E225 missing whitespace around operator
16      E231 missing whitespace after ':'
3       E302 expected 2 blank lines, found 1
1       E305 expected 2 blank lines after class or function definition, found 1
6       E501 line too long (135 > 79 characters)
1       W291 trailing whitespace
1       W293 blank line contains whitespace
1       W391 blank line at end of file
  • 参数 --show-source:更详细的输出
$ pycodestyle gkcx.py --show-source
gkcx.py:14:1: E302 expected 2 blank lines, found 1
def get_school_id(school_name):
^
gkcx.py:15:80: E501 line too long (94 > 79 characters)
    Referer = "https://gkcx.eol.cn/soudaxue/queryschool.html?&keyWord1={}".format(school_name)
                                                                               ^
gkcx.py:18:19: E203 whitespace before ':'
        "messtype" : "jsonp",
                  ^
gkcx.py:19:12: E231 missing whitespace after ':'
        "_":"1530074932531",
           ^
...省略部分
  • 参数 --ignore:忽略指定输出
$ pycodestyle gkcx.py --ignore=E225,E501,E231
gkcx.py:14:1: E302 expected 2 blank lines, found 1
gkcx.py:18:19: E203 whitespace before ':'
gkcx.py:20:19: E203 whitespace before ':'
gkcx.py:21:19: E203 whitespace before ':'
gkcx.py:32:1: E302 expected 2 blank lines, found 1
gkcx.py:41:1: E302 expected 2 blank lines, found 1
gkcx.py:55:15: W291 trailing whitespace
gkcx.py:67:1: E305 expected 2 blank lines after class or function definition, found 1
gkcx.py:84:1: W293 blank line contains whitespace
gkcx.py:84:1: W391 blank line at end of file

 

错误码含义

  • E...:错误
  • W...:警告
  • 100 型:缩进问题
  • 200 型:空格问题
  • 300 型:空行问题
  • 400 型:导入问题
  • 500 型:行长度问题
  • 600 型:已弃用
  • 700 型:声明问题
  • 900 型:语法错误

 

Pyflakes

一个用于检查 Python 源文件错误的简单程序。

Pyflakes 分析程序而且检查各类错误。它经过解析源文件实现,无需导入它,所以在模块中使用是安全的,没有任何的反作用。

  • 不会检查代码风格
  • 因为它是单独检查各个文件,所以它也至关的快,固然检测范围也有必定的局限

 

安装

pip install pyflakes

 

使用

$ pyflakes [ file name or directory name]

$ pyflakes gkcx.py
gkcx.py:3: 'bs4.BeautifulSoup' imported but unused
gkcx.py:6: 're' imported but unused

 

Pylint

PyLint 是 Python 源代码分析器,能够分析 Python 代码中的错误,查找不符合代码风格标准和有潜在问题的代码,是一个能够用于验证多个文件的模块和包的工具。

缺省状况下,PyLint 启用许多规则。它具备高度可配置性,从代码内部处理程序控制它。另外,编写插件添加到本身的检查中是可能的。

 

安装

$ pip install pylint

$ pylint --version
pylint 2.0.0
astroid 2.0.1
Python 3.5.2 (default, Nov 23 2017, 16:37:01)
[GCC 5.4.0 20160609]

 

使用

基本使用:pylint [options] module_or_package

$ pylint gkcx.py
************* Module gkcx
gkcx.py:11:0: C0301: Line too long (135/100) (line-too-long)
gkcx.py:25:47: C0326: Exactly one space required after comma
    response = requests.request("GET", data_url,headers=headers,params=params)
                                               ^ (bad-whitespace)
gkcx.py:36:0: C0301: Line too long (113/100) (line-too-long)
gkcx.py:1:0: C0111: Missing module docstring (missing-docstring)
gkcx.py:10:0: C0103: Constant name "headers" doesn't conform to UPPER_CASE naming style (invalid-name)
...省略部分
gkcx.py:14:18: W0621: Redefining name 'school_name' from outer scope (line 68) (redefined-outer-name)
gkcx.py:32:0: C0111: Missing function docstring (missing-docstring)
gkcx.py:33:4: W0622: Redefining built-in 'id' (redefined-builtin)
gkcx.py:32:12: W0621: Redefining name 'school' from outer scope (line 72) (redefined-outer-name)

------------------------------------------------------------------
Your code has been rated at 3.33/10 (previous run: 3.33/10, +0.00)

发现 Pylint 还会给代码总体打一个分数,咱们就能够根据提示一步步调优,提升分数!10 分满分。

若是运行两次 Pylint,它会同时显示出当前和上次的运行结果,从而能够看出代码质量是否获得了改进。

 

错误代码含义

  • C:惯例,违反了编码风格标准
  • R:重构,代码很是糟糕
  • W:警告,某些 Python 特定的问题
  • E:错误,极可能是代码中的错误
  • F:致命错误,阻止 Pylint 进一步运行的错误

 

flake8

Flake8 是由 Python 官方发布的一款辅助检测 Python 代码是否规范的工具,相对于目前热度比较高的 Pylint 来讲,Flake8 检查规则灵活,支持集成额外插件,扩展性强。Flake8 是对下面三个工具的封装:

  1. PyFlakes:静态检查 Python 代码逻辑错误的工具。
  2. Pep8: 静态检查 PEP8 编码风格的工具。
  3. NedBatchelder’s McCabe :静态分析 Python 代码复杂度的工具。

不光对以上三个工具的封装,Flake8还提供了扩展的开发接口。

官方文档:https://pypi.python.org/pypi/flake8/

 

安装

$ pip install flake8       

$ flake8 --version
3.5.0 (mccabe: 0.6.1, pycodestyle: 2.3.1, pyflakes: 1.6.0) CPython 3.5.2 on Linux

 

使用

基本使用方法:flake8 [file name or directory name]

$ flake8 gkcx.py
gkcx.py:3:1: F401 'bs4.BeautifulSoup' imported but unused
gkcx.py:6:1: F401 're' imported but unused
gkcx.py:11:80: E501 line too long (135 > 79 characters)
gkcx.py:14:1: E302 expected 2 blank lines, found 1
...省略部分
gkcx.py:71:80: E501 line too long (100 > 79 characters)
gkcx.py:82:25: E231 missing whitespace after ','
gkcx.py:82:29: E231 missing whitespace after ','
gkcx.py:84:1: W293 blank line contains whitespace
gkcx.py:84:1: W391 blank line at end of file

PyFlakes 和 Pep8 的输出将合并起来一块儿返回。能够看出 flake8 不止检查代码错误,还会对代码规范不对的地方进行检查,好比:一行代码过长。

Flake8 提供一个扩展选项:--max-complexity,若是函数的 McCabe 复杂度比给定的值更高将发出一个告警。该功能对于发现代码过分复杂很是有用,根据 Thomas J. McCabe, Sr 研究,代码复杂度不宜超过 10,而 Flake8 官网建议值为 12。

  • McCabe 复杂度默认状况下是不会输出的,须要经过 --max-complexity 指定:
$ flake8 gkcx.py --max-complexity=5
gkcx.py:3:1: F401 'bs4.BeautifulSoup' imported but unused
gkcx.py:6:1: F401 're' imported but unused
...省略部分
gkcx.py:67:1: E305 expected 2 blank lines after class or function definition, found 1
gkcx.py:67:1: C901 'If 67' is too complex (6)
gkcx.py:71:80: E501 line too long (100 > 79 characters)
gkcx.py:82:25: E231 missing whitespace after ','
gkcx.py:82:29: E231 missing whitespace after ','
gkcx.py:84:1: W391 blank line at end of file
gkcx.py:84:1: W293 blank line contains whitespace
  • 能够经过 --ignore 忽略指定输出:
$ flake8 gkcx.py --ignore E501,E231,E203
gkcx.py:3:1: F401 'bs4.BeautifulSoup' imported but unused
gkcx.py:6:1: F401 're' imported but unused
gkcx.py:14:1: E302 expected 2 blank lines, found 1
gkcx.py:32:1: E302 expected 2 blank lines, found 1
gkcx.py:38:22: E225 missing whitespace around operator
gkcx.py:41:1: E302 expected 2 blank lines, found 1
gkcx.py:55:15: W291 trailing whitespace
gkcx.py:67:1: E305 expected 2 blank lines after class or function definition, found 1
gkcx.py:84:1: W391 blank line at end of file
gkcx.py:84:1: W293 blank line contains whitespace
  • 经过 --select 参数设置只展现指定输出:
$ flake8 gkcx.py --select F401
gkcx.py:3:1: F401 'bs4.BeautifulSoup' imported but unused
gkcx.py:6:1: F401 're' imported but unused

 

错误码含义

Flake8 基础错误返回码一共有三类:

  • E***/W***:PEP8 中的 error 和 warning。
  • F***:经过 PyFlakes 检测出的 error,其实 PyFlakes 自己是不提供错误返回码的,flake8 对 pyflakes 返回的错误消息进行了分类。
  • C9**:经过 McCabe 检测出的代码复杂度。

 

总结

Python 静态代码检查工具不止这几个,你们能够挑选合适的进行使用。本人也没有进行深刻地探究,有兴趣进行高阶使用的能够参照官网。

另外,各个工具应该都有对应的插件,好比 vim、vscode、eclipse、pycharm 上应该都能集成上述工具,你们能够网上找下适合本身 ide 的插件进行安装。

有些人估计看到那么多异常也会烦,可是毕竟是能够培养你们代码规范的,后续工做后,确定也有相应的代码规范,建议你们养成习惯。

本文原创发布于慕课网 ,转载请注明出处,谢谢合做

做者:不正经程序员 连接:https://www.imooc.com/article/46328 来源:慕课网 本文原创发布于慕课网 ,转载请注明出处,谢谢合做

相关文章
相关标签/搜索