carlogtt_python_library.utils.context_managers module

This module …

carlogtt_python_library.utils.context_managers.redirect_stdout_to_file(fileobj: TextIOWrapper)[source]

Context manager to redirect stdout to file.

Parameters:

fileobj – Opened file object to redirect stdout to.

Example

with open(“file.txt”, ‘w’) as file:
with redirect_stdout_to_file(file):

print(“Hello World!”) # This will print to file.

carlogtt_python_library.utils.context_managers.redirect_stdout_to_stderr()[source]

Context manager to redirect stdout to stderr.

Example

with redirect_stdout_to_stderr():

print(“Hello World!”) # This will print to stderr.

carlogtt_python_library.utils.context_managers.suppress_errors(*exceptions: type[Exception])[source]

Context manager to suppress specified exceptions.

Parameters:

exceptions – Variable length exception list which includes the exception classes of exceptions to be suppressed.

Example

with suppress_errors(ZeroDivisionError):

1/0 # This will not raise an exception