python代码质量检查工具_python代码检查工具pylint 让你的python更规范
復(fù)制代碼 代碼如下:
#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)
基本上有以下幾種判斷標(biāo)準(zhǔn):
1、命名方式
2、docstring
當(dāng)然直接用pylint進(jìn)行包檢測(cè)也是可以的:pylint package
參看下面了解更多的使用方法,一定要?jiǎng)邮志毩?xí)才行:
參看內(nèi)容:
Pylint 的調(diào)用
清單 1. Pylint 的調(diào)用命令
pylint [options] module_or_package
使用 Pylint 對(duì)一個(gè)模塊 module.py 進(jìn)行代碼檢查:
1. 進(jìn)入這個(gè)模塊所在的文件夾,運(yùn)行 pylint [options] module.py
這種調(diào)用方式是一直可以工作的,因?yàn)楫?dāng)前的工作目錄會(huì)被自動(dòng)加入 Python 的路徑中。
2. 不進(jìn)入模塊所在的文件夾,運(yùn)行 pylint [options] directory/module.py
這種調(diào)用方式當(dāng)如下條件滿足的時(shí)候是可以工作的:directory 是個(gè) Python 包 ( 比如包含一個(gè) __init__.py 文件 ),或者 directory 被加入了 Python 的路徑中。
使用 Pylint 對(duì)一個(gè)包 pakage 進(jìn)行代碼檢查:
1. 進(jìn)入這個(gè)包所在文件夾,運(yùn)行 pylint [options] pakage。
這種調(diào)用方式是一直可以工作的,因?yàn)楫?dāng)前的工作目錄會(huì)被自動(dòng)加入 Python 的路徑中。
2. 不進(jìn)入包所在的文件夾,運(yùn)行 pylint [options] directory/ pakage。
這種情況下當(dāng)如下條件滿足的時(shí)候是可以工作的:directory 被加入了 Python 的路徑中。比如在 Linux 上,export PYTHONPATH=$PYTHONPATH: directory。
此外,對(duì)于安裝了 tkinter 包的機(jī)器,可以使用命令 pylint-gui打開一個(gè)簡(jiǎn)單的 GUI 界面,在這里輸入模塊或者包的名字 ( 規(guī)則同命令行 ), 點(diǎn)擊 Run,Pylint 的輸出會(huì)在 GUI 中顯示。
Pylint 的常用命令行參數(shù)
-h,–help
顯示所有幫助信息。
–generate-rcfile
可以使用 pylint –generate-rcfile 來生成一個(gè)配置文件示例。可以使用重定向把這個(gè)配置文件保存下來用做以后使用。也可以在前面加上其它選項(xiàng),使這些選項(xiàng)的值被包含在這個(gè)產(chǎn)生的配置文件里。如:pylint –persistent=n –generate-rcfile > pylint.conf,查看 pylint.conf,可以看到 persistent=no,而不再是其默認(rèn)值 yes。
–rcfile=
指定一個(gè)配置文件。把使用的配置放在配置文件中,這樣不僅規(guī)范了自己代碼,也可以方便地和別人共享這些規(guī)范。
-i , –include-ids=
在輸出中包含 message 的 id, 然后通過 pylint –help-msg=來查看這個(gè)錯(cuò)誤的詳細(xì)信息,這樣可以具體地定位錯(cuò)誤。
-r , –reports=
默認(rèn)是 y, 表示 Pylint 的輸出中除了包含源代碼分析部分,也包含報(bào)告部分。
–files-output=
將每個(gè) module /package 的 message 輸出到一個(gè)以 pylint_module/package. [txt|html] 命名的文件中,如果有 report 的話,輸出到名為 pylint_global.[txt|html] 的文件中。默認(rèn)是輸出到屏幕上不輸出到文件里。
-f , –output-format=
設(shè)置輸出格式。可以選擇的格式有 text, parseable, colorized, msvs (visual studio) 和 html, 默認(rèn)的輸出格式是 text。
–disable-msg=
禁止指定 id 的 message. 比如說輸出中包含了 W0402 這個(gè) warning 的 message, 如果不希望它在輸出中出現(xiàn),可以使用 –disable-msg= W0402
Pylint 的輸出
Pylint的默認(rèn)輸出格式是原始文本(raw text)格式 ,可以通過 -f ,–output-format= 來指定別的輸出格式如html等等。在Pylint的輸出中有如下兩個(gè)部分:源代碼分析部分和報(bào)告部分。
源代碼分析部分:
對(duì)于每一個(gè) Python 模塊,Pylint 的結(jié)果中首先顯示一些”*”字符 , 后面緊跟模塊的名字,然后是一系列的 message, message 的格式如下:
MESSAGE_TYPE: LINE_NUM:[OBJECT:] MESSAGE
MESSAGE_TYPE 有如下幾種:
(C) 慣例。違反了編碼風(fēng)格標(biāo)準(zhǔn)
(R) 重構(gòu)。寫得非常糟糕的代碼。
(W) 警告。某些 Python 特定的問題。
(E) 錯(cuò)誤。很可能是代碼中的錯(cuò)誤。
(F) 致命錯(cuò)誤。阻止 Pylint 進(jìn)一步運(yùn)行的錯(cuò)誤。
清單 2. Pylint 中的 utils 模塊的輸出結(jié)果
************* 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)
報(bào)告部分:
在源代碼分析結(jié)束后面,會(huì)有一系列的報(bào)告,每個(gè)報(bào)告關(guān)注于項(xiàng)目的某些方面,如每種類別的 message 的數(shù)目,模塊的依賴關(guān)系等等。具體來說,報(bào)告中會(huì)包含如下的方面:
檢查的 module 的個(gè)數(shù)。
對(duì)于每個(gè) module, 錯(cuò)誤和警告在其中所占的百分比。比如有兩個(gè) module A 和 B, 如果一共檢查出來 4 個(gè)錯(cuò)誤,1 個(gè)錯(cuò)誤是在 A 中,3 個(gè)錯(cuò)誤是在 B 中,那么 A 的錯(cuò)誤的百分比是 25%, B 的錯(cuò)誤的百分比是 75%。
錯(cuò)誤,警告的總數(shù)量。
回頁首
使用 Pylint 分析 Python 代碼的具體示例
下面是一個(gè)從 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 的內(nèi)容
這時(shí)候使用 Pylint 的結(jié)果(這是從 html 格式的輸出中拷貝的)為:
清單 5. Pylint 的分析結(jié)果
************* 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 部分省略
輸出中第一部分是源代碼分析,第二部分是報(bào)告。輸出結(jié)果中有這么多信息,從哪里開始分析呢?首先使用如下的步驟來分析代碼:
1. 因?yàn)檩敵鼋Y(jié)果太長(zhǎng),所以可以先不讓它輸出報(bào)告部分,先根據(jù)源代碼分析部分來找出代碼中的問題。使用選項(xiàng) “–reports=n”。
2. 使用選項(xiàng) “–include-ids=y”。可以獲取到源代碼分析部分每條信息的 ID。
清單 6. 使用 pylint –reports=n –include-ids=y dw.py 的結(jié)果
************* 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_]*)|(__.*__))$)
每個(gè)信息前面都會(huì)加上一個(gè) id, 如果不理解這個(gè)信息的意思,可以通過 pylint –help-msg=id來查看。
清單 7. 使用 pylint –help-msg= C0111 的結(jié)果
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. 開始分析每個(gè)源代碼中的問題。從上面知道,第一個(gè)問題的原因是缺少 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')
重新運(yùn)行 pylint –reports=n –include-ids=y dw.py,結(jié)果為:
清單 9. 運(yùn)行結(jié)果
************* 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_]*)|(__.*__))$)
可以看到源代碼中的第一個(gè)問題已被解決。
4. 關(guān)于第二個(gè) C0322 的問題,這里的分析結(jié)果說明得比較清楚,是代碼第七行中的等號(hào)運(yùn)算符兩邊沒有空格。我們?cè)谶@里加上空格,重新運(yùn)行 pylint –reports=n –include-ids=y dw.py,結(jié)果為:
清單 10. 運(yùn)行結(jié)果
************* 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. 可以看到現(xiàn)在問題只剩下 C0103 了。這里的意思是變量命名規(guī)則應(yīng)該符合后面正則表達(dá)式的規(guī)定。Pylint 定義了一系列針對(duì)變量,函數(shù),類等的名字的命名規(guī)則。實(shí)際中我們不一定要使用這樣的命名規(guī)則,我們可以定義使用正則表達(dá)式定義自己的命名規(guī)則,比如使用選項(xiàng) –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')
運(yùn)行 pylint –reports=n –include-ids=y –const-rgx='[a-z_][a-z0-9_]{2,30}$' dw.py,結(jié)果中就沒有任何問題了。
6. 如果希望一個(gè)組里的人都使用這些統(tǒng)一的規(guī)則,來規(guī)范一個(gè)部門的代碼風(fēng)格。比如說大家都使用 –const-rgx='[a-z_][a-z0-9_]{2,30}$'作為命名規(guī)則,那么一個(gè)比較便捷的方法是使用配置文件。
使用 pylint –generate-rcfile > pylint.conf來生成一個(gè)示例配置文件,然后編輯其中的 –const-rgx選項(xiàng)。或者也可以直接 pylint –const-rgx='[a-z_][a-z0-9_]{2,30}$' –generate-rcfile > pylint.conf,這樣生成的配置文件中 –const-rgx選項(xiàng)直接就是 ‘[a-z_][a-z0-9_]{2,30}$'了。
以后運(yùn)行 Pylint 的時(shí)候指定配置文件:pylint –rcfile=pylint.conf dw.py
這樣 Pylint 就會(huì)按照配置文件 pylint.conf中的選項(xiàng)來指定參數(shù)。在一個(gè)部門中,大家可以共同使用同一個(gè)配置文件,這樣就可以保持一致的代碼風(fēng)格。
7. 如果把 report 部分加上,即不使用 –reports=n,可以看到報(bào)告部分的內(nèi)容。
與此相關(guā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
本文原創(chuàng)發(fā)布php中文網(wǎng),轉(zhuǎn)載請(qǐng)注明出處,感謝您的尊重!
相關(guān)文章
相關(guān)視頻
總結(jié)
以上是生活随笔為你收集整理的python代码质量检查工具_python代码检查工具pylint 让你的python更规范的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 娇子香烟是杀精的吗??
- 下一篇: 神经性脚臭在穿上鞋后闻到带着鞋材料的异味