如何通过 warnings.warn 抑制源上下文的打印?
How to suppress the printing of source context by warnings.warn?
如果我将以下代码片段放入文件 annoying.py
:
import warnings
message = 'I know! How about if we are just maximally annoying?!'
warnings.warn(message)
...和 运行 它在 Unix shell 中,这就是我得到的:
% python annoying.py
annoying.py:3: UserWarning: I know! How about if we are just maximally annoying?!
warnings.warn(message)
(我得到与 Python 3.7.3 和 2.7.16 相同的输出。)
我只想打印此输出的第一行。
关于我正在做的事情,第二行只会增加视觉上分散注意力的混乱。 (当有很多警告时,这种无端的混乱变得尤其成问题。)
如何抑制 second/clutter 行?
从 warnings.py 源代码看来 msg.line 控制是否需要打印该额外行。可以猴子修补 WarningMessage 的 init,使 self.line 是 "" 而不是 None,如下所示
import warnings
#modify the __init__ so that self.line = "" instead of None
def new_init(self, message, category, filename, lineno, file=None,
line=None, source=None):
self.message = message
self.category = category
self.filename = filename
self.lineno = lineno
self.file = file
self.line = ""
self.source = source
self._category_name = category.__name__ if category else None
warnings.WarningMessage.__init__ = new_init
message = 'I know! How about if we are just maximally annoying?!'
warnings.warn(message)
这导致:
annoying.py:19: UserWarning: I know! How about if we are just maximally annoying?!
另一种 hacky 方法可能是使用 -W 选项使警告作为错误出现:
% python -Werror::UserWarning annoying.py
如果您有关注 annoying.py
import warnings
message = 'I know! How about if we are just maximally annoying?!'
try:
warnings.warn(message)
except Exception as error:
print(error)
结果
I know! How about if we are just maximally annoying?!
要获取文件名、行号、警告类型和警告消息,我必须执行以下操作
import warnings
import sys, os
message = 'I know! How about if we are just maximally annoying?!'
try:
warnings.warn(message)
except Exception as error:
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print('{}:{}: {}: {}'.format(fname, exc_tb.tb_lineno,exc_type.__name__,exc_obj))
结果是
annoying.py:5: UserWarning: I know! How about if we are just maximally annoying?!
如果我将以下代码片段放入文件 annoying.py
:
import warnings
message = 'I know! How about if we are just maximally annoying?!'
warnings.warn(message)
...和 运行 它在 Unix shell 中,这就是我得到的:
% python annoying.py
annoying.py:3: UserWarning: I know! How about if we are just maximally annoying?!
warnings.warn(message)
(我得到与 Python 3.7.3 和 2.7.16 相同的输出。)
我只想打印此输出的第一行。
关于我正在做的事情,第二行只会增加视觉上分散注意力的混乱。 (当有很多警告时,这种无端的混乱变得尤其成问题。)
如何抑制 second/clutter 行?
从 warnings.py 源代码看来 msg.line 控制是否需要打印该额外行。可以猴子修补 WarningMessage 的 init,使 self.line 是 "" 而不是 None,如下所示
import warnings
#modify the __init__ so that self.line = "" instead of None
def new_init(self, message, category, filename, lineno, file=None,
line=None, source=None):
self.message = message
self.category = category
self.filename = filename
self.lineno = lineno
self.file = file
self.line = ""
self.source = source
self._category_name = category.__name__ if category else None
warnings.WarningMessage.__init__ = new_init
message = 'I know! How about if we are just maximally annoying?!'
warnings.warn(message)
这导致:
annoying.py:19: UserWarning: I know! How about if we are just maximally annoying?!
另一种 hacky 方法可能是使用 -W 选项使警告作为错误出现:
% python -Werror::UserWarning annoying.py
如果您有关注 annoying.py
import warnings
message = 'I know! How about if we are just maximally annoying?!'
try:
warnings.warn(message)
except Exception as error:
print(error)
结果
I know! How about if we are just maximally annoying?!
要获取文件名、行号、警告类型和警告消息,我必须执行以下操作
import warnings
import sys, os
message = 'I know! How about if we are just maximally annoying?!'
try:
warnings.warn(message)
except Exception as error:
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
print('{}:{}: {}: {}'.format(fname, exc_tb.tb_lineno,exc_type.__name__,exc_obj))
结果是
annoying.py:5: UserWarning: I know! How about if we are just maximally annoying?!