Upload text_utils.py with huggingface_hub
Browse files11234567891057 unmodified lines68697068697172737472757677787977788079818283848586858687888988909091929294939495979896971001011029899104100101102103104105106107108109110111import loggingimport reimport shutil
from .logging_utils import get_logger
logger = get_logger()
def split_words(s): """Splits a string into words based on PascalCase, camelCase, snake_case, kebab-case, and numbers attached to strings.57 unmodified lines return s.lower()
def print_dict(d, indent=0, indent_delta=4, max_chars=None): """Prints a dictionary in a formatted manner, taking into account the terminal width.def construct_dict_str(d, indent=0, indent_delta=4, max_chars=None): """Constructs a formatted string of a dictionary.
Args: d (dict): The dictionary to be printed. d (dict): The dictionary to be formatted. indent (int, optional): The current level of indentation. Defaults to 0. indent_delta (int, optional): The amount of spaces to add for each level of indentation. Defaults to 4. max_chars (int, optional): The maximum number of characters for each line. Defaults to terminal width - 10. """ max_chars = ( max_chars or shutil.get_terminal_size()[0] - 10 max_chars = max_chars or shutil.get_terminal_size()[0] - 10 ) # Get terminal size if max_chars not set indent_str = " " * indent indent_delta_str = " " * indent_delta res = ""
for key, value in d.items(): if isinstance(value, dict): logging.info(f"{indent_str}{key}:") print_dict(value, indent=indent + indent_delta, max_chars=max_chars) res += f"{indent_str}{key}:\n" res += construct_dict_str(value, indent + indent_delta, max_chars=max_chars) else: # Value is not a dict, print as a string str_value = str(value)
line_width = max_chars - indent # Split value by newline characters and handle each line separately lines = str_value.split("\n") logging.info(f"{indent_str}{key} ({type(value).__name__}):") res += f"{indent_str}{key} ({type(value).__name__}):\n" for line in lines: if len(line) + len(indent_str) + indent_delta > line_width: # Split long lines into multiple lines logging.info(f"{indent_str}{indent_delta_str}{line[:line_width]}") res += f"{indent_str}{indent_delta_str}{line[:line_width]}\n" for i in range(line_width, len(line), line_width): logging.info( f"{indent_str}{indent_delta_str}{line[i:i+line_width]}" ) res += f"{indent_str}{indent_delta_str}{line[i:i+line_width]}\n" else: logging.info(f"{indent_str}{indent_delta_str}{line}") res += f"{indent_str}{indent_delta_str}{line}\n" key = "" # Empty the key for lines after the first one return res
def print_dict(d, indent=0, indent_delta=4, max_chars=None): dict_str = construct_dict_str(d, indent, indent_delta, max_chars) dict_str = "\n" + dict_str logger.info(dict_str)
def nested_tuple_to_string(nested_tuple: tuple) -> str:text_utils.py+22-19