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 block

  • Muffle - 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"))
Terminal output of the example, the log file holding the same lines

Submodules#