DoubleTee

giant.utilities.tee:

class giant.utilities.tee.DoubleTee(file, mode='a+', buff=-1)[source]

This class provides the ability to simultaneously tee both STDOUT and STDERR to a file.

This is done by creating two instances of the Tee class, one for STDOUT and STDERR.

This class can be used as a context manager, which will automatically handle closing the Tee instances for you. If you do not use it as a context manager, you should call close() manually when you are done.

Example:

>>> from giant.utilities.tee import DoubleTee
>>> import sys
>>> with DoubleTee('mylog.txt'):
...     print('first line')
...     print('Second line')
...     print('I like tacos!!', file=sys.stderr)
first line
second line
I like tacos!!
>>> with open('mylog.txt', 'r') as ifile:
...     print(ifile.readlines())
['first line\n', 'second line\n', 'I like tacos!!\n']

Non-context manager example:

>>> from giant.utilities.tee import DoubleTee
>>> import sys
>>> doubletee = DoubleTee('mylog.txt')
>>> print('first line')
first line
>>> print('second line')
second line
>>> print('I like tacos!!', file=sys.stderr)
I like tacos!!
>>> doubletee.close()
>>> with open('mylog.txt', 'r') as ifile:
...     print(ifile.readlines())
['first line\n', 'second line\n', 'I like tacos!!\n']
Parameters:
  • file (Path | str | IO) – The file to write to as a string or a IO object which defines write, flush, and close methods.

  • mode (str) – The mode to open the file with. Should be either ‘w’, ‘a’, or ‘r+’. Ignored if file is IO

  • buff (int) – How to buffer the file. Should be -1 (for system default) or >= 1. Ignored if file is IO

stdout: Tee

An instance of Tee for teeing stdout to the requested file

stderr: Tee

An instance of Tee for teeing stderr to the requested file

Summary of Methods

close

Closes both the STDOUT and STDERR Tees.