python 代碼檢測工具

對於我這種習慣了 Java 這種編譯型語言,在使用 Python 這種動態語言的時候,發現錯誤常常只能在執行的時候發現,總感受有點不放心。html

並且有一些錯誤因爲隱藏的比較深,只有特定邏輯纔會觸發,每每致使須要花不少時間才能將語法錯誤慢慢排查出來。其實有一些錯誤是很明顯的,假如能在寫程序的時候發現這些錯誤,就能提升工做效率。python

這時候 Python 靜態語法檢查工具就出現了。shell

pep8/pycodestyle   PEP(Python Enhancement Proposal)的縮寫,翻譯過來就是 Python加強建議書

相信你們多多少少都見過 PEP 8,那 PEP 8 究竟是個啥?json

其實 PEP 8 是一種 Python 代碼規範指南,能夠參閱官網:https://www.python.org/dev/peps/pep-0008/,其目的是爲了保持代碼的一致性、可讀性。vim

檢查本身代碼是否符合 PEP 8 規範,一個簡單的工具就是:pep8。安全

安裝

$ pip install pep8
  • 1

在使用時發現 pep8 給出了一個警告:bash

$ 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'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

意思是 pep8 已被 pycodestyle 替代!微信

使用

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

$ 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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 參數 --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 源文件錯誤的簡單程序。ide

Pyflakes 分析程序而且檢查各類錯誤。它經過解析源文件實現,無需導入它,所以在模塊中使用是安全的,沒有任何的反作用。

  • 不會檢查代碼風格
  • 因爲它是單獨檢查各個文件,所以它也至關的快,固然檢測範圍也有必定的侷限

安裝

pip install pyflakes
  • 1

使用

$ 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]
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

使用

基本使用: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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

發現 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
  • 1
  • 2
  • 3
  • 4

使用

基本使用方法: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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 能夠經過 --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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 經過 --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
  • 1
  • 2
  • 3

錯誤碼含義

Flake8 基礎錯誤返回碼一共有三類:

  • E***/W***:PEP8 中的 error 和 warning。
  • F***:經過 PyFlakes 檢測出的 error,其實 PyFlakes 自己是不提供錯誤返回碼的,flake8 對 pyflakes 返回的錯誤消息進行了分類。
  • C9**:經過 McCabe 檢測出的代碼複雜度。

總結

Python 靜態代碼檢查工具不止這幾個,你們能夠挑選合適的進行使用。本人也沒有進行深刻地探究,有興趣進行高階使用的能夠參照官網。

另外,各個工具應該都有對應的插件,好比 vim、vscode、eclipse、pycharm 上應該都能集成上述工具,你們能夠網上找下適合本身 ide 的插件進行安裝。

有些人估計看到那麼多異常也會煩,可是畢竟是能夠培養你們代碼規範的,後續工做後,確定也有相應的代碼規範,建議你們養成習慣。


若是以爲有用,歡迎關注個人微信,一塊兒學習,共同進步,不按期推出贈書活動~

你的關注是對我最大的鼓勵!

相關文章
相關標籤/搜索