Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table: add raw output #265

Merged
merged 5 commits into from
Oct 18, 2024
Merged
Changes from 2 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
58 changes: 50 additions & 8 deletions pytools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1553,26 +1553,35 @@ def enumerate_basic_directions(dimensions):
class Table:
"""An ASCII table generator.

.. attribute:: nrows
.. attribute:: ncolumns
.. property:: nrows

.. attribute:: alignments
The number of rows currently in the table.

A :class:`tuple` of alignments of each column: ``"l"``, ``"c"``, or ``"r"``,
for left, center, and right alignment, respectively). Columns which
have no alignment specifier will use the last specified alignment. For
example, with ``alignments=("l", "r")``, the third and all following
columns will use right alignment.
.. property:: ncolumns

The number of columns currently in the table.

.. automethod:: __init__
.. automethod:: add_row

.. automethod:: __str__
.. automethod:: github_markdown
.. automethod:: csv
.. automethod:: latex
.. automethod:: raw
"""

def __init__(self, alignments: Optional[Tuple[str, ...]] = None) -> None:
"""Create a new :class:`Table`.

:arg alignments: A :class:`tuple` of alignments of each column:
``"l"``, ``"c"``, or ``"r"``, for left, center, and right
alignment, respectively). Columns which have no alignment specifier
will use the last specified alignment. For example, with
``alignments=("l", "r")``, the third and all following
columns will use right alignment.
"""

if alignments is None:
alignments = ("l",)
else:
Expand All @@ -1593,6 +1602,7 @@ def ncolumns(self) -> int:
return len(self.rows[0])

def add_row(self, row: Tuple[Any, ...]) -> None:
"""Add *row* to the table."""
if self.rows and len(row) != self.ncolumns:
raise ValueError(
f"tried to add a row with {len(row)} columns to "
Expand Down Expand Up @@ -1756,6 +1766,38 @@ def latex(self,

return "\n".join(lines)

def raw(self) -> str:
matthiasdiener marked this conversation as resolved.
Show resolved Hide resolved
"""Returns a string representation of the table without any formatting.

.. doctest::

>>> tbl = Table()
>>> tbl.add_row([0, "skipped"])
>>> tbl.add_row([1111, "apple"])
>>> tbl.add_row([2, "pear"])
>>> print(tbl.raw())
0 skipped
1111 apple
2 pear
"""
if not self.rows:
return ""

alignments = self._get_alignments()
col_widths = self._get_column_widths(self.rows)

lines = [" ".join([
cell.center(col_width) if align == "c"
else cell.ljust(col_width) if align == "l"
else cell.rjust(col_width)
for cell, col_width, align in zip(row, col_widths, alignments)])
for row in self.rows]

# Remove the extra space added by the last cell
lines = [line.rstrip() for line in lines]

return "\n".join(lines)


def merge_tables(*tables: Table,
skip_columns: Optional[Tuple[int, ...]] = None) -> Table:
Expand Down
Loading