如何使用python代码获取pylint错误消息的简要摘要,或获取pylint错误、警告和重构的数量?
pylint -rn这允许我们获得pylint输出,只包含消息,并排除报告。到目前为止,我尝试的是:
lint_arg = "pylint -rn %s"%file_name
pr = subprocess.Popen(
arg, cwd=os.path.dirname(lint_path),
shell=True, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
(out, error) = pr.communicate()
pylint_result = out + "\n" + error
file=open("pylint_output",w)
file.write(pylint_result)
file.close()我只想获得发生错误和警告的行数,并将此信息移动到字符串中。我的想法如下:
步骤1:使用subprocess.Popen()在python脚本中运行pylint
步骤2:将输出存储在文件或变量中。
步骤3:解析文件,生成预期输出,如下:
预期输出:以下文件< filename>具有:
5行号中的警告: 11、34、56(出现警告的行)
2行号错误:2 (发生错误的行)。
如何借助pylint命令或使用python脚本来完成此操作?感谢您的帮助。提前谢谢。
发布于 2014-11-07 20:40:27
首先,在引用Pylint's docs时不要使用Popen
在module_name.py模块上静默运行Pylint,并获得其标准输出和错误:
从pylint导入epylint as lint (pylint_stdout,pylint_stderr) = lint.py_run('module_name.py',True)
第二,查看输出格式,man pylint说:
消息使用默认文本输出,消息格式为: MESSAGE_TYPE: LINE_NUM:OBJECT:
假设line是一行Pylint输出,这将收集您需要的所有信息:
pylint_res = {'C': [], 'R': [], 'W': [], 'E': [], 'F': []}
msg_type, lineno = line.split(':', 2)[:2]
pylint_res[msg_type].append(lineno)您所需要做的就是迭代Pylint的输出,如上所述添加到pylint_res,然后打印出结果。
发布于 2020-04-08 00:09:24
例如:要计算错误数,可以使用以下代码片段:
from pylint.lint import Run
paths_to_check = ['./']
pylint_params = ["--rcfile=.pylintrc", "--exit-zero", "--errors-only"]
results = Run(pylint_params + paths_to_check, do_exit=False)
errors = results.linter.stats['error']
message = ('Number of errors: {}'.format(errors))https://stackoverflow.com/questions/26801144
复制相似问题