This is an automated email from the ASF dual-hosted git repository.

fokko pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/iceberg.git


The following commit(s) were added to refs/heads/master by this push:
     new bc71976fa2 Python: Add `__repr__` method to Table class (#8447)
bc71976fa2 is described below

commit bc71976fa22b5432c1eb8b04ccef3a494299e1ce
Author: Tai Le <[email protected]>
AuthorDate: Fri Sep 22 18:09:55 2023 +0700

    Python: Add `__repr__` method to Table class (#8447)
    
    * 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]>
---
 python/pyiceberg/table/__init__.py | 10 ++++++++++
 python/tests/table/test_init.py    | 12 ++++++++++++
 2 files changed, 22 insertions(+)

diff --git a/python/pyiceberg/table/__init__.py 
b/python/pyiceberg/table/__init__.py
index b905c955c8..8443315a64 100644
--- a/python/pyiceberg/table/__init__.py
+++ b/python/pyiceberg/table/__init__.py
@@ -541,6 +541,16 @@ class Table:
             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)."""
diff --git a/python/tests/table/test_init.py b/python/tests/table/test_init.py
index 3ee0cd37f2..8fd5e2bcdb 100644
--- a/python/tests/table/test_init.py
+++ b/python/tests/table/test_init.py
@@ -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),

Reply via email to