当我想使用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命令时。
使用子进程运行pylint可能是最简单,最受支持的方法。您可以手动配置和运行pylint.lint.PyLinter
,但据我所知,它是无证的,不受支持的,绝对给您带来痛苦,并且容易掉下来(因为pylint崩溃-这是很常见的-删除整个脚本)。我们曾经在$ dayjob上使用过这种方法,然后又回到了在子进程中运行CLI的方式,它更加可靠。
我使用pylint的一种方法,以便无论我有什么输出日志,都有一个分隔符,我可以将其存储在.txt文件中。