code: def capture_stdout():
      
      stdout = sys.stdout
      sys.stdout = six.moves.cStringIO()
      try:
          yield sys.stdout
      finally:
          sys.stdout = stdout
docs: Intercept standard output in a with-context
      :return: cStringIO instance
  
      >>> with capture_stdout() as stdout:
              ...
          print stdout.getvalue()