lkj 0.1.48__py3-none-any.whl → 0.1.49__py3-none-any.whl

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/dicts.py CHANGED
@@ -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
- return (
99
- [truncate_dict_values(v, **kwargs) for v in d[:max_list_size]]
100
- if max_list_size is not None
101
- else d
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:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: lkj
3
- Version: 0.1.48
3
+ Version: 0.1.49
4
4
  Summary: A dump of homeless useful utils
5
5
  Home-page: https://github.com/thorwhalen/lkj
6
6
  Author: Thor Whalen
@@ -1,6 +1,6 @@
1
1
  lkj/__init__.py,sha256=z8IZi5ql-bCM0xRgcEERg48oi2SRkgX9qSZdyaKb62Y,7300
2
2
  lkj/chunking.py,sha256=K7yakDuTgyn6t2rvFeDIWS5pO636BKGOe1spKk7v4Mc,3746
3
- lkj/dicts.py,sha256=0Ow8AgMHx2Og7hAAkBjFd3f2YTBhRbSvZ9fO-xkGkpU,10430
3
+ lkj/dicts.py,sha256=67E9qwxPLLsBeRj67cSDgkAGvB6xfmNsi8igXdFGwjA,11307
4
4
  lkj/filesys.py,sha256=hYWztDoJUvVcPMD5AxRyYKkTB-sy5hx6_RDw4OyRcos,14628
5
5
  lkj/funcs.py,sha256=LXJlj3gMMsbD0t2gn2NZZ6mOqmW5bxM-94uGoYgrhzI,8930
6
6
  lkj/importing.py,sha256=TcW3qUDmw7jqswpxXnksjlHkkbOJq70NbUk1ZyaafT0,6658
@@ -10,8 +10,8 @@ lkj/misc.py,sha256=RqJg4HgcT1ZZRBtxOppwYibvvLIyn6pNqI-WMg0uKgI,1457
10
10
  lkj/strings.py,sha256=JukQ4pvLlwGajBEK0PXyqsumEFKm8i1rx6SgASz7tqo,43798
11
11
  lkj/tests/__init__.py,sha256=kReYfWiyz1T79AuZAy5m4PIBj3Oj_Dlc0E-T8HVQ504,20
12
12
  lkj/tests/test_strings.py,sha256=Ix54io8WqWmYTY4guBgZShtKabtjd5uVpdxzxwJkgNA,1707
13
- lkj-0.1.48.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
14
- lkj-0.1.48.dist-info/METADATA,sha256=qACdUbDx6cImMq9DmVvGqYiFFO2mxH3ImUwQfTGjY5U,8267
15
- lkj-0.1.48.dist-info/WHEEL,sha256=SmOxYU7pzNKBqASvQJ7DjX3XGUF92lrGhMb3R6_iiqI,91
16
- lkj-0.1.48.dist-info/top_level.txt,sha256=3DZOUwYmyurJFBXQCvCmEIVm8z2b42O5Sx3RDQyePfg,4
17
- lkj-0.1.48.dist-info/RECORD,,
13
+ lkj-0.1.49.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
14
+ lkj-0.1.49.dist-info/METADATA,sha256=pURwBZHm355AXQBzLViKArzBWbJhCFLxiNiHRaNRih8,8267
15
+ lkj-0.1.49.dist-info/WHEEL,sha256=SmOxYU7pzNKBqASvQJ7DjX3XGUF92lrGhMb3R6_iiqI,91
16
+ lkj-0.1.49.dist-info/top_level.txt,sha256=3DZOUwYmyurJFBXQCvCmEIVm8z2b42O5Sx3RDQyePfg,4
17
+ lkj-0.1.49.dist-info/RECORD,,
File without changes