歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 使用Pylint規范你的Python代碼

使用Pylint規范你的Python代碼

日期:2017/3/1 9:26:23   编辑:Linux編程

Pylint是一個Python代碼風格的檢查工具,功能上類似於pychecker,默認用PEP8作為代碼風格標准,它所提供的功能包括:檢查代碼行的長度,檢查變量命名是否符合規范,檢查聲明的接口是否被真正的實現等等,詳細信息參考:http://docs.pylint.org/。

安裝:

sudo apt-get install pylint ? #Ubuntu
pip install pyint ? #windows

更多安裝方式可以查看:http://www.pylint.org/#install

使用:

pylint [options] module_or_package

比如運行:pylint setup.py ?,輸出:

No config file found, using default configuration
************* Module setup
C: 1,0: Missing docstring
C: 6,0: Invalid name "here" for type constant (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$)
C: 10,0: Invalid name "requires" for type constant (should match (([A-Z_][A-Z0-9_]*)|(__.*__))$)
C: 31,0: Comma not followed by a space
if sys.version_info[:3] < (2,5,0):

這部分是源代碼的分析結果,其格式是: 第一行都是這樣子: ****************** Module 模塊的名稱 接下來的幾行就是message,其格式為:

MESSAGETYPE: LINENUM:[OBJECT:] MESSAGE MESSAGE有如下幾種類型:

  • (C)Convertion,指違反了編碼風格標准
  • (R) Refactor,指寫得非常糟糕的代碼。
  • (W) Warn,某些 Python 特定的問題。
  • (E) Error,很可能是代碼中的錯誤。
  • (F) Fatal,阻止 Pylint 進一步運行的錯誤。
  • 如果只看消息而不想看後面很長一段報告的話,使用使用參數:

pylint -r n 第二部分就是一些統計報告,這部分可以不關注。

下面關於Python的文章您也可能喜歡,不妨看看:

Python:在指定目錄下查找滿足條件的文件 http://www.linuxidc.com/Linux/2015-08/121283.htm

Python2.7.7源碼分析 http://www.linuxidc.com/Linux/2015-08/121168.htm

無需操作系統直接運行 Python 代碼 http://www.linuxidc.com/Linux/2015-05/117357.htm

CentOS上源碼安裝Python3.4 http://www.linuxidc.com/Linux/2015-01/111870.htm

《Python核心編程 第二版》.(Wesley J. Chun ).[高清PDF中文版] http://www.linuxidc.com/Linux/2013-06/85425.htm

《Python開發技術詳解》.( 周偉,宗傑).[高清PDF掃描版+隨書視頻+代碼] http://www.linuxidc.com/Linux/2013-11/92693.htm

Python腳本獲取Linux系統信息 http://www.linuxidc.com/Linux/2013-08/88531.htm

在Ubuntu下用Python搭建桌面算法交易研究環境 http://www.linuxidc.com/Linux/2013-11/92534.htm

Python 語言的發展簡史 http://www.linuxidc.com/Linux/2014-09/107206.htm

Python 的詳細介紹:請點這裡
Python 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved