stouputils.print.color_formatting module#

format_colored(*values: Any) str[source]#

Format text with Python 3.14 style colored formatting.

Dynamically colors text by analyzing each word: - File paths in magenta - Numbers in magenta - Function names (built-in and callable objects) in magenta - Exception names in bold magenta

Parameters:

values (Any) – Values to format (like the print function)

Returns:

The formatted text with ANSI color codes

Return type:

str

Examples

>>> # Test function names with parentheses
>>> result = format_colored("Call print() with 42 items")
>>> result.count(Cfg.MAGENTA)  # print and 42
2
>>> # Test function names without parentheses
>>> result = format_colored("Use len and sum functions")
>>> result.count(Cfg.MAGENTA)  # len and sum
2
>>> # Test exceptions (bold magenta)
>>> result = format_colored("Got ValueError when parsing")
>>> result.count(Cfg.MAGENTA), result.count(Cfg.BOLD)  # ValueError in bold magenta
(1, 1)
>>> # Test file paths
>>> result = format_colored("Processing ./data.csv file")
>>> result.count(Cfg.MAGENTA)  # ./data.csv
1
>>> # Test file paths with quotes
>>> result = format_colored('File "/path/to/script.py" line 42')
>>> result.count(Cfg.MAGENTA)  # /path/to/script.py and 42
2
>>> # Test numbers
>>> result = format_colored("Found 100 items and 3.14 value, 3.0e+10 is big")
>>> result.count(Cfg.MAGENTA)  # 100 and 3.14
3
>>> # Test mixed content
>>> result = format_colored("Call sum() got IndexError at line 256 in utils.py")
>>> result.count(Cfg.MAGENTA)  # sum, IndexError (bold), and 256
3
>>> result.count(Cfg.BOLD)  # IndexError is bold
1
>>> # Test keywords always colored
>>> result = format_colored("Check class dtype type")
>>> result.count(Cfg.MAGENTA)  # class, dtype, type
3
>>> # Test plain text (no coloring)
>>> result = format_colored("This is plain text")
>>> result.count(Cfg.MAGENTA) == 0 and result == "This is plain text"
True
>>> # Affix punctuation should not be colored (assert exact coloring, punctuation uncolored)
>>> result = format_colored("<class")
>>> result == "<" + Cfg.MAGENTA + "class" + Cfg.RESET
True
>>> result = format_colored("(dtype:")
>>> result == "(" + Cfg.MAGENTA + "dtype" + Cfg.RESET + ":"
True
>>> result = format_colored("[1.")
>>> result == "[" + Cfg.MAGENTA + "1" + Cfg.RESET + "."
True
>>> # Test complex
>>> text = "<class 'numpy.ndarray'>, <id 140357548266896>: (dtype: float32, shape: (6,), min: 0.0, max: 1.0) [1. 0. 0. 0. 1. 0.]"
>>> result = format_colored(text)
>>> result.count(Cfg.MAGENTA)  # class, numpy, ndarray, float32, 6, 0.0, 1.0, 1. 0.
16
colored(
*values: Any,
file: TextIO | None = None,
**print_kwargs: Any,
) None[source]#

Print with Python 3.14 style colored formatting.

Dynamically colors text by analyzing each word: - File paths in magenta - Numbers in magenta - Function names (built-in and callable objects) in magenta - Exception names in bold magenta

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

  • file (TextIO) – File to write the message to (default: sys.stdout)

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

Examples

>>> colored("File '/path/to/file.py', line 42, in function_name")
>>> colored("KeyboardInterrupt")
>>> colored("Processing data.csv with 100 items")
>>> colored("Using print and len functions")