除了预先存在的警告类别,用户还可以define their own warning classes,例如下面的代码:
$ cat mwe.py
#!/usr/bin/env python3.5
import warnings
import pprint
class ObnoxiousWarning(UserWarning):
pass
for i in range(3):
print(i)
warnings.warn("I don't like this.", ObnoxiousWarning)
调用Python时,-W
flag controls how to filter warnings。但是当我试图让它忽略我的新鲜警告类别时,我被告知过滤器被忽略了:
$ python3.5 -W ignore::ObnoxiousWarning ./mwe.py
Invalid -W option ignored: unknown warning category: 'ObnoxiousWarning'
0
./mwe.py:11: ObnoxiousWarning: I don't like this.
warnings.warn("I don't like this.", ObnoxiousWarning)
1
2
如何使用命令行为自定义警告类别插入过滤器(与所有UserWarnings相反或基于警告消息进行过滤,我可以这样做?)
修改2018-11-29 :请参阅Issue 22543: -W option cannot use nonstandard categories
答案 0 :(得分:3)
某些答案位于python源代码中。查看_getcategory函数: https://github.com/python/cpython/blob/3.5/Lib/warnings.py#L147
def _getcategory(category):
import re
if not category:
return Warning
if re.match("^[a-zA-Z0-9_]+$", category):
try:
cat = eval(category)
except NameError:
raise _OptionError("unknown warning category: %r" % (category,))
else:
i = category.rfind(".")
module = category[:i]
klass = category[i+1:]
try:
m = __import__(module, None, None, [klass])
except ImportError:
raise _OptionError("invalid module name: %r" % (module,))
try:
cat = getattr(m, klass)
except AttributeError:
raise _OptionError("unknown warning category: %r" % (category,))
if not issubclass(cat, Warning):
raise _OptionError("invalid warning category: %r" % (category,))
return cat
Python尝试评估您的类别或从您在过滤器中指定的模块导入类别。除非您将模块放在PYTHONPATH中,否则这将失败。
PYTHONPATH='<path_to_dir_where_mwe_located>' python -W ignore::mwe.ObnoxiousWarning
这样,如果您在python shell中导入模块,警告将被过滤。 要在命令行中使用过滤器,您必须在执行的模块之外的单独模块中定义警告。
mwe.py
class ObnoxiousWarning(UserWarning):
pass
entrypoint.py
#!/usr/bin/env python3.5
import warnings
import pprint
from mwe import ObnoxiousWarning
for i in range(3):
print(i)
warnings.warn("I don't like this.", ObnoxiousWarning)
最后:
PYTHONPATH='<path_to_dir_where_mwe_located>' python -W ignore::mwe.ObnoxiousWarning ./entrypoint.py
0
1
2
我不明白为什么用警告定义单独的模块有效,但是确实如此。可能有人会解释它。