cornsnake.util_print

A Single entry point for printing - so can add custom printing with color and logging.

Functions for printing with different colors, logging messages, printing sections, and handling warnings.

Documentation

  1"""
  2A Single entry point for printing - so can add custom printing with color and logging.
  3
  4Functions for printing with different colors, logging messages, printing sections, and handling warnings.
  5
  6[Documentation](http://docs.mrseanryan.cornsnake.s3-website-eu-west-1.amazonaws.com/cornsnake/util_print.html)
  7"""
  8
  9import logging
 10from . import util_color
 11from . import util_log
 12
 13logger = util_log.getLogger(__name__)
 14
 15
 16def percent(num: int, denom: int, ndigits: int = 0) -> str:
 17    """
 18    Calculate the percentage of two numbers.
 19
 20    Args:
 21    num (int): The numerator.
 22    denom (int): The denominator.
 23    ndigits (int): Number of digits after the decimal point. Default is 0.
 24
 25    Returns:
 26    str: The percentage as a string.
 27    """
 28    if denom == 0:
 29        return format(0, f".{ndigits}f")
 30    return str(round((num * 100.0) / denom, ndigits)) + "%"
 31
 32
 33def print_no_endline(text: str) -> None:
 34    """
 35    Print text without a newline character and log the message.
 36
 37    Args:
 38    text (str): The text to print.
 39    """
 40    print(text, end="")
 41    logger.info(text)
 42
 43
 44def print_custom(text: str) -> None:
 45    """
 46    Print text with a newline character and log the message.
 47
 48    Args:
 49    text (str): The text to print.
 50    """
 51    print(text)
 52    logger.info(text)
 53
 54
 55def print_custom_with_logger(text: str, given_logger: logging.Logger) -> None:
 56    """
 57    Print text with a newline character using a specific logger.
 58
 59    Args:
 60    text (str): The text to print.
 61    given_logger: The logger to use.
 62    """
 63    print(text)
 64    given_logger.info(text)
 65
 66
 67def print_with_color(text: str, color: str) -> None:
 68    """
 69    Print text with a specified color.
 70
 71    Args:
 72    text (str): The text to print.
 73    color (str): The color to apply.
 74    """
 75    print_custom(util_color.colorize(text, color))
 76
 77
 78def print_error(message: str) -> None:
 79    """
 80    Print an error message with a specific color.
 81
 82    Args:
 83    message (str): The error message to print.
 84    """
 85    print_with_color(message, util_color.ERROR_COLOR)
 86
 87
 88def print_important(text: str) -> None:
 89    """
 90    Print important text with a specific color.
 91
 92    Args:
 93    text (str): The important text to print.
 94    """
 95    print_with_color(text, util_color.IMPORTANT)
 96
 97
 98def _print_section(title: str, color: str, section_id: int) -> None:
 99    """
100    Print a section title with a specific color and section ID.
101
102    Args:
103    title (str): The title of the section.
104    color (str): The color of the section.
105    section_id (int): The ID of the section.
106    """
107    print_with_color(f"=== === ===\t[{section_id}] {title}\t=== === ===", color)
108
109
110test_section_id = 1
111
112
113def print_test_section(title: str) -> None:
114    """
115    Print a test section with a specific color.
116
117    Args:
118    title (str): The title of the test section.
119    """
120    global test_section_id
121    _print_section(title, util_color.TEST_SECTION_COLOR, test_section_id)
122    test_section_id += 1
123
124
125section_id = 1
126
127
128def print_section(
129    title: str, color: str = util_color.SECTION_COLOR, _section_id: int | None = None
130) -> None:
131    """
132    Print a section with a specific color and section ID.
133
134    Args:
135    title (str): The title of the section.
136    color (str): The color of the section. Default is util_color.SECTION_COLOR.
137    _section_id (int): The ID of the section. Default is None.
138    """
139    global section_id
140    _section_id = _section_id if _section_id is not None else section_id
141    print_with_color(f"=== === ===\t[{section_id}] {title}\t=== === ===", color)
142    section_id += 1
143
144
145def reset_section_count() -> None:
146    """
147    Reset the section count back to 1.
148    """
149    global section_id
150    section_id = 1
151
152
153def print_result(text: str) -> None:
154    """
155    Print a result message with a specific color.
156
157    Args:
158    text (str): The result message to print.
159    """
160    print_with_color(text, util_color.RESULT_COLOR)
161
162
163def print_warning(text: str) -> None:
164    """
165    Print a warning message with a specific color.
166
167    Args:
168    text (str): The warning message to print.
169    """
170    print_with_color("WARNING: " + str(text), util_color.WARNING_COLOR)
logger = <Logger cornsnake.util_print (DEBUG)>
def percent(num: int, denom: int, ndigits: int = 0) -> str:
17def percent(num: int, denom: int, ndigits: int = 0) -> str:
18    """
19    Calculate the percentage of two numbers.
20
21    Args:
22    num (int): The numerator.
23    denom (int): The denominator.
24    ndigits (int): Number of digits after the decimal point. Default is 0.
25
26    Returns:
27    str: The percentage as a string.
28    """
29    if denom == 0:
30        return format(0, f".{ndigits}f")
31    return str(round((num * 100.0) / denom, ndigits)) + "%"

Calculate the percentage of two numbers.

Args: num (int): The numerator. denom (int): The denominator. ndigits (int): Number of digits after the decimal point. Default is 0.

Returns: str: The percentage as a string.

test_section_id = 1
section_id = 1
def reset_section_count() -> None:
146def reset_section_count() -> None:
147    """
148    Reset the section count back to 1.
149    """
150    global section_id
151    section_id = 1

Reset the section count back to 1.