pytb.io module¶
Redirecting output streams¶
The io Module offers function to temporarly redirect or mirror
stdout and stderr streams to a file
Stream redirection:
>>> from pytb.io import redirected_stdout
>>> with redirected_stdout('stdout.txt'):
... print('this will be written to stdout.txt and not to the console')
Stream mirroring¶
>>> from pytb.io import mirrored_stdstreams
>>> with mirrored_stdstreams('alloutput.txt'):
... print('this will be written to alloutput.txt AND to the console')
API Documentation¶
This module contains a set of helpers for common Input/Output related tasks
-
class
pytb.io.Tee(*args)[source]¶ Bases:
objectA N-ended T-piece (manifold) for File objects that supports writing. This is useful if you want to write to multiple files or file-like objects (e.g.
sys.stdout,sys.stderr) simultaneously.>>> import sys, io >>> file_like = io.StringIO() >>> combined = Tee(file_like, sys.stdout) >>> _ = combined.write('This is printed into a file and on stdout\n') This is printed into a file and on stdout >>> assert file_like.getvalue() == 'This is printed into a file and on stdout\n'
-
close() → None[source]¶ Close all connected files
This does avoid closing
sys.__stdout__andsys.__stderr__>>> import sys, io >>> file_like = io.StringIO() >>> combined = Tee(file_like, sys.__stdout__) >>> file_like.closed False >>> combined.close() >>> file_like.closed True >>> sys.stdout.closed False
-
-
pytb.io.mirrored_stdout(file: Union[str, TextIO, pytb.io.Tee]) → Generator[TextIO, None, None][source]¶ ContextManager that mirrors stdout to a given file-like object and restores the original state when leaving the context
This is essentially using a
Teepiece manifold tofileandsys.stdoutas a parameter toredirected_stdoutParameters: file – string or file-like object to mirror stdout to. If passed a string, the file is opened for writing and closed after the contextmanager exits >>> import io >>> outfile = io.StringIO() >>> with mirrored_stdout(outfile): ... print('this is written to outfile and stdout') this is written to outfile and stdout >>> assert outfile.getvalue() == 'this is written to outfile and stdout\n'
-
pytb.io.mirrored_stdstreams(file: Union[str, TextIO, pytb.io.Tee]) → Generator[TextIO, None, None][source]¶ Version of
mirrored_stdout()but mirrorsstderrandstdoutto file
-
pytb.io.redirected_stderr(file: Union[str, TextIO, pytb.io.Tee]) → Generator[TextIO, None, None][source]¶ Same functionality as
redirect_stdoutbut redirects the stderr stram instead
-
pytb.io.redirected_stdout(file: Union[str, TextIO, pytb.io.Tee]) → Generator[TextIO, None, None][source]¶ ContextManager that redirects stdout to a given file-like object and restores the original state when leaving the context
Parameters: file – string or file-like object to redirect stdout to. If passed a string, the file is opened for writing and closed after the contextmanager exits >>> import io >>> outfile = io.StringIO() >>> with redirected_stdout(outfile): ... print('this is written to outfile') >>> assert outfile.getvalue() == 'this is written to outfile\n'
-
pytb.io.redirected_stdstreams(file: Union[str, TextIO, pytb.io.Tee]) → Generator[TextIO, None, None][source]¶ redirects both output streams (
stderrandstdout) tofile
-
pytb.io.render_text(text: str, maxwidth: int = -1) → str[source]¶ Attempt to render a text like an (potentiall infinitely wide) terminal would.
Thus carriage-returns move the cursor to the start of the line, so subsequent characters overwrite the previous.
>>> render_text('asd\rbcd\rcde\r\nqwe\rert\n123', maxwidth=2) 'cd\ne \ner\nt \n12\n3'
Parameters: - text – Input text to render
- maxwidth – if > 0, wrap the text to the specified maximum length using the textwrapper library