Skip to content

Commit 909b61d

Browse files
authored
Merge pull request #9171 from khoaguin/asset-repr-displays-entire-list
Repr truncation for asset's primitive types (list, strings, set, dict...)
2 parents a0de849 + aa5629d commit 909b61d

2 files changed

Lines changed: 40 additions & 3 deletions

File tree

packages/syft/src/syft/service/dataset/dataset.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
from ...util.misc_objs import MarkdownDescription
3939
from ...util.notebook_ui.icons import Icon
4040
from ...util.table import itable_template_from_df
41+
from ...util.util import repr_truncation
4142
from ..action.action_data_empty import ActionDataEmpty
4243
from ..action.action_object import ActionObject
4344
from ..data_subject.data_subject import DataSubject
@@ -135,19 +136,26 @@ def _repr_html_(self) -> Any:
135136
if isinstance(private_data_obj, ActionObject):
136137
df = pd.DataFrame(self.data.syft_action_data)
137138
data_table_line = itable_template_from_df(df=private_data_obj.head(5))
138-
139139
elif isinstance(private_data_obj, pd.DataFrame):
140140
data_table_line = itable_template_from_df(df=private_data_obj.head(5))
141141
else:
142-
data_table_line = private_data_res.ok_value
142+
try:
143+
data_table_line = repr_truncation(private_data_obj)
144+
except Exception as e:
145+
logger.debug(f"Failed to truncate private data repr. {e}")
146+
data_table_line = private_data_res.ok_value
143147

144148
if isinstance(self.mock, ActionObject):
145149
df = pd.DataFrame(self.mock.syft_action_data)
146150
mock_table_line = itable_template_from_df(df=df.head(5))
147151
elif isinstance(self.mock, pd.DataFrame):
148152
mock_table_line = itable_template_from_df(df=self.mock.head(5))
149153
else:
150-
mock_table_line = self.mock
154+
try:
155+
mock_table_line = repr_truncation(self.mock)
156+
except Exception as e:
157+
logger.debug(f"Failed to truncate mock data repr. {e}")
158+
mock_table_line = self.mock
151159
if isinstance(mock_table_line, SyftError):
152160
mock_table_line = mock_table_line.message
153161

packages/syft/src/syft/util/util.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import platform
2828
import random
2929
import re
30+
import reprlib
3031
import secrets
3132
from secrets import randbelow
3233
import socket
@@ -1082,3 +1083,31 @@ def get_nb_secrets(defaults: dict | None = None) -> dict:
10821083
print(f"Unable to load {filename}")
10831084

10841085
return defaults
1086+
1087+
1088+
class CustomRepr(reprlib.Repr):
1089+
def repr_str(self, obj: Any, level: int = 0) -> str:
1090+
if len(obj) <= self.maxstring:
1091+
return repr(obj)
1092+
return repr(obj[: self.maxstring] + "...")
1093+
1094+
1095+
def repr_truncation(obj: Any, max_elements: int = 10) -> str:
1096+
"""
1097+
Return a truncated string representation of the object if it is too long.
1098+
1099+
Args:
1100+
- obj: The object to be represented (can be str, list, dict, set...).
1101+
- max_elements: Maximum number of elements to display before truncating.
1102+
1103+
Returns:
1104+
- A string representation of the object, truncated if necessary.
1105+
"""
1106+
r = CustomRepr()
1107+
r.maxlist = max_elements # For lists
1108+
r.maxdict = max_elements # For dictionaries
1109+
r.maxset = max_elements # For sets
1110+
r.maxstring = 100 # For strings
1111+
r.maxother = 100 # For other objects
1112+
1113+
return r.repr(obj)

0 commit comments

Comments
 (0)