def coalesce_streams(outputs):
    """
    Merge all stream outputs with shared names into single streams
    to ensure deterministic outputs.

    Parameters
    ----------
    outputs : iterable of NotebookNodes
        Outputs being processed
    """
    if not outputs:
        return outputs

    new_outputs = []
    streams = {}
    for output in outputs:
        if (output.output_type == 'stream'):
            if output.name in streams:
                streams[output.name].text += output.text
            else:
                new_outputs.append(output)
                streams[output.name] = output
        else:
            new_outputs.append(output)

    # process \r and \b characters
    for output in streams.values():
        old = output.text
        while len(output.text) < len(old):
            old = output.text
            # Cancel out anything-but-newline followed by backspace
            output.text = backspace_pat.sub('', output.text)
        # Replace all carriage returns not followed by newline
        output.text = carriagereturn_pat.sub('', output.text)

    return new_outputs
def coalesce_streams ( outputs ) : if not outputs : return outputs new_outputs = [ ] streams = { } for output in outputs : if ( output . output_type == 'stream' ) : if output . name in streams : streams [ output . name ] . text += output . text else : new_outputs . append ( output ) streams [ output . name ] = output else : new_outputs . append ( output ) for output in streams . values ( ) : old = output . text while len ( output . text ) < len ( old ) : old = output . text output . text = backspace_pat . sub ( '' , output . text ) output . text = carriagereturn_pat . sub ( '' , output . text ) return new_outputs
Merge all stream outputs with shared names into single streams
    to ensure deterministic outputs.

    Parameters
    ----------
    outputs : iterable of NotebookNodes
        Outputs being processed