Upload text_utils.py with huggingface_hub
Browse files- text_utils.py +22 -19
text_utils.py
CHANGED
|
@@ -1,7 +1,10 @@
|
|
| 1 |
-
import logging
|
| 2 |
import re
|
| 3 |
import shutil
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def split_words(s):
|
| 7 |
"""Splits a string into words based on PascalCase, camelCase, snake_case, kebab-case, and numbers attached to strings.
|
|
@@ -65,44 +68,44 @@ def camel_to_snake_case(s):
|
|
| 65 |
return s.lower()
|
| 66 |
|
| 67 |
|
| 68 |
-
def
|
| 69 |
-
"""
|
| 70 |
|
| 71 |
Args:
|
| 72 |
-
d (dict): The dictionary to be
|
| 73 |
indent (int, optional): The current level of indentation. Defaults to 0.
|
| 74 |
indent_delta (int, optional): The amount of spaces to add for each level of indentation. Defaults to 4.
|
| 75 |
max_chars (int, optional): The maximum number of characters for each line. Defaults to terminal width - 10.
|
| 76 |
"""
|
| 77 |
-
max_chars = (
|
| 78 |
-
max_chars or shutil.get_terminal_size()[0] - 10
|
| 79 |
-
) # Get terminal size if max_chars not set
|
| 80 |
indent_str = " " * indent
|
| 81 |
indent_delta_str = " " * indent_delta
|
|
|
|
| 82 |
|
| 83 |
for key, value in d.items():
|
| 84 |
if isinstance(value, dict):
|
| 85 |
-
|
| 86 |
-
|
| 87 |
else:
|
| 88 |
-
# Value is not a dict, print as a string
|
| 89 |
str_value = str(value)
|
| 90 |
-
|
| 91 |
line_width = max_chars - indent
|
| 92 |
-
# Split value by newline characters and handle each line separately
|
| 93 |
lines = str_value.split("\n")
|
| 94 |
-
|
| 95 |
for line in lines:
|
| 96 |
if len(line) + len(indent_str) + indent_delta > line_width:
|
| 97 |
-
|
| 98 |
-
logging.info(f"{indent_str}{indent_delta_str}{line[:line_width]}")
|
| 99 |
for i in range(line_width, len(line), line_width):
|
| 100 |
-
|
| 101 |
-
f"{indent_str}{indent_delta_str}{line[i:i+line_width]}"
|
| 102 |
-
)
|
| 103 |
else:
|
| 104 |
-
|
| 105 |
key = "" # Empty the key for lines after the first one
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
|
| 108 |
def nested_tuple_to_string(nested_tuple: tuple) -> str:
|
|
|
|
|
|
|
| 1 |
import re
|
| 2 |
import shutil
|
| 3 |
|
| 4 |
+
from .logging_utils import get_logger
|
| 5 |
+
|
| 6 |
+
logger = get_logger()
|
| 7 |
+
|
| 8 |
|
| 9 |
def split_words(s):
|
| 10 |
"""Splits a string into words based on PascalCase, camelCase, snake_case, kebab-case, and numbers attached to strings.
|
|
|
|
| 68 |
return s.lower()
|
| 69 |
|
| 70 |
|
| 71 |
+
def construct_dict_str(d, indent=0, indent_delta=4, max_chars=None):
|
| 72 |
+
"""Constructs a formatted string of a dictionary.
|
| 73 |
|
| 74 |
Args:
|
| 75 |
+
d (dict): The dictionary to be formatted.
|
| 76 |
indent (int, optional): The current level of indentation. Defaults to 0.
|
| 77 |
indent_delta (int, optional): The amount of spaces to add for each level of indentation. Defaults to 4.
|
| 78 |
max_chars (int, optional): The maximum number of characters for each line. Defaults to terminal width - 10.
|
| 79 |
"""
|
| 80 |
+
max_chars = max_chars or shutil.get_terminal_size()[0] - 10
|
|
|
|
|
|
|
| 81 |
indent_str = " " * indent
|
| 82 |
indent_delta_str = " " * indent_delta
|
| 83 |
+
res = ""
|
| 84 |
|
| 85 |
for key, value in d.items():
|
| 86 |
if isinstance(value, dict):
|
| 87 |
+
res += f"{indent_str}{key}:\n"
|
| 88 |
+
res += construct_dict_str(value, indent + indent_delta, max_chars=max_chars)
|
| 89 |
else:
|
|
|
|
| 90 |
str_value = str(value)
|
|
|
|
| 91 |
line_width = max_chars - indent
|
|
|
|
| 92 |
lines = str_value.split("\n")
|
| 93 |
+
res += f"{indent_str}{key} ({type(value).__name__}):\n"
|
| 94 |
for line in lines:
|
| 95 |
if len(line) + len(indent_str) + indent_delta > line_width:
|
| 96 |
+
res += f"{indent_str}{indent_delta_str}{line[:line_width]}\n"
|
|
|
|
| 97 |
for i in range(line_width, len(line), line_width):
|
| 98 |
+
res += f"{indent_str}{indent_delta_str}{line[i:i+line_width]}\n"
|
|
|
|
|
|
|
| 99 |
else:
|
| 100 |
+
res += f"{indent_str}{indent_delta_str}{line}\n"
|
| 101 |
key = "" # Empty the key for lines after the first one
|
| 102 |
+
return res
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
def print_dict(d, indent=0, indent_delta=4, max_chars=None):
|
| 106 |
+
dict_str = construct_dict_str(d, indent, indent_delta, max_chars)
|
| 107 |
+
dict_str = "\n" + dict_str
|
| 108 |
+
logger.info(dict_str)
|
| 109 |
|
| 110 |
|
| 111 |
def nested_tuple_to_string(nested_tuple: tuple) -> str:
|