I'm trying to redirect some of the prints in a module, specifically keras-tuner, to a variable. It seems that there might be a similar solution: similar solution
from io import StringIO
import sys
class Capturing(list):
def __enter__(self):
self._stdout = sys.stdout
sys.stdout = self._stringio = StringIO()
return self
def __exit__(self, *args):
self.extend(self._stringio.getvalue().splitlines())
del self._stringio # free up some memory
sys.stdout = self._stdout
Then I wrapped around my code that has a module that prints, for example search_space_summary
with Capturing() as output:
model_info["tuner"].search_space_summary()
However upon inspection there is nothing in output
. But one can simply do a print("Hello")
and it is captured in output
, but not this function.