stouputils.ctx package#
This module provides context managers for various utilities such as logging to a file, measuring execution time, silencing output, and setting multiprocessing start methods.
LogToFile- Context manager to log to a file every print call (with LINE_UP handling)MeasureTime- Context manager to measure execution time of a code blockMuffle- Silences output and can replay it on error (alternative to@silent)DoNothing- Context manager that does nothing (no-op)SetMPStartMethod- Context manager to temporarily set multiprocessing start method
import time
import stouputils as stp
# Everything printed inside this block also goes to run.log
with stp.LogToFile("run.log"):
stp.info("Training started")
# Print how long the indented block took
with stp.MeasureTime(stp.progress, "Loading the dataset"):
time.sleep(0.6)
# Hide everything printed inside this block
with stp.Muffle():
print("A chatty library prints this, nobody sees it")
stp.warning("Validation loss went up")
stp.info("Content of run.log:")
print(stp.read_file("run.log"))
Submodules#
- stouputils.ctx.common module
- stouputils.ctx.do_nothing module
- stouputils.ctx.fd_capture module
- stouputils.ctx.log_to_file module
- stouputils.ctx.measure_time module
- stouputils.ctx.muffle module
- stouputils.ctx.set_mp_start_method module