簡體   English   中英

如何從 python 代碼執行 pylint 命令。 此外,pylint 中的哪些參數可以根據我的需要生成日志消息

[英]How can I execute pylint command from python code. Also what argument in pylint can make log message as per my need

當我想使用 Python 文件運行pylint命令時,我有一個場景。 使用我正在使用的命令提示符

python3 -m pylint test.py

除此之外,我想格式化我的消息,以便我以后可以使用 split 方法來分隔參數並提供像 excel 一樣的報告。 也用更有意義的名稱替換代碼,如 C0103。 我在命令提示符下嘗試了以下命令,但無法得到正確的答案

python3 -m pylint --msg-template="{module}{obj}{line}{column}{msg}" init.py

代碼

# all of the following are equivalent
my_string = 'Hello'
print(my_string)

my_string = "Hello"
print(my_string)

my_string = '''Hello'''
print(my_string)

# triple quotes string can extend multiple lines
my_string = """Hello, welcome to
           the world of Python"""
print(my_string)

輸出

python3 -m pylint init.py
************* Module init
init.py:14:0: C0304: Final newline missing (missing-final-newline)
init.py:1:0: C0114: Missing module docstring (missing-module-docstring)
init.py:2:0: C0103: Constant name "my_string" doesn't conform to UPPER_CASE naming style (invalid-name)
init.py:5:0: C0103: Constant name "my_string" doesn't conform to UPPER_CASE naming style (invalid-name)
init.py:8:0: C0103: Constant name "my_string" doesn't conform to UPPER_CASE naming style (invalid-name)
init.py:12:0: C0103: Constant name "my_string" doesn't conform to UPPER_CASE naming style (invalid-name)

-------------------------------------------------------------------
Your code has been rated at 2.50/10 (previous run: -1.39/10, +3.89)

除此之外,我想格式化我的消息,以便我以后可以使用 split 方法來分隔參數並提供像 excel 一樣的報告。

 python3 -m pylint --msg-template="{module}{obj}{line}{column}{msg}" init.py

您是否考慮過在消息模板中放置分隔符?

因為在這里您只是將所有項目混在一起,這似乎沒有什么幫助或用處,但也不難修復:

$ python3 -m pylint --msg-template="{module}{obj}{line}{column}{msg}" test.py
************* Module test
test10Missing module docstring
test20Constant name "my_string" doesn't conform to UPPER_CASE naming style

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

$ python3 -m pylint --msg-template="{module}|{obj}|{line}|{column}|{msg}" test.py
************* Module test
test||1|0|Missing module docstring
test||2|0|Constant name "my_string" doesn't conform to UPPER_CASE naming style

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

盡管如果您想以編程方式使用輸出, -f參數可能更有用。 pylint -f json將所有診斷信息轉儲為一個 json 對象數組,例如具有很好命名的屬性。

也用更有意義的名稱替換代碼,如 C0103。 我在命令提示符下嘗試了以下命令,但無法得到正確的答案

從文檔中,您想要的模板項是symbol ,即“消息的符號名稱”:

$ pylint test.py
************* Module test
test.py:1:0: C0114: Missing module docstring (missing-module-docstring)
test.py:2:0: C0103: Constant name "my_string" doesn't conform to UPPER_CASE naming style (invalid-name)

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

$ pylint --msg-template='{msg_id}' test.py
************* Module test
C0114
C0103

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

$ pylint --msg-template='{symbol}' test.py
************* Module test
missing-module-docstring
invalid-name

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

當我想使用 Python 文件運行 pylint 命令時,我有一個場景。

使用 subprocess 運行 pylint 可能是最簡單和最受支持的方法。 您可以手動配置和運行pylint.lint.PyLinter但據我所知它沒有記錄,不受支持,絕對令人痛苦,並且容易摔倒(因為 pylint 崩潰——這很常見—— - 將刪除整個腳本)。 我們曾經在 $dayjob 中這樣做,然后回到在子進程中運行 CLI,它更可靠。

我使用 pylint 的一種方法是,我擁有的任何輸出日志都有一個分隔符,我可以存儲在 .txt 文件中。

除此之外,我通過 Python 腳本運行它。 在這里,在script_name放置要針對其運行 Pylint 的腳本。

import os    
script_name = <source_script>     #example test.py
output_file = <output_pylint_txt> #example test.txt

param1 = 'python -m pylint --max-line-length=400 -d relative-beyond-top-level,wildcard-import --msg-template="{abspath}||{path}||{symbol}||{msg_id}||{line}||{column}||{msg}" --reports=y '+ script_name

 param2 = ' > '+output_file
 param = param1 + param2
 os.system(param)   # this will execute final command on command prompt

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM