def linebuffered_stdout ( ) : if sys . stdout . line_buffering : return sys . stdout orig = sys . stdout new = type ( orig ) ( orig . buffer , encoding = orig . encoding , errors = orig . errors , line_buffering = True ) new . mode = orig . mode return new
python set standard output to line buffered
Always line buffer stdout so pipes and redirects are CLI friendly .
def linebuffered_stdout():
    """ Always line buffer stdout so pipes and redirects are CLI friendly. """
    if sys.stdout.line_buffering:
        return sys.stdout
    orig = sys.stdout
    new = type(orig)(orig.buffer, encoding=orig.encoding, errors=orig.errors,
                     line_buffering=True)
    new.mode = orig.mode
    return new