numpy.seterrcall(func)
[source]
Set the floating-point error callback function or log object.
There are two ways to capture floating-point error messages. The first is to set the error-handler to ‘call’, using seterr
. Then, set the function to call using this function.
The second is to set the error-handler to ‘log’, using seterr
. Floating-point errors then trigger a call to the ‘write’ method of the provided object.
Parameters: |
|
---|---|
Returns: |
|
See also
Callback upon error:
>>> def err_handler(type, flag): ... print("Floating point error (%s), with flag %s" % (type, flag)) ...
>>> saved_handler = np.seterrcall(err_handler) >>> save_err = np.seterr(all='call') >>> from collections import OrderedDict
>>> np.array([1, 2, 3]) / 0.0 Floating point error (divide by zero), with flag 1 array([inf, inf, inf])
>>> np.seterrcall(saved_handler) <function err_handler at 0x...> >>> OrderedDict(sorted(np.seterr(**save_err).items())) OrderedDict([('divide', 'call'), ('invalid', 'call'), ('over', 'call'), ('under', 'call')])
Log error message:
>>> class Log(object): ... def write(self, msg): ... print("LOG: %s" % msg) ...
>>> log = Log() >>> saved_handler = np.seterrcall(log) >>> save_err = np.seterr(all='log')
>>> np.array([1, 2, 3]) / 0.0 LOG: Warning: divide by zero encountered in true_divide array([inf, inf, inf])
>>> np.seterrcall(saved_handler) <numpy.core.numeric.Log object at 0x...> >>> OrderedDict(sorted(np.seterr(**save_err).items())) OrderedDict([('divide', 'log'), ('invalid', 'log'), ('over', 'log'), ('under', 'log')])
© 2005–2019 NumPy Developers
Licensed under the 3-clause BSD License.
https://docs.scipy.org/doc/numpy-1.17.0/reference/generated/numpy.seterrcall.html