只打印警告信息 [英] Print only the message on warnings
本文介绍了只打印警告信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在验证器中发出了很多警告,我想禁止标准输出中的所有内容,除了提供给 warnings.warn()
的消息.
I'm issuing lots of warnings in a validator, and I'd like to suppress everything in stdout except the message that is supplied to warnings.warn()
.
也就是说,现在我看到了:
I.e., now I see this:
./file.py:123: UserWarning: My looong warning message
some Python code
我想看看这个:
My looong warning message
编辑 2: 覆盖 warnings.showwarning()
结果可行:
def _warning(
message,
category = UserWarning,
filename = '',
lineno = -1):
print(message)
...
warnings.showwarning = _warning
warnings.warn('foo')
推荐答案
Monkeypatch warnings.showwarning()
使用您自己的自定义函数.
Monkeypatch warnings.showwarning()
with your own custom function.
这篇关于只打印警告信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文