python - Pylint:如何防止打印
问题描述
当我使用 pylint 时:
import pylint.lint
options = [
filename,
"--output-format=json"
]
results = pylint.lint.Run(options, do_exit=False)
messages = results.linter.reporter.messages
我的messages
变量具有正确的信息,如JSON
. 但是,pylint 在控制台中打印JSON
消息...
我怎样才能防止print()
pylint ?
此选项不起作用:
options = [
filename,
"--output-format=json",
"--reports=no" # Tells whether to display a full report or only the messages.
]
见https://pylint.readthedocs.io/en/stable/technical_reference/features.html#reports-options
解决方案
做好它的唯一方法......是使用你的ReporterClass
.
import pylint.lint
options = [
filename,
"--output-format=mypackage.mymodule.MyReporterClass"
]
results = pylint.lint.Run(options, do_exit=False)
messages = results.linter.reporter.messages
下面的代码具有相同的行为,json
但它的display_messages
方法什么也不做
import html
from pylint.interfaces import IReporter
from pylint.reporters import *
class MyReporterClass(BaseReporter):
"""Report messages and layouts."""
__implements__ = IReporter
name = "myreporter"
extension = "myreporter"
def __init__(self, output=sys.stdout):
BaseReporter.__init__(self, output)
self.messages = []
def handle_message(self, msg):
"""Manage message of different type and in the context of path."""
self.messages.append(
{
"type": msg.category,
"module": msg.module,
"obj": msg.obj,
"line": msg.line,
"column": msg.column,
"path": msg.path,
"symbol": msg.symbol,
"message": html.escape(msg.msg or "", quote=False),
"message-id": msg.msg_id,
}
)
def display_messages(self, layout):
"""Do nothing."""
def display_reports(self, layout):
"""Do nothing."""
def _display(self, layout):
"""Do nothing."""
def register(linter):
"""Register the reporter classes with the linter."""
linter.register_reporter(MyReporterClass)
print()
PyLint在评估代码后将不再执行此操作。
推荐阅读
- python - Apache Beam 不会针对 google 数据流上的会话窗口提前重复触发
- java - 避免 QProcess 被杀死 (QProcess: Destroyed while process is still running)
- algorithm - 有向树变异算法
- xampp - xampp 在 ubuntu 中安装但 php 和 mysql 显示未找到
- html - 使用 pd.read_html 从网页导入表格
- oracle - 数据透视摘要 Oracle Apex
- apache-spark - pySpark 3.0 如何修剪所有列的空间
- xcode - 如何启用手机身份验证 firebase、flutter、xcode
- javascript - 如何为单个属性嵌套 Redux Toolkit 减速器
- r - 闪亮的服务器重复多次独特的输入