Created
February 22, 2018 00:08
-
-
Save turing4ever/d37047994d4e239f67ed87d75464bb63 to your computer and use it in GitHub Desktop.
What if you have to call a noisy function that prints a lot but you can't modify it? Mute it with a context manager.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A context manager to shut down stdout | |
# Within this context, sys.stdout will be written into devnull. | |
# so, effectively it's muted. | |
@contextlib.contextmanager | |
def shut_stdout(): | |
sys.stdout = open(os.devnull, 'w') | |
yield | |
sys.stdout = sys.__stdout__ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment