def capture_stdout ( ) : stdout = sys . stdout sys . stdout = six . moves . cStringIO ( ) try : yield sys . stdout finally : sys . stdout = stdout
capture the standard output stream of a cell into a variable in local namespace ,python
Intercept standard output in a with - context : return : cStringIO instance
def capture_stdout():
    """Intercept standard output in a with-context
    :return: cStringIO instance

    >>> with capture_stdout() as stdout:
            ...
        print stdout.getvalue()
    """
    stdout = sys.stdout
    sys.stdout = six.moves.cStringIO()
    try:
        yield sys.stdout
    finally:
        sys.stdout = stdout