python代码质量检查工具_python代码检查工具pylint 让你的python更规范
復制代碼 代碼如下:
#coding:utf-8
'''
a test function module
'''
import urllib
import time
def fetch(url):
'''
fetch url
'''
content = urllib.urlopen(url).read()
f_html = open('tmp%s.html' % str(time.time()), 'w')
f_html.write(content)
f_html.close()
def main(urls):
'''
main func to be called
'''
for url in urls:
fetch(url)
if __name__ == '__main__':
FROM_URLS = ['http://www.baidu.com','http://www.sohu.com']
main(FROM_URLS)
基本上有以下幾種判斷標準:
1、命名方式
2、docstring
當然直接用pylint進行包檢測也是可以的:pylint package
參看下面了解更多的使用方法,一定要動手練習才行:
參看內容:
Pylint 的調用
清單 1. Pylint 的調用命令
pylint [options] module_or_package
使用 Pylint 對一個模塊 module.py 進行代碼檢查:
1. 進入這個模塊所在的文件夾,運行 pylint [options] module.py
這種調用方式是一直可以工作的,因為當前的工作目錄會被自動加入 Python 的路徑中。
2. 不進入模塊所在的文件夾,運行 pylint [options] directory/module.py
這種調用方式當如下條件滿足的時候是可以工作的:directory 是個 Python 包 ( 比如包含一個 __init__.py 文件 ),或者 directory 被加入了 Python 的路徑中。
使用 Pylint 對一個包 pakage 進行代碼檢查:
1. 進入這個包所在文件夾,運行 pylint [options] pakage。
這種調用方式是一直可以工作的,因為當前的工作目錄會被自動加入 Python 的路徑中。
2. 不進入包所在的文件夾,運行 pylint [options] directory/ pakage。
這種情況下當如下條件滿足的時候是可以工作的:directory 被加入了 Python 的路徑中。比如在 Linux 上,export PYTHONPATH=$PYTHONPATH: directory。
此外,對于安裝了 tkinter 包的機器,可以使用命令 pylint-gui打開一個簡單的 GUI 界面,在這里輸入模塊或者包的名字 ( 規則同命令行 ), 點擊 Run,Pylint 的輸出會在 GUI 中顯示。
Pylint 的常用命令行參數
-h,–help
顯示所有幫助信息。
–generate-rcfile
可以使用 pylint –generate-rcfile 來生成一個配置文件示例。可以使用重定向把這個配置文件保存下來用做以后使用。也可以在前面加上其它選項,使這些選項的值被包含在這個產生的配置文件里。如:pylint –persistent=n –generate-rcfile > pylint.conf,查看 pylint.conf,可以看到 persistent=no,而不再是其默認值 yes。
–rcfile=
指定一個配置文件。把使用的配置放在配置文件中,這樣不僅規范了自己代碼,也可以方便地和別人共享這些規范。
-i , –include-ids=
在輸出中包含 message 的 id, 然后通過 pylint –help-msg=來查看這個錯誤的詳細信息,這樣可以具體地定位錯誤。
-r , –reports=
默認是 y, 表示 Pylint 的輸出中除了包含源代碼分析部分,也包含報告部分。
–files-output=
將每個 module /package 的 message 輸出到一個以 pylint_module/package. [txt|html] 命名的文件中,如果有 report 的話,輸出到名為 pylint_global.[txt|html] 的文件中。默認是輸出到屏幕上不輸出到文件里。
-f , –output-format=
設置輸出格式。可以選擇的格式有 text, parseable, colorized, msvs (visual studio) 和 html, 默認的輸出格式是 text。
–disable-msg=
禁止指定 id 的 message. 比如說輸出中包含了 W0402 這個 warning 的 message, 如果不希望它在輸出中出現,可以使用 –disable-msg= W0402
Pylint 的輸出
Pylint的默認輸出格式是原始文本(raw text)格式 ,可以通過 -f ,–output-format= 來指定別的輸出格式如html等等。在Pylint的輸出中有如下兩個部分:源代碼分析部分和報告部分。
源代碼分析部分:
對于每一個 Python 模塊,Pylint 的結果中首先顯示一些”*”字符 , 后面緊跟模塊的名字,然后是一系列的 message, message 的格式如下:
MESSAGE_TYPE: LINE_NUM:[OBJECT:] MESSAGE
MESSAGE_TYPE 有如下幾種:
(C) 慣例。違反了編碼風格標準
(R) 重構。寫得非常糟糕的代碼。
(W) 警告。某些 Python 特定的問題。
(E) 錯誤。很可能是代碼中的錯誤。
(F) 致命錯誤。阻止 Pylint 進一步運行的錯誤。
清單 2. Pylint 中的 utils 模塊的輸出結果
************* Module utils
C: 88:Message: Missing docstring
R: 88:Message: Too few public methods (0/2)
C:183:MessagesHandlerMixIn._cat_ids: Missing docstring
R:183:MessagesHandlerMixIn._cat_ids: Method could be a function
R:282:MessagesHandlerMixIn.list_messages: Too many branches (14/12)
報告部分:
在源代碼分析結束后面,會有一系列的報告,每個報告關注于項目的某些方面,如每種類別的 message 的數目,模塊的依賴關系等等。具體來說,報告中會包含如下的方面:
檢查的 module 的個數。
對于每個 module, 錯誤和警告在其中所占的百分比。比如有兩個 module A 和 B, 如果一共檢查出來 4 個錯誤,1 個錯誤是在 A 中,3 個錯誤是在 B 中,那么 A 的錯誤的百分比是 25%, B 的錯誤的百分比是 75%。
錯誤,警告的總數量。
回頁首
使用 Pylint 分析 Python 代碼的具體示例
下面是一個從 xml 文件中讀取一些值并顯示出來的一段 Python 代碼 dw.py,代碼如下:
清單 3. 源碼
import string
#!/usr/bin/env python
import xml.dom.minidom
xmlDom=xml.dom.minidom.parse(“identity.xml”)
organizations = xmlDom.getElementsByTagName(‘DW')
for org in organizations:
products = org.getElementsByTagName(‘linux')
for product in products:
print ‘ID: ‘ + product.getAttribute(‘id')
print ‘Name: ‘ + product.getAttribute(‘name')
print ‘Word Count: ‘ + product.getAttribute(‘count')
清單 4. identity.xml 的內容
這時候使用 Pylint 的結果(這是從 html 格式的輸出中拷貝的)為:
清單 5. Pylint 的分析結果
************* Module dw
C:1:Missing docstring
C:5:Operator not preceded by a space xmlDom=xml.dom.minidom.parse(“identity.xml”) ^
C:5:Invalid name “xmlDom” (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$)
C:6:Invalid name “organizations” (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$)
Report 部分省略
輸出中第一部分是源代碼分析,第二部分是報告。輸出結果中有這么多信息,從哪里開始分析呢?首先使用如下的步驟來分析代碼:
1. 因為輸出結果太長,所以可以先不讓它輸出報告部分,先根據源代碼分析部分來找出代碼中的問題。使用選項 “–reports=n”。
2. 使用選項 “–include-ids=y”。可以獲取到源代碼分析部分每條信息的 ID。
清單 6. 使用 pylint –reports=n –include-ids=y dw.py 的結果
************* Module dw
C0111: 1: Missing docstring
C0322: 5: Operator not preceded by a space xmlDom=xml.dom.minidom.parse(“identity.xml”) ^
C0103: 5: Invalid name “xmlDom” (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$)
C0103: 6: Invalid name “organizations” (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$)
每個信息前面都會加上一個 id, 如果不理解這個信息的意思,可以通過 pylint –help-msg=id來查看。
清單 7. 使用 pylint –help-msg= C0111 的結果
C0111: *Missing docstring*
Used when a module, function, class or method has no docstring. Some special
methods like __init__ doesn't necessary require a docstring.
This message belongs to the basic checker.
3. 開始分析每個源代碼中的問題。從上面知道,第一個問題的原因是缺少 docstring,在代碼中增加 docstring, 修改后的代碼如下:
清單 8. 增加 docstring 修改后的源碼
#!/usr/bin/env python
“”"This script parse the content of a xml file”"”
import xml.dom.minidom
xmlDom=xml.dom.minidom.parse(“identity.xml”)
organizations = xmlDom.getElementsByTagName(‘DW')
for org in organizations:
products = org.getElementsByTagName(‘linux')
for product in products:
print ‘ID: ‘ + product.getAttribute(‘id')
print ‘Name: ‘ + product.getAttribute(‘name')
print ‘Word Count: ‘ + product.getAttribute(‘count')
重新運行 pylint –reports=n –include-ids=y dw.py,結果為:
清單 9. 運行結果
************* Module dw
C0322: 7: Operator not preceded by a space
xmlDom=xml.dom.minidom.parse(“identity.xml”)
^
C0103: 7: Invalid name “xmlDom” (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$)
C0103: 8: Invalid name “organizations” (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$)
可以看到源代碼中的第一個問題已被解決。
4. 關于第二個 C0322 的問題,這里的分析結果說明得比較清楚,是代碼第七行中的等號運算符兩邊沒有空格。我們在這里加上空格,重新運行 pylint –reports=n –include-ids=y dw.py,結果為:
清單 10. 運行結果
************* Module dw
C0103: 7: Invalid name “xmlDom” (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$)
C0103: 8: Invalid name “organizations” (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$)
5. 可以看到現在問題只剩下 C0103 了。這里的意思是變量命名規則應該符合后面正則表達式的規定。Pylint 定義了一系列針對變量,函數,類等的名字的命名規則。實際中我們不一定要使用這樣的命名規則,我們可以定義使用正則表達式定義自己的命名規則,比如使用選項 –const-rgx='[a-z_][a-z0-9_]{2,30}$',我們將變量 xmlDom改為 xmldom, 代碼如下:
清單 11. 將變量 xmlDom 改為 xmldom 后的源碼
#!/usr/bin/env python
“”"This script parse the content of a xml file”"”
import xml.dom.minidom
xmldom = xml.dom.minidom.parse(“identity.xml”)
organizations = xmldom.getElementsByTagName(‘DW')
for org in organizations:
products = org.getElementsByTagName(‘linux')
for product in products:
print ‘ID: ‘ + product.getAttribute(‘id')
print ‘Name: ‘ + product.getAttribute(‘name')
print ‘Word Count: ‘ + product.getAttribute(‘count')
運行 pylint –reports=n –include-ids=y –const-rgx='[a-z_][a-z0-9_]{2,30}$' dw.py,結果中就沒有任何問題了。
6. 如果希望一個組里的人都使用這些統一的規則,來規范一個部門的代碼風格。比如說大家都使用 –const-rgx='[a-z_][a-z0-9_]{2,30}$'作為命名規則,那么一個比較便捷的方法是使用配置文件。
使用 pylint –generate-rcfile > pylint.conf來生成一個示例配置文件,然后編輯其中的 –const-rgx選項。或者也可以直接 pylint –const-rgx='[a-z_][a-z0-9_]{2,30}$' –generate-rcfile > pylint.conf,這樣生成的配置文件中 –const-rgx選項直接就是 ‘[a-z_][a-z0-9_]{2,30}$'了。
以后運行 Pylint 的時候指定配置文件:pylint –rcfile=pylint.conf dw.py
這樣 Pylint 就會按照配置文件 pylint.conf中的選項來指定參數。在一個部門中,大家可以共同使用同一個配置文件,這樣就可以保持一致的代碼風格。
7. 如果把 report 部分加上,即不使用 –reports=n,可以看到報告部分的內容。
與此相關:
代碼審察工具:
http://www.cnblogs.com/LiGleam/archive/2012/02/19/2358549.html
http://www.ibm.com/developerworks/cn/linux/l-cn-pylint/index.html?ca=drs-cn-1217
本文原創發布php中文網,轉載請注明出處,感謝您的尊重!
相關文章
相關視頻
總結
以上是生活随笔為你收集整理的python代码质量检查工具_python代码检查工具pylint 让你的python更规范的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 娇子香烟是杀精的吗??
- 下一篇: 神经性脚臭在穿上鞋后闻到带着鞋材料的异味