Skip to content

Commit

Permalink
Python: Add __repr__ method to Table class (#8447)
Browse files Browse the repository at this point in the history
* Python: Add __repr__ method to Table class

* update test for __repr__ of Table class

* add sort_order to table repr

---------

Co-authored-by: Thi Cam Tu Phan <[email protected]>
  • Loading branch information
tlm365 and KaiPhan authored Sep 22, 2023
1 parent 37d4890 commit bc71976
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions python/pyiceberg/table/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,16 @@ def __eq__(self, other: Any) -> bool:
else False
)

def __repr__(self) -> str:
"""Return the string representation of the Table class."""
table_name = self.catalog.table_name_from(self.identifier)
schema_str = ",\n ".join(str(column) for column in self.schema().columns if self.schema())
partition_str = f"partition by: [{', '.join(field.name for field in self.spec().fields if self.spec())}]"
sort_order_str = f"sort order: [{', '.join(str(field) for field in self.sort_order().fields if self.sort_order())}]"
snapshot_str = f"snapshot: {str(self.current_snapshot()) if self.current_snapshot() else 'null'}"
result_str = f"{table_name}(\n {schema_str}\n),\n{partition_str},\n{sort_order_str},\n{snapshot_str}"
return result_str


class StaticTable(Table):
"""Load a table directly from a metadata file (i.e., without using a catalog)."""
Expand Down
12 changes: 12 additions & 0 deletions python/tests/table/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,18 @@ def test_snapshot_by_name_does_not_exist(table: Table) -> None:
assert table.snapshot_by_name("doesnotexist") is None


def test_repr(table: Table) -> None:
expected = """table(
1: x: required long,
2: y: required long (comment),
3: z: required long
),
partition by: [x],
sort order: [2 ASC NULLS FIRST, bucket[4](3) DESC NULLS LAST],
snapshot: Operation.APPEND: id=3055729675574597004, parent_id=3051729675574597004, schema_id=1"""
assert repr(table) == expected


def test_history(table: Table) -> None:
assert table.history() == [
SnapshotLogEntry(snapshot_id=3051729675574597004, timestamp_ms=1515100955770),
Expand Down

0 comments on commit bc71976

Please sign in to comment.