lkj 0.1.48__tar.gz → 0.1.49__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {lkj-0.1.48 → lkj-0.1.49}/PKG-INFO +1 -1
- {lkj-0.1.48 → lkj-0.1.49}/lkj/dicts.py +25 -6
- {lkj-0.1.48 → lkj-0.1.49}/lkj.egg-info/PKG-INFO +1 -1
- {lkj-0.1.48 → lkj-0.1.49}/setup.cfg +1 -1
- {lkj-0.1.48 → lkj-0.1.49}/LICENSE +0 -0
- {lkj-0.1.48 → lkj-0.1.49}/README.md +0 -0
- {lkj-0.1.48 → lkj-0.1.49}/lkj/__init__.py +0 -0
- {lkj-0.1.48 → lkj-0.1.49}/lkj/chunking.py +0 -0
- {lkj-0.1.48 → lkj-0.1.49}/lkj/filesys.py +0 -0
- {lkj-0.1.48 → lkj-0.1.49}/lkj/funcs.py +0 -0
- {lkj-0.1.48 → lkj-0.1.49}/lkj/importing.py +0 -0
- {lkj-0.1.48 → lkj-0.1.49}/lkj/iterables.py +0 -0
- {lkj-0.1.48 → lkj-0.1.49}/lkj/loggers.py +0 -0
- {lkj-0.1.48 → lkj-0.1.49}/lkj/misc.py +0 -0
- {lkj-0.1.48 → lkj-0.1.49}/lkj/strings.py +0 -0
- {lkj-0.1.48 → lkj-0.1.49}/lkj/tests/__init__.py +0 -0
- {lkj-0.1.48 → lkj-0.1.49}/lkj/tests/test_strings.py +0 -0
- {lkj-0.1.48 → lkj-0.1.49}/lkj.egg-info/SOURCES.txt +0 -0
- {lkj-0.1.48 → lkj-0.1.49}/lkj.egg-info/dependency_links.txt +0 -0
- {lkj-0.1.48 → lkj-0.1.49}/lkj.egg-info/not-zip-safe +0 -0
- {lkj-0.1.48 → lkj-0.1.49}/lkj.egg-info/requires.txt +0 -0
- {lkj-0.1.48 → lkj-0.1.49}/lkj.egg-info/top_level.txt +0 -0
- {lkj-0.1.48 → lkj-0.1.49}/setup.py +0 -0
|
@@ -48,17 +48,21 @@ def truncate_dict_values(
|
|
|
48
48
|
max_list_size: int | None = 2,
|
|
49
49
|
max_string_size: int | None = 66,
|
|
50
50
|
middle_marker: str = "...",
|
|
51
|
+
d_ingress: callable = lambda d: d,
|
|
51
52
|
) -> dict:
|
|
52
53
|
"""
|
|
53
54
|
Returns a new dictionary with the same nested keys structure, where:
|
|
54
55
|
- List values are reduced to a maximum size of max_list_size.
|
|
55
56
|
- String values longer than max_string_size are truncated in the middle.
|
|
57
|
+
- Values are preprocessed with d_ingress before type checking.
|
|
56
58
|
|
|
57
59
|
Parameters:
|
|
58
60
|
d (dict): The input dictionary.
|
|
59
61
|
max_list_size (int, optional): Maximum size for lists. Defaults to 2.
|
|
60
62
|
max_string_size (int, optional): Maximum length for strings. Defaults to None (no truncation).
|
|
61
63
|
middle_marker (str, optional): String to insert in the middle of truncated strings. Defaults to '...'.
|
|
64
|
+
d_ingress (callable, optional): Function applied to each value before type checking.
|
|
65
|
+
Defaults to identity function. Useful for handling special types like pandas Series.
|
|
62
66
|
|
|
63
67
|
Returns:
|
|
64
68
|
dict: A new dictionary with truncated lists and strings.
|
|
@@ -79,6 +83,14 @@ def truncate_dict_values(
|
|
|
79
83
|
... == large_dict
|
|
80
84
|
... )
|
|
81
85
|
|
|
86
|
+
For handling special types like pandas Series:
|
|
87
|
+
|
|
88
|
+
>>> def handle_pandas(obj):
|
|
89
|
+
... if hasattr(obj, 'head'): # pandas Series/DataFrame
|
|
90
|
+
... return f"<{type(obj).__name__}: {len(obj)} items>"
|
|
91
|
+
... return obj
|
|
92
|
+
>>> # truncate_dict_values(data_with_pandas, d_ingress=handle_pandas)
|
|
93
|
+
|
|
82
94
|
"""
|
|
83
95
|
|
|
84
96
|
def truncate_string(value, max_len, marker):
|
|
@@ -87,19 +99,26 @@ def truncate_dict_values(
|
|
|
87
99
|
half_len = (max_len - len(marker)) // 2
|
|
88
100
|
return value[:half_len] + marker + value[-half_len:]
|
|
89
101
|
|
|
102
|
+
# Apply d_ingress to preprocess the value
|
|
103
|
+
d = d_ingress(d)
|
|
104
|
+
|
|
90
105
|
kwargs = dict(
|
|
91
106
|
max_list_size=max_list_size,
|
|
92
107
|
max_string_size=max_string_size,
|
|
93
108
|
middle_marker=middle_marker,
|
|
109
|
+
d_ingress=d_ingress,
|
|
94
110
|
)
|
|
95
111
|
if isinstance(d, dict):
|
|
96
112
|
return {k: truncate_dict_values(v, **kwargs) for k, v in d.items()}
|
|
97
|
-
elif isinstance(d, list):
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
113
|
+
elif isinstance(d, (list, tuple)):
|
|
114
|
+
container_type = type(d)
|
|
115
|
+
if max_list_size is not None:
|
|
116
|
+
truncated_items = [
|
|
117
|
+
truncate_dict_values(v, **kwargs) for v in d[:max_list_size]
|
|
118
|
+
]
|
|
119
|
+
else:
|
|
120
|
+
truncated_items = [truncate_dict_values(v, **kwargs) for v in d]
|
|
121
|
+
return container_type(truncated_items)
|
|
103
122
|
elif isinstance(d, str):
|
|
104
123
|
return truncate_string(d, max_string_size, middle_marker)
|
|
105
124
|
else:
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|