hi
first post here :)
i have a recurring use pattern where i
want to save to file everything i print to the screen. i have used the
standard logging module and found it too cumbersome for my simple use
case, and i find myself changing all my print()'s to custom myprint()'s.
i am interested in suggesting several additions to the standard
libraries, as i feel this use case is common enough, important enough,
and currently painful enough to justify such changes, and that all four
of the following suggestions could be useful in a complementary fashion.
1. allow the print() "file" argument to take a list of streams (currently it can take only a single stream). e.g:
with open('output.txt', 'w') as f:
print('hello', file=[sys.stdout, f])
this
seems like the quickest and least resistant path for the user. need to
decide how multiple streams will work with the flush argument.
2.
modify contextlib.redirect_stdout() to take an additional boolean
argument which we can call tee/replicate/duplicate/clone etc. when True
it will duplicate instead of just redirecting:
with open('output.txt', 'w') as f:
with redirect_stdout(f, tee=True): # will duplicate instead of redirecting
print('hello')
or we could add
a new context manager contextlib.copy_stdout()
3. allow the contextlib.redirect_stdout()
"new_target" argument to take a list of streams, or allow specifying
multiple arguments for multiple streams. e.g:
with open('output.txt', 'w') as f:
with redirect_stdout(sys.stdout, f): # or
redirect_stdout([sys.stdout, f])
print('hello')
this has the benefit
over the tee argument of allowing more than one additional stream, but
you need to explicitly specify stdout. so would be nice to have both
modifications.
4. add to the standard io library
a new class which gives you the write interface of a single stream, but
is a wrapper that will write to multiple streams:
with open('output.txt', 'w') as f:
multi = io.OutputStreamCollection(sys.stdout, f)
multi.write('hello\n')
# or: print('hello', file=multi)
eyal.