I know little about Python warnings and exceptions but find myself in a situation where I'm designing a tkinter gui that has an option to execute an outside function imported from a module written by someone else. The function performs calculations but produces no graphs.
Numeric stability issues may arise when I call the function, in which case it returns an output along with a RunTimeWarning that indicates the output may not be very reliable and suggests I adjust the input I used when calling the function.
Of course, the warnings are not visible to the user from within the gui, so I'd like to inform the user of the warning and give them options to proceed. The pseudo code would look something like the following:
output=outside_function(input)
if outside_function issued RuntimeWarning: do something such as issuing error message, asking user to adjust input,etc.
else: proceed with further calculations utilizing the output obtained.
I apologize that this question is somewhat vague and lacks a minimal reproducible example. However is there some way, perhaps using the warnings module, which allows me to determine whether outside_function issued RuntimeWarning is true or false?
If the warning extended Warning
(which RuntimeWarning
should), you should be able to catch it with the catch_warnings
context manager, see here for an example.
But you should be able to do something like:
import warnings
with warnings.catch_warnings(record=True) as w:
outside_function(...)
# code to check if there was a warning
if len(w) > 0:
# do something, e.g. check to see the warning type/whatnot
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.