Temporarily Suppressing Warnings in Python

You know the ones I’m talking about those ***** messages that pop up in your console whenever you do something “wrong” (or at least not exactly according to PEP 8).
But what if you just don’t care? What if you want to ignore these warnings and move on with your life? Well, bro, I have good news for you Python has a solution! It’s called the “warnings” module.
Now, before we dive into how to use this magical tool, let me first explain why it exists in the first place. The reason is simple: warnings are meant to help you catch potential issues with your code early on so that you can fix them before they become bigger problems down the line. But sometimes, these warnings aren’t actually a big deal maybe you know what you’re doing and don’t want to be bothered by them anymore.
That’s where the “warnings” module comes in handy! This little gem allows you to temporarily suppress certain warnings so that they won’t show up in your console or log files. And best of all, it’s super easy to use!
Here’s an example:

# Import the "warnings" module
import warnings

# Suppress a specific warning (in this case, "DeprecationWarning") for the duration of the script
# Use the "catch_warnings" context manager to temporarily suppress warnings
with warnings.catch_warnings():
    # Use the "simplefilter" function to specify which warning to ignore and for how long
    warnings.simplefilter("ignore", DeprecationWarning)
    
    # Do some potentially deprecated code here... (e.g. using an outdated function)
    # The DeprecationWarning will not be displayed in the console or log files
    
# The DeprecationWarning will now be displayed again, as the context manager has ended

In this example, we’re using a context manager (the “with” statement) to temporarily suppress any “DeprecationWarnings” that might be raised within the script. We do this by first importing the warnings module and then creating an instance of it with the “catch_warnings()” function.
Next, we use the “simplefilter()” method to filter out a specific type of warning (in this case, “DeprecationWarning”). This tells Python that any instances of this particular warning should be ignored for the duration of our script.
Finally, we do some potentially deprecated code within the context manager in this example, I’m just printing out a message to let you know what’s going on. But feel free to replace this with your own custom code!
And that’s it! With these simple steps, you can now temporarily suppress warnings in Python and get back to coding without being bothered by ***** messages.

SICORPS