datapyground.utils.tabulate

Format tabular data into a text table for print.

the tabulate() function takes a pyarrow.RecordBatch and formats it into a text table. It will truncate long strings, format floats to 2 decimal places, and limit the number of rows to display. The function is used to display the result of a query in the DataPyground SQL engine.

Example:

>>> import pyarrow as pa
>>> data = {
...     "Product": ["Videogame", "Laptop", "Laptop"],
...     "Quantity": [8, 8, 7],
...     "Price": [66.5, 38.72, 77.46],
... }
>>> table = pa.RecordBatch.from_pydict(data)
>>> print(tabulate(table))
Product   | Quantity | Price
--------- | -------- | -----
Videogame | 8        | 66.50
Laptop    | 8        | 38.72
Laptop    | 7        | 77.46

Functions

datapyground.utils.tabulate.compute_max_colsize(cols: list[str], rows: list[list[str]]) list[int][source]

Compute the maximum size of each column in a table.

datapyground.utils.tabulate.format_value(v: Any) str[source]

Format a value to be printed in the table.

This function will format floats to 2 decimal places, and truncate long strings.

datapyground.utils.tabulate.maketablerow(cols: list[str], colsizes: list[int], fillvalue: str = ' ') str[source]

Make a table row with the given column sizes.

datapyground.utils.tabulate.tabulate(recordbatch: RecordBatch, max_rows: int = 20) str[source]

Format a RecordBatch into a text table.

Will produce a string like:

Product   | Quantity | Price | Total
--------- | -------- | ----- | ------
Videogame | 8        | 66.50 | 532.00
Laptop    | 8        | 38.72 | 309.76
Laptop    | 7        | 77.46 | 542.22