Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions icechunk-python/python/icechunk/display.py
Original file line number Diff line number Diff line change
@@ -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)
16 changes: 16 additions & 0 deletions icechunk-python/python/icechunk/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
)
from icechunk._icechunk_python import PySession
from icechunk.store import IcechunkStore
import icechunk.display as display


class Session:
Expand All @@ -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
Expand Down
Loading