Skip to content

Commit

Permalink
Merge pull request #9171 from khoaguin/asset-repr-displays-entire-list
Browse files Browse the repository at this point in the history
Repr truncation for asset's primitive types (list, strings, set, dict...)
  • Loading branch information
khoaguin authored Aug 16, 2024
2 parents a0de849 + aa5629d commit 909b61d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
14 changes: 11 additions & 3 deletions packages/syft/src/syft/service/dataset/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from ...util.misc_objs import MarkdownDescription
from ...util.notebook_ui.icons import Icon
from ...util.table import itable_template_from_df
from ...util.util import repr_truncation
from ..action.action_data_empty import ActionDataEmpty
from ..action.action_object import ActionObject
from ..data_subject.data_subject import DataSubject
Expand Down Expand Up @@ -135,19 +136,26 @@ def _repr_html_(self) -> Any:
if isinstance(private_data_obj, ActionObject):
df = pd.DataFrame(self.data.syft_action_data)
data_table_line = itable_template_from_df(df=private_data_obj.head(5))

elif isinstance(private_data_obj, pd.DataFrame):
data_table_line = itable_template_from_df(df=private_data_obj.head(5))
else:
data_table_line = private_data_res.ok_value
try:
data_table_line = repr_truncation(private_data_obj)
except Exception as e:
logger.debug(f"Failed to truncate private data repr. {e}")
data_table_line = private_data_res.ok_value

if isinstance(self.mock, ActionObject):
df = pd.DataFrame(self.mock.syft_action_data)
mock_table_line = itable_template_from_df(df=df.head(5))
elif isinstance(self.mock, pd.DataFrame):
mock_table_line = itable_template_from_df(df=self.mock.head(5))
else:
mock_table_line = self.mock
try:
mock_table_line = repr_truncation(self.mock)
except Exception as e:
logger.debug(f"Failed to truncate mock data repr. {e}")
mock_table_line = self.mock
if isinstance(mock_table_line, SyftError):
mock_table_line = mock_table_line.message

Expand Down
29 changes: 29 additions & 0 deletions packages/syft/src/syft/util/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import platform
import random
import re
import reprlib
import secrets
from secrets import randbelow
import socket
Expand Down Expand Up @@ -1082,3 +1083,31 @@ def get_nb_secrets(defaults: dict | None = None) -> dict:
print(f"Unable to load {filename}")

return defaults


class CustomRepr(reprlib.Repr):
def repr_str(self, obj: Any, level: int = 0) -> str:
if len(obj) <= self.maxstring:
return repr(obj)
return repr(obj[: self.maxstring] + "...")


def repr_truncation(obj: Any, max_elements: int = 10) -> str:
"""
Return a truncated string representation of the object if it is too long.
Args:
- obj: The object to be represented (can be str, list, dict, set...).
- max_elements: Maximum number of elements to display before truncating.
Returns:
- A string representation of the object, truncated if necessary.
"""
r = CustomRepr()
r.maxlist = max_elements # For lists
r.maxdict = max_elements # For dictionaries
r.maxset = max_elements # For sets
r.maxstring = 100 # For strings
r.maxother = 100 # For other objects

return r.repr(obj)

0 comments on commit 909b61d

Please sign in to comment.