Skip to content

Commit 2cb4295

Browse files
committed
New Column.__repr__() for an nice overview of the column
1 parent fb66107 commit 2cb4295

3 files changed

Lines changed: 34 additions & 4 deletions

File tree

examples/ctable/indexing.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ def load_rows(table: blosc2.CTable, nrows: int = 240) -> None:
4141
packed = None
4242

4343
try:
44+
print("Creating a CTable with mixed dtypes...")
4445
pt = blosc2.CTable(Measurement, urlpath=str(table_path), mode="w")
4546
load_rows(pt)
4647

4748
# Create a couple of indexes on columns with different dtypes.
49+
print("\nCreating indexes...")
4850
idx_sensor = pt.create_index("sensor_id", kind=blosc2.IndexKind.FULL)
4951
idx_active = pt.create_index("active")
5052
print("Indexes created:", pt.indexes)
@@ -54,8 +56,8 @@ def load_rows(table: blosc2.CTable, nrows: int = 240) -> None:
5456
# Queries can combine indexed and non-indexed predicates.
5557
recent_active = pt.where((pt["sensor_id"] >= 180) & pt["active"] & (pt["region"] == "north"))
5658
print("\nLive rows with sensor_id >= 180, active=True, region='north':", len(recent_active))
57-
print("sensor_ids:", recent_active["sensor_id"].to_numpy().tolist())
58-
print("statuses:", recent_active["status"].to_numpy().tolist())
59+
print("sensor_ids:", recent_active["sensor_id"])
60+
print("statuses:", recent_active["status"].to_numpy())
5961

6062
# Close the table, pack the TreeStore into a single .b2z file, and reopen it.
6163
del pt
@@ -79,8 +81,8 @@ def load_rows(table: blosc2.CTable, nrows: int = 240) -> None:
7981
# Query directly against the .b2z bundle; no unpack step is needed.
8082
warm_active = packed.where(packed["active"] & (packed["status"] == "warm") & (packed["sensor_id"] > 100))
8183
print("\nRows from .b2z with active=True, status='warm', sensor_id > 100:", len(warm_active))
82-
print("sensor_ids:", warm_active["sensor_id"].to_numpy().tolist())
83-
print("regions:", warm_active["region"].to_numpy().tolist())
84+
print("sensor_ids:", warm_active["sensor_id"])
85+
print("regions:", warm_active["region"].to_numpy())
8486

8587
print("\nThe packed file is kept on disk.")
8688
print(f"Inspect it later with: f = blosc2.open({bundle_path.name!r}, mode='r')")

src/blosc2/ctable.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
import contextlib
1414
import dataclasses
15+
import itertools
1516
import os
1617
import pprint
1718
import shutil
@@ -475,6 +476,8 @@ def __getitem__(self, col_name: str):
475476

476477

477478
class Column:
479+
_REPR_PREVIEW_ITEMS = 8
480+
478481
def __init__(self, table: CTable, col_name: str, mask=None):
479482
self._table = table
480483
self._col_name = col_name
@@ -597,6 +600,21 @@ def __iter__(self):
597600
data_chunk = self._raw_col[chunk_start : chunk_start + actual_size]
598601
yield from data_chunk[mask_chunk]
599602

603+
def __repr__(self) -> str:
604+
preview_items = []
605+
for value in itertools.islice(self, self._REPR_PREVIEW_ITEMS + 1):
606+
if isinstance(value, np.generic):
607+
value = value.item()
608+
preview_items.append(repr(value))
609+
610+
truncated = len(preview_items) > self._REPR_PREVIEW_ITEMS
611+
if truncated:
612+
preview_items = preview_items[: self._REPR_PREVIEW_ITEMS]
613+
preview_items.append("...")
614+
615+
preview = ", ".join(preview_items)
616+
return f"Column({self._col_name!r}, dtype={self.dtype}, len={len(self)}, values=[{preview}])"
617+
600618
def __len__(self):
601619
return blosc2.count_nonzero(self._valid_rows)
602620

tests/ctable/test_column.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,5 +675,15 @@ def test_repr_is_single_line():
675675
assert "\n" not in repr(t)
676676

677677

678+
def test_column_repr_shows_preview_values():
679+
t = CTable(Row, new_data=DATA20)
680+
r = repr(t["id"][:])
681+
assert "Column('id'" in r
682+
assert "dtype=int64" in r
683+
assert "len=20" in r
684+
assert "values=[0, 1, 2" in r
685+
assert "..." in r
686+
687+
678688
if __name__ == "__main__":
679689
pytest.main(["-v", __file__])

0 commit comments

Comments
 (0)