Upload text_utils.py with huggingface_hub
Browse files123456757 unmodified lines6566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108import loggingimport reimport shutildef split_words(s):"""Splits a string into words based on PascalCase, camelCase, snake_case, kebab-case, and numbers attached to strings.57 unmodified linesreturn 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.Args:d (dict): The dictionary to be printed.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) # Get terminal size if max_chars not setindent_str = " " * indentindent_delta_str = " " * indent_deltafor 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)else:# Value is not a dict, print as a stringstr_value = str(value)line_width = max_chars - indent# Split value by newline characters and handle each line separatelylines = str_value.split("\n")logging.info(f"{indent_str}{key} ({type(value).__name__}):")for line in lines:if len(line) + len(indent_str) + indent_delta > line_width:# Split long lines into multiple lineslogging.info(f"{indent_str}{indent_delta_str}{line[:line_width]}")for i in range(line_width, len(line), line_width):logging.info(f"{indent_str}{indent_delta_str}{line[i:i+line_width]}")else:logging.info(f"{indent_str}{indent_delta_str}{line}")key = "" # Empty the key for lines after the first onedef nested_tuple_to_string(nested_tuple: tuple) -> str:1234567891057 unmodified lines6869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111import reimport shutilfrom .logging_utils import get_loggerlogger = 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 linesreturn s.lower()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 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] - 10indent_str = " " * indentindent_delta_str = " " * indent_deltares = ""for key, value in d.items():if isinstance(value, dict):res += f"{indent_str}{key}:\n"res += construct_dict_str(value, indent + indent_delta, max_chars=max_chars)else:str_value = str(value)line_width = max_chars - indentlines = str_value.split("\n")res += f"{indent_str}{key} ({type(value).__name__}):\n"for line in lines:if len(line) + len(indent_str) + indent_delta > line_width:res += f"{indent_str}{indent_delta_str}{line[:line_width]}\n"for i in range(line_width, len(line), line_width):res += f"{indent_str}{indent_delta_str}{line[i:i+line_width]}\n"else:res += f"{indent_str}{indent_delta_str}{line}\n"key = "" # Empty the key for lines after the first onereturn resdef 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_strlogger.info(dict_str)def nested_tuple_to_string(nested_tuple: tuple) -> str:
text_utils.py+22-19