INNER CODE UNIT · Python
Tee
huggingface/large_language_model_training_playbook · debug/NicerTrace.py:158
class Tee:
"""
A helper class to tee print's output into a file.
Usage:
sys.stdout = Tee(filename)
"""
def __init__(self, filename):
self.stdout = sys.stdout
self.file = open(filename, "a")
def __getattr__(self, attr):
return getattr(self.stdout, attr)
def write(self, msg):
# comment out the next line if you don't want to write to stdout
self.stdout.write(msg)
self.file.write(msg)