Let's say I have a file-like object like StreamIO and want the python's warning module write all warning messages to it. How do I do that?
Python programmers issue warnings by calling the warn() function defined in this module. (C programmers use PyErr_WarnEx() ; see Exception Handling for details). Warning messages are normally written to sys.
Use warnings. filterwarnings() to ignore deprecation warnings filterwarnings(action, category=DeprecationWarning) with action as "ignore" and category set to DeprecationWarning to ignore any deprecation warnings that may rise. Leave category unset to ignore all warnings.
removing is danger because user may used that and if a developer want to remove a thing first have to notify others to don't use this feature or things and after this he can remove. and DeprecationWarning is this notification.
Try reassigning warnings.showwarning i.e.
#!/sw/bin/python2.5
import warnings, sys
def customwarn(message, category, filename, lineno, file=None, line=None):
sys.stdout.write(warnings.formatwarning(message, category, filename, lineno))
warnings.showwarning = customwarn
warnings.warn("test warning")
will redirect all warnings to stdout.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With