diff --git a/icechunk-python/python/icechunk/display.py b/icechunk-python/python/icechunk/display.py new file mode 100644 index 000000000..c3d53fe75 --- /dev/null +++ b/icechunk-python/python/icechunk/display.py @@ -0,0 +1,73 @@ +def dataclass_repr( + obj: object, + # make this default to inspecting the cls? + cls_name: str, + attributes: list[str] = None, + # TODO optional indent +) -> str: + """ + Dynamically create a repr for this dataclass-like object. + + Parameters + ---------- + obj : object + Object for which to make a repr. + cls_name : Type + What to display as the name of the class, including submodule. + attributes : list[str] | None + Names of attributes or properties to display the values of. + These must all exist on the instance and be printable. + + Returns + ------- + str + Repr for the class. + """ + header = f"<{cls_name}>" + + if not attributes: + return header + else: + contents = [] + for attr_name in attributes: + line = f"{attr_name}: {getattr(obj, attr_name)}" + contents.append(line) + return "\n".join([header] + contents) + + + +def dataclass_html_repr( + obj: object, + # make this default to inspecting the cls? + cls_name: str, + attributes: list[str] | None = None, + # TODO optional indent +) -> str: + """ + Dynamically create a repr for this dataclass-like object. + + Parameters + ---------- + obj : object + Object for which to make a repr. + cls_name : Type + What to display as the name of the class, including submodule. + attributes : list[str] | None + Names of attributes or properties to display the values of. + These must all exist on the instance and be printable. + + Returns + ------- + str + Repr for the class. + """ + header = f"<{cls_name}>" + + if not attributes: + return header + else: + contents = [] + for attr_name in attributes: + line = f"{attr_name}: {getattr(obj, attr_name)}" + contents.append(line) + return "\n".join([header] + contents) diff --git a/icechunk-python/python/icechunk/session.py b/icechunk-python/python/icechunk/session.py index 87d4acb00..1745819d7 100644 --- a/icechunk-python/python/icechunk/session.py +++ b/icechunk-python/python/icechunk/session.py @@ -10,6 +10,7 @@ ) from icechunk._icechunk_python import PySession from icechunk.store import IcechunkStore +import icechunk.display as display class Session: @@ -22,6 +23,21 @@ def __init__(self, session: PySession): self._session = session self._allow_changes = False + def __repr__(self) -> str: + mutable_attributes=[ + "read_only", + "snapshot_id", + ] + + if not self.read_only: + mutable_attributes += ["branch", "has_uncommitted_changes"] + + return display.dataclass_repr( + obj=self, + cls_name="icechunk.Session", + attributes=mutable_attributes, + ) + def __eq__(self, value: object) -> bool: if not isinstance(value, Session): return False