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_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)
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_important(text: str) -> None: 90 """ 91 Print important text with a specific color. 92 93 Args: 94 text (str): The important text to print. 95 """ 96 print_with_color(text, util_color.IMPORTANT)
Print important text with a specific color.
Args: text (str): The important text to print.
114def print_test_section(title: str) -> None: 115 """ 116 Print a test section with a specific color. 117 118 Args: 119 title (str): The title of the test section. 120 """ 121 global test_section_id 122 _print_section(title, util_color.TEST_SECTION_COLOR, test_section_id) 123 test_section_id += 1
Print a test section with a specific color.
Args: title (str): The title of the test section.
129def print_section( 130 title: str, color: str = util_color.SECTION_COLOR, _section_id: int | None = None 131) -> None: 132 """ 133 Print a section with a specific color and section ID. 134 135 Args: 136 title (str): The title of the section. 137 color (str): The color of the section. Default is util_color.SECTION_COLOR. 138 _section_id (int): The ID of the section. Default is None. 139 """ 140 global section_id 141 _section_id = _section_id if _section_id is not None else section_id 142 print_with_color(f"=== === ===\t[{section_id}] {title}\t=== === ===", color) 143 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.
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.
154def print_result(text: str) -> None: 155 """ 156 Print a result message with a specific color. 157 158 Args: 159 text (str): The result message to print. 160 """ 161 print_with_color(text, util_color.RESULT_COLOR)
Print a result message with a specific color.
Args: text (str): The result message to print.
164def print_warning(text: str) -> None: 165 """ 166 Print a warning message with a specific color. 167 168 Args: 169 text (str): The warning message to print. 170 """ 171 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.