I am using a python module that generates a lot of stdout/stderr Future warnings when calling its constructor like this:
foo_obj = foo(par1, par2)
I can't really edit, or suppress the output inside the module, since I have no access to it/don't want to mess with it.
Is there a way to easily do something like:
if verbose:
foo_obj = foo(par1, par2)
#generating lots of output
else:
foo_obj = foo(par1, par2)
#and do something fancy to the above to not generate and stdout/stderr
Edit/Clarification
If I take the solution from Suppress stdout / stderr print from Python functions I cannot suppress the output. I have constructed a more elaborate example here:
import numpy as np
import warnings
class WritingOutput(object):
def __init__(self, b):
self._a = 5
self._b = b
self.generate_output()
def generate_output(self):
print("This is normal output")
warnings.warn("This is a warning")
print ('a is %d and b is %s ' %(self._a, self._b))
wo_obj = WritingOutput('10')
print (wo_obj._a)
with suppress_stdout_stderr():
wo_obj = WritingOutput('10')
With the suggested fix the output is still printed. I must admit that I don't quite understand how the fix should work in the first place. I need something that would return both the output that is written out (both stderr and stdout) in WritingOutput, as well as the object I generate. I can't change the function call in the constructor of my actual problem.