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.
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_highlighted(text: str) -> None: 89 """ 90 Print text with a highlighted (stand-out) color. 91 92 Args: 93 text (str): The text to print. 94 """ 95 print_with_color(text, util_color.HIGHLIGHTED) 96 97 98def print_important(text: str) -> None: 99 """ 100 Print important text with a specific color. 101 102 Args: 103 text (str): The important text to print. 104 """ 105 print_with_color(text, util_color.IMPORTANT) 106 107 108def _print_section(title: str, color: str, section_id: int) -> None: 109 """ 110 Print a section title with a specific color and section ID. 111 112 Args: 113 title (str): The title of the section. 114 color (str): The color of the section. 115 section_id (int): The ID of the section. 116 """ 117 print_with_color(f"=== === ===\t[{section_id}] {title}\t=== === ===", color) 118 119 120test_section_id = 1 121 122 123def print_test_section(title: str) -> None: 124 """ 125 Print a test section with a specific color. 126 127 Args: 128 title (str): The title of the test section. 129 """ 130 global test_section_id 131 _print_section(title, util_color.TEST_SECTION_COLOR, test_section_id) 132 test_section_id += 1 133 134 135section_id = 1 136 137 138def print_section( 139 title: str, color: str = util_color.SECTION_COLOR, _section_id: int | None = None 140) -> None: 141 """ 142 Print a section with a specific color and section ID. 143 144 Args: 145 title (str): The title of the section. 146 color (str): The color of the section. Default is util_color.SECTION_COLOR. 147 _section_id (int): The ID of the section. Default is None. 148 """ 149 global section_id 150 _section_id = _section_id if _section_id is not None else section_id 151 print_with_color(f"=== === ===\t[{section_id}] {title}\t=== === ===", color) 152 section_id += 1 153 154 155def reset_section_count() -> None: 156 """ 157 Reset the section count back to 1. 158 """ 159 global section_id 160 section_id = 1 161 162 163def print_result(text: str) -> None: 164 """ 165 Print a result message with a specific color. 166 167 Args: 168 text (str): The result message to print. 169 """ 170 print_with_color(text, util_color.RESULT_COLOR) 171 172 173def print_warning(text: str) -> None: 174 """ 175 Print a warning message with a specific color. 176 177 Args: 178 text (str): The warning message to print. 179 """ 180 print_with_color("WARNING: " + str(text), util_color.WARNING_COLOR)
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.
34def print_no_endline(text: str) -> None: 35 """ 36 Print text without a newline character and log the message. 37 38 Args: 39 text (str): The text to print. 40 """ 41 print(text, end="") 42 logger.info(text)
Print text without a newline character and log the message.
Args: text (str): The text to print.
45def print_custom(text: str) -> None: 46 """ 47 Print text with a newline character and log the message. 48 49 Args: 50 text (str): The text to print. 51 """ 52 print(text) 53 logger.info(text)
Print text with a newline character and log the message.
Args: text (str): The text to print.
56def print_custom_with_logger(text: str, given_logger: logging.Logger) -> None: 57 """ 58 Print text with a newline character using a specific logger. 59 60 Args: 61 text (str): The text to print. 62 given_logger: The logger to use. 63 """ 64 print(text) 65 given_logger.info(text)
Print text with a newline character using a specific logger.
Args: text (str): The text to print. given_logger: The logger to use.
68def print_with_color(text: str, color: str) -> None: 69 """ 70 Print text with a specified color. 71 72 Args: 73 text (str): The text to print. 74 color (str): The color to apply. 75 """ 76 print_custom(util_color.colorize(text, color))
Print text with a specified color.
Args: text (str): The text to print. color (str): The color to apply.
79def print_error(message: str) -> None: 80 """ 81 Print an error message with a specific color. 82 83 Args: 84 message (str): The error message to print. 85 """ 86 print_with_color(message, util_color.ERROR_COLOR)
Print an error message with a specific color.
Args: message (str): The error message to print.
89def print_highlighted(text: str) -> None: 90 """ 91 Print text with a highlighted (stand-out) color. 92 93 Args: 94 text (str): The text to print. 95 """ 96 print_with_color(text, util_color.HIGHLIGHTED)
Print text with a highlighted (stand-out) color.
Args: text (str): The text to print.
99def print_important(text: str) -> None: 100 """ 101 Print important text with a specific color. 102 103 Args: 104 text (str): The important text to print. 105 """ 106 print_with_color(text, util_color.IMPORTANT)
Print important text with a specific color.
Args: text (str): The important text to print.
124def print_test_section(title: str) -> None: 125 """ 126 Print a test section with a specific color. 127 128 Args: 129 title (str): The title of the test section. 130 """ 131 global test_section_id 132 _print_section(title, util_color.TEST_SECTION_COLOR, test_section_id) 133 test_section_id += 1
Print a test section with a specific color.
Args: title (str): The title of the test section.
139def print_section( 140 title: str, color: str = util_color.SECTION_COLOR, _section_id: int | None = None 141) -> None: 142 """ 143 Print a section with a specific color and section ID. 144 145 Args: 146 title (str): The title of the section. 147 color (str): The color of the section. Default is util_color.SECTION_COLOR. 148 _section_id (int): The ID of the section. Default is None. 149 """ 150 global section_id 151 _section_id = _section_id if _section_id is not None else section_id 152 print_with_color(f"=== === ===\t[{section_id}] {title}\t=== === ===", color) 153 section_id += 1
Print a section with a specific color and section ID.
Args: title (str): The title of the section. color (str): The color of the section. Default is util_color.SECTION_COLOR. _section_id (int): The ID of the section. Default is None.
156def reset_section_count() -> None: 157 """ 158 Reset the section count back to 1. 159 """ 160 global section_id 161 section_id = 1
Reset the section count back to 1.
164def print_result(text: str) -> None: 165 """ 166 Print a result message with a specific color. 167 168 Args: 169 text (str): The result message to print. 170 """ 171 print_with_color(text, util_color.RESULT_COLOR)
Print a result message with a specific color.
Args: text (str): The result message to print.
174def print_warning(text: str) -> None: 175 """ 176 Print a warning message with a specific color. 177 178 Args: 179 text (str): The warning message to print. 180 """ 181 print_with_color("WARNING: " + str(text), util_color.WARNING_COLOR)
Print a warning message with a specific color.
Args: text (str): The warning message to print.