Python & R: warning information management

Sometimes very user-friendly computer program, such as to give you a warning message;

Computer programs sometimes very humane, like always give you warning ......

If your program is to give customers, have run landscaping requirements;

Especially in the case of another such warning appeared in the loop, then every cycle should print out a bunch of warnings ...... it is nonsense.

How to do?

Obviously, the warnings (Warning) is not an error (Error), and the program will not run warnings and suspension.

Those original developers must take this into account already set up for warning output adjustment mode.

We Python and R, for example, look at the basic warning information management operations.

 

(A) Python

We write the following program:

import warnings as w
if 1==1:
    w.warn('警告!!!')

Operating results as follows:

Warning (from warnings module):
  File "D:/warnings.py", line 3
    w.warn('警告!!!')
UserWarning: 警告!!!

1) When executing the program using the command line mode, can be added to the execution command in the -W ignore:

python -W ignore XXX.py

Operating results at this time does not output a warning.

2) may also be introduced in the program module warnings, the warning using the filter.

import warnings as w
w.filterwarnings("ignore")
if 1==1:
    w.warn('警告!!!')

In this case run results are not output a warning.

In fact, there are several other parameters are available to control the warning:

Value

Disposition

"error"

turn matching warnings into exceptions

"ignore"

never print matching warnings

"always"

always print matching warnings

"default"

print the first occurrence of matching warnings for each location where the warning is issued

"module"

print the first occurrence of matching warnings for each module where the warning is issued

"once"

print only the first occurrence of matching warnings, regardless of location

As the program read:

import warnings as w
w.filterwarnings("ignore")
if 1==1:
    w.warn('警告!!!')
w.filterwarnings("always")
if 1==1:
    w.warn('警告!!!---')
w.filterwarnings("error")
if 1==1:
    w.warn('警告!!!---===')
print('sfsdfsfsdfsdf')

Output:

Warning (from warnings module):
  File "D:/warnings.py", line 7
    w.warn('警告!!!---')
UserWarning: 警告!!!---
Traceback (most recent call last):
  File "D:/warnings.py", line 10, in <module>
    w.warn('警告!!!---===')
UserWarning: 警告!!!---===

 

(Ii) R

R language to control the warning even more convenient. Command can be inserted before the program:

options(warn=N)

Wherein, N values ​​can be negative, 0,1,2.

warn = negative, all the warning message is ignored.
warn = 0 (the default), then all warning messages will be stored until the upper end of the run function.
warn = 1, then once generated warning message, this message will be displayed immediately.
warn = 2 or larger values, the warning message will be displayed immediately and converted into error message.

 

References:

https://docs.python.org/2/library/warnings.html#temporarily-suppressing-warnings

https://blog.csdn.net/xiaodongxiexie/article/details/65646239

https://blog.csdn.net/stat_elliott/article/details/37878247

Guess you like

Origin www.cnblogs.com/maoerbao/p/11849714.html