stouputils.print module

This module provides utility functions for printing messages with different levels of importance.

If a message is printed multiple times, it will be displayed as “(xN) message” where N is the number of times the message has been printed.

stouputils print examples
remove_colors(text: str) str[source]

Remove the colors from a text

is_same_print(*args: Any, **kwargs: Any) bool[source]

Checks if the current print call is the same as the previous one.

info(*values: Any, color: str = '\x1b[92m', text: str = 'INFO ', prefix: str = '', file: TextIO | list[TextIO] | None = None, **print_kwargs: Any) None[source]

Print an information message looking like “[INFO HH:MM:SS] message” in green by default.

Parameters:
  • values (Any) – Values to print (like the print function)

  • color (str) – Color of the message (default: GREEN)

  • text (str) – Text of the message (default: “INFO “)

  • prefix (str) – Prefix to add to the values

  • file (TextIO|list[TextIO]) – File(s) to write the message to (default: sys.stdout)

  • print_kwargs (dict) – Keyword arguments to pass to the print function

debug(*values: Any, **print_kwargs: Any) None[source]

Print a debug message looking like “[DEBUG HH:MM:SS] message” in cyan by default.

alt_debug(*values: Any, **print_kwargs: Any) None[source]

Print a debug message looking like “[DEBUG HH:MM:SS] message” in blue by default.

suggestion(*values: Any, **print_kwargs: Any) None[source]

Print a suggestion message looking like “[SUGGESTION HH:MM:SS] message” in cyan by default.

progress(*values: Any, **print_kwargs: Any) None[source]

Print a progress message looking like “[PROGRESS HH:MM:SS] message” in magenta by default.

warning(*values: Any, **print_kwargs: Any) None[source]

Print a warning message looking like “[WARNING HH:MM:SS] message” in yellow by default and in sys.stderr.

error(*values: Any, exit: bool = True, **print_kwargs: Any) None[source]

Print an error message (in sys.stderr and in red by default) and optionally ask the user to continue or stop the program.

Parameters:
  • values (Any) – Values to print (like the print function)

  • exit (bool) – Whether to ask the user to continue or stop the program, false to ignore the error automatically and continue

  • print_kwargs (dict) – Keyword arguments to pass to the print function

whatisit(*values: ~typing.Any, print_function: ~typing.Callable[[...], None] = <function debug>, max_length: int = 250, color: str = '\x1b[96m', **print_kwargs: ~typing.Any) None[source]

Print the type of each value and the value itself, with its id and length/shape.

The output format is: “type, <id id_number>: (length/shape) value”

Parameters:
  • values (Any) – Values to print

  • print_function (Callable) – Function to use to print the values (default: debug())

  • max_length (int) – Maximum length of the value string to print (default: 250)

  • color (str) – Color of the message (default: CYAN)

  • print_kwargs (dict) – Keyword arguments to pass to the print function

breakpoint(*values: ~typing.Any, print_function: ~typing.Callable[[...], None] = <function warning>, **print_kwargs: ~typing.Any) None[source]

Breakpoint function, pause the program and print the values.

Parameters:
  • values (Any) – Values to print

  • print_function (Callable) – Function to use to print the values (default: warning())

  • print_kwargs (dict) – Keyword arguments to pass to the print function

class TeeMultiOutput(*files: IO[Any], strip_colors: bool = True, ascii_only: bool = True, ignore_lineup: bool = True)[source]

Bases: object

File-like object that duplicates output to multiple file-like objects.

Parameters:
  • *files (IO[Any]) – One or more file-like objects that have write and flush methods

  • strip_colors (bool) – Whether to strip ANSI color codes from output sent to non-stdout/stderr files

  • ascii_only (bool) – Whether to replace non-ASCII characters with their ASCII equivalents for non-stdout/stderr files

  • ignore_lineup (bool) – Whether to ignore lines containing LINE_UP escape sequence in non-terminal outputs

Examples

>>> f = open("logfile.txt", "w")
>>> original_stdout = sys.stdout
>>> sys.stdout = TeeMultiOutput(sys.stdout, f)
>>> print("Hello World")  # Output goes to both console and file
Hello World
>>> sys.stdout = original_stdout
>>> f.close()
files: tuple[IO[Any], ...]

File-like objects to write to

strip_colors: bool

Whether to strip ANSI color codes from output sent to non-stdout/stderr files

ascii_only: bool

Whether to replace non-ASCII characters with their ASCII equivalents for non-stdout/stderr files

ignore_lineup: bool

Whether to ignore lines containing LINE_UP escape sequence in non-terminal outputs

property encoding: str

Get the encoding of the first file, or “utf-8” as fallback.

Returns:

The encoding, ex: “utf-8”, “ascii”, “latin1”, etc.

Return type:

str

write(obj: str) None[source]

Write the object to all files while stripping colors if needed.

Parameters:

obj (str) – String to write

flush() None[source]

Flush all files.

fileno() int[source]

Return the file descriptor of the first file.