手把手教你使用pylint

安裝pylint

在terminal里面運行如下命令

reno@PS20190428UMVP:~$ pip install pylint

讓我們來看看pylint

直接運行一下命令

reno@PS20190428UMVP:~$ pylint

pylint會輸出一系列的help文檔,然后其中重要的參數(shù)有

Master:
  --generate-rcfile=<file>
Commands:
  --help-msg=<msg-id>
Commands:
  --help-msg=<msg-id>
Message control:
  --disable=<msg-ids>
Reports:
  --files-output=<y_or_n>
  --reports=<y_or_n>
  --output-format=<format>

第一個pylint運行

待檢查代碼:

from datetime import datetime

def see_current_time():
    print(datetime.now())

def SSEEEWAIREDFUNC():
    SSSSSSS=0
    return SSSSSSS


if __name__ == '__main__':
    LLLLLLLong_variable = ''
    see_current_time()

講上述代碼復(fù)制到

~/pylint_example.py文件里面

運行檢查

reno@PS20190428UMVP:~$ pylint pylint_example.py

得到如下結(jié)果

reno@PS20190428UMVP:/mnt/d/work_env/test_pylint$ pylint app.py
No config file found, using default configuration
************* Module app
C:  4, 0: Unnecessary parens after 'print' keyword (superfluous-parens)
C:  7, 0: Exactly one space required around assignment
    SSSSSSS=0
           ^ (bad-whitespace)
C:  1, 0: Missing module docstring (missing-docstring)
C:  3, 0: Missing function docstring (missing-docstring)
C:  6, 0: Function name "SSEEEWAIREDFUNC" doesn't conform to snake_case naming style (invalid-name)
C:  6, 0: Missing function docstring (missing-docstring)
C:  7, 4: Variable name "SSSSSSS" doesn't conform to snake_case naming style (invalid-name)
C: 12, 4: Constant name "LLLLLLLong_variable" doesn't conform to UPPER_CASE naming style (invalid-name)

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

加上報告參數(shù)

reno@PS20190428UMVP:~$ pylint -r y pylint_example.py

這次的輸出結(jié)果

************* Module pylint_example
C:  4, 0: Unnecessary parens after 'print' keyword (superfluous-parens)
C:  7, 0: Exactly one space required around assignment
    SSSSSSS=0
           ^ (bad-whitespace)
C:  1, 0: Missing module docstring (missing-docstring)
C:  3, 0: Missing function docstring (missing-docstring)
C:  6, 0: Function name "SSEEEWAIREDFUNC" doesn't conform to snake_case naming style (invalid-name)
C:  6, 0: Missing function docstring (missing-docstring)
C:  7, 4: Variable name "SSSSSSS" doesn't conform to snake_case naming style (invalid-name)
C: 12, 4: Constant name "LLLLLLLong_variable" doesn't conform to UPPER_CASE naming style (invalid-name)


Report
======
9 statements analysed.

Statistics by type
------------------

+---------+-------+-----------+-----------+------------+---------+
|type     |number |old number |difference |%documented |%badname |
+=========+=======+===========+===========+============+=========+
|module   |1      |1          |=          |0.00        |0.00     |
+---------+-------+-----------+-----------+------------+---------+
|class    |0      |0          |=          |0           |0        |
+---------+-------+-----------+-----------+------------+---------+
|method   |0      |0          |=          |0           |0        |
+---------+-------+-----------+-----------+------------+---------+
|function |2      |2          |=          |0.00        |50.00    |
+---------+-------+-----------+-----------+------------+---------+



Raw metrics
-----------

+----------+-------+------+---------+-----------+
|type      |number |%     |previous |difference |
+==========+=======+======+=========+===========+
|code      |10     |71.43 |10       |=          |
+----------+-------+------+---------+-----------+
|docstring |0      |0.00  |0        |=          |
+----------+-------+------+---------+-----------+
|comment   |0      |0.00  |0        |=          |
+----------+-------+------+---------+-----------+
|empty     |4      |28.57 |4        |=          |
+----------+-------+------+---------+-----------+



Duplication
-----------

+-------------------------+------+---------+-----------+
|                         |now   |previous |difference |
+=========================+======+=========+===========+
|nb duplicated lines      |0     |0        |=          |
+-------------------------+------+---------+-----------+
|percent duplicated lines |0.000 |0.000    |=          |
+-------------------------+------+---------+-----------+



Messages by category
--------------------

+-----------+-------+---------+-----------+
|type       |number |previous |difference |
+===========+=======+=========+===========+
|convention |8      |8        |=          |
+-----------+-------+---------+-----------+
|refactor   |0      |0        |=          |
+-----------+-------+---------+-----------+
|warning    |0      |0        |=          |
+-----------+-------+---------+-----------+
|error      |0      |0        |=          |
+-----------+-------+---------+-----------+



Messages
--------

+-------------------+------------+
|message id         |occurrences |
+===================+============+
|missing-docstring  |3           |
+-------------------+------------+
|invalid-name       |3           |
+-------------------+------------+
|superfluous-parens |1           |
+-------------------+------------+
|bad-whitespace     |1           |
+-------------------+------------+




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


?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容