本文整理匯總了Python中numpy.testing.suppress_warnings方法的典型用法代碼示例。如果您正苦於以下問題:Python testing.suppress_warnings方法的具體用法?Python testing.suppress_warnings怎麽用?Python testing.suppress_warnings使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類numpy.testing
的用法示例。
在下文中一共展示了testing.suppress_warnings方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_fcompiler_flags_append_warning
# 需要導入模塊: from numpy import testing [as 別名]
# 或者: from numpy.testing import suppress_warnings [as 別名]
def test_fcompiler_flags_append_warning(monkeypatch):
# Test to check that the warning for append behavior changing in future
# is triggered. Need to use a real compiler instance so that we have
# non-empty flags to start with (otherwise the "if var and append" check
# will always be false).
try:
with suppress_warnings() as sup:
sup.record()
fc = numpy.distutils.fcompiler.new_fcompiler(compiler='gnu95')
fc.customize()
except numpy.distutils.fcompiler.CompilerNotFound:
pytest.skip("gfortran not found, so can't execute this test")
# Ensure NPY_DISTUTILS_APPEND_FLAGS not defined
monkeypatch.delenv('NPY_DISTUTILS_APPEND_FLAGS', raising=False)
for opt, envvar in customizable_flags:
new_flag = '-dummy-{}-flag'.format(opt)
with suppress_warnings() as sup:
sup.record()
prev_flags = getattr(fc.flag_vars, opt)
monkeypatch.setenv(envvar, new_flag)
with suppress_warnings() as sup:
sup.record()
new_flags = getattr(fc.flag_vars, opt)
if prev_flags:
# Check that warning was issued
assert len(sup.log) == 1
monkeypatch.delenv(envvar)
assert_(new_flags == [new_flag])
示例2: __enter__
# 需要導入模塊: from numpy import testing [as 別名]
# 或者: from numpy.testing import suppress_warnings [as 別名]
def __enter__(self):
if self._entered:
raise RuntimeError("cannot enter suppress_warnings twice.")
self._orig_show = warnings.showwarning
self._filters = warnings.filters
warnings.filters = self._filters[:]
self._entered = True
self._tmp_suppressions = []
self._tmp_modules = set()
self._forwarded = set()
self.log = [] # reset global log (no need to keep same list)
for cat, mess, _, mod, log in self._suppressions:
if log is not None:
del log[:] # clear the log
if mod is None:
warnings.filterwarnings(
"always", category=cat, message=mess)
else:
module_regex = mod.__name__.replace('.', r'\.') + '$'
warnings.filterwarnings(
"always", category=cat, message=mess,
module=module_regex)
self._tmp_modules.add(mod)
warnings.showwarning = self._showwarning
self._clear_registries()
return self
示例3: test_py2_float_print
# 需要導入模塊: from numpy import testing [as 別名]
# 或者: from numpy.testing import suppress_warnings [as 別名]
def test_py2_float_print(self):
# gh-10753
# In python2, the python float type implements an obsolte method
# tp_print, which overrides tp_repr and tp_str when using "print" to
# output to a "real file" (ie, not a StringIO). Make sure we don't
# inherit it.
x = np.double(0.1999999999999)
with TemporaryFile('r+t') as f:
print(x, file=f)
f.seek(0)
output = f.read()
assert_equal(output, str(x) + '\n')
# In python2 the value float('0.1999999999999') prints with reduced
# precision as '0.2', but we want numpy's np.double('0.1999999999999')
# to print the unique value, '0.1999999999999'.
# gh-11031
# Only in the python2 interactive shell and when stdout is a "real"
# file, the output of the last command is printed to stdout without
# Py_PRINT_RAW (unlike the print statement) so `>>> x` and `>>> print
# x` are potentially different. Make sure they are the same. The only
# way I found to get prompt-like output is using an actual prompt from
# the 'code' module. Again, must use tempfile to get a "real" file.
# dummy user-input which enters one line and then ctrl-Ds.
def userinput():
yield 'np.sqrt(2)'
raise EOFError
gen = userinput()
input_func = lambda prompt="": next(gen)
with TemporaryFile('r+t') as fo, TemporaryFile('r+t') as fe:
orig_stdout, orig_stderr = sys.stdout, sys.stderr
sys.stdout, sys.stderr = fo, fe
# py2 code.interact sends irrelevant internal DeprecationWarnings
with suppress_warnings() as sup:
sup.filter(DeprecationWarning)
code.interact(local={'np': np}, readfunc=input_func, banner='')
sys.stdout, sys.stderr = orig_stdout, orig_stderr
fo.seek(0)
capture = fo.read().strip()
assert_equal(capture, repr(np.sqrt(2)))