stouputils.ctx module¶
This module provides context managers for temporarily silencing output.
Muffle: Context manager that temporarily silences output (alternative to stouputils.decorators.silent())
LogToFile: Context manager to log to a file every calls to the print functions in stouputils.print

- class Muffle(mute_stderr: bool = False)[source]¶
Bases:
object
Context manager that temporarily silences output.
Alternative to stouputils.decorators.silent()
Examples
>>> with Muffle(): ... print("This will not be printed")
- mute_stderr: bool¶
Attribute remembering if stderr should be muted
- original_stdout: TextIO¶
Attribute remembering original stdout
- original_stderr: TextIO¶
Attribute remembering original stderr
- class LogToFile(path: str, mode: str = 'w', encoding: str = 'utf-8')[source]¶
Bases:
object
Context manager to log to a file.
This context manager allows you to temporarily log output to a file while still printing normally. The file will receive log messages without ANSI color codes.
- Parameters:
path (str) – Path to the log file
mode (str) – Mode to open the file in (default: “w”)
encoding (str) – Encoding to use for the file (default: “utf-8”)
Examples
> import stouputils as stp > with stp.LogToFile("output.log"): > stp.info("This will be logged to output.log and printed normally")
- path: str¶
Attribute remembering path to the log file
- mode: str¶
Attribute remembering mode to open the file in
- encoding: str¶
Attribute remembering encoding to use for the file
- file: IO[Any]¶
Attribute remembering opened file
- static common(logs_folder: str, filepath: str, func: Callable[[...], Any], *args: Any, **kwargs: Any) Any [source]¶
Common code used at the beginning of a program to launch main function
- Parameters:
logs_folder (str) – Folder to store logs in
filepath (str) – Path to the main function
func (Callable[..., Any]) – Main function to launch
*args (tuple[Any, ...]) – Arguments to pass to the main function
**kwargs (dict[str, Any]) – Keyword arguments to pass to the main function
- Returns:
Return value of the main function
- Return type:
Any
Examples
>>> if __name__ == "__main__": ... LogToFile.common(f"{ROOT}/logs", __file__, main)