class documentation

class Table:

View In Hierarchy

Represents a table, handling cells that can span multiple lines or rows, like:

+-----------+-----+
| AAA       | BBB |
+-----+-----+     |
|     | XXX |     |
|     +-----+-----+
| DDD | CCC       |
+-----+-----------+

This class can be used in two ways, either:

  • With absolute positions: call table[line, col] = Cell(...), this overwrites any existing cell(s) at these positions.
  • With relative positions: call the add_row() and add_cell(Cell(...)) as needed.

Cells spanning multiple rows or multiple columns (having a colspan or rowspan greater than one) are automatically referenced by all the table cells they cover. This is a useful representation as we can simply check if self[x, y] is self[x, y+1] to recognize a rowspan.

Colwidth is not automatically computed, it has to be given, either at construction time, or during the table construction.

Example usage:

table = Table([6, 6])
table.add_cell(Cell("foo"))
table.add_cell(Cell("bar"))
table.set_separator()
table.add_row()
table.add_cell(Cell("FOO"))
table.add_cell(Cell("BAR"))
print(table)
+--------+--------+
| foo    | bar    |
|========|========|
| FOO    | BAR    |
+--------+--------+
Method __getitem__ Undocumented
Method __init__ Undocumented
Method __repr__ Undocumented
Method __setitem__ Undocumented
Method __str__ Undocumented
Method ​_ensure​_has​_column Undocumented
Method ​_ensure​_has​_line Undocumented
Method add​_cell No summary
Method add​_row Add a row to the table, to use with add_cell(). It is not needed to call add_row() before the first add_cell().
Method cell​_width Give the cell width, according to the given source (either self.colwidth or self.measured_widths). This takes into account cells spanning multiple columns.
Method physical​_lines​_for​_line For a given line, compute the number of physical lines it spans due to text wrapping.
Method rewrap Call cell.wrap() on all cells, and measure each column width after wrapping (result written in self.measured_widths).
Method set​_separator Sets the separator below the current line.
Instance Variable colwidth Undocumented
Instance Variable current​_col Undocumented
Instance Variable current​_line Undocumented
Instance Variable lines Undocumented
Instance Variable measured​_widths Undocumented
Instance Variable separator Undocumented
Property cells Undocumented
def __getitem__(self, pos):

Undocumented

Parameters
pos:Tuple[int, int]Undocumented
Returns
CellUndocumented
def __init__(self, colwidth=None):

Undocumented

Parameters
colwidth:List[int]Undocumented
def __repr__(self):

Undocumented

Returns
strUndocumented
def __setitem__(self, pos, cell):

Undocumented

Parameters
pos:Tuple[int, int]Undocumented
cell:CellUndocumented
def __str__(self):

Undocumented

Returns
strUndocumented
def _ensure_has_column(self, col):

Undocumented

Parameters
col:intUndocumented
def _ensure_has_line(self, line):

Undocumented

Parameters
line:intUndocumented
def add_cell(self, cell):
Add a cell to the current line, to use with add_row(). To add a cell spanning multiple lines or rows, simply set the cell.colspan or cell.rowspan BEFORE inserting it into the table.
Parameters
cell:CellUndocumented
def add_row(self):
Add a row to the table, to use with add_cell(). It is not needed to call add_row() before the first add_cell().
def cell_width(self, cell, source):
Give the cell width, according to the given source (either self.colwidth or self.measured_widths). This takes into account cells spanning multiple columns.
Parameters
cell:CellUndocumented
source:List[int]Undocumented
Returns
intUndocumented
def physical_lines_for_line(self, line):
For a given line, compute the number of physical lines it spans due to text wrapping.
Parameters
line:List[Cell]Undocumented
Returns
intUndocumented
def rewrap(self):
Call cell.wrap() on all cells, and measure each column width after wrapping (result written in self.measured_widths).
def set_separator(self):
Sets the separator below the current line.
colwidth: List[int] =

Undocumented

current_col: int =

Undocumented

current_line: int =

Undocumented

lines: List[List[Cell]] =

Undocumented

measured_widths =

Undocumented

separator =

Undocumented

@property
cells: Generator[Cell, None, None] =

Undocumented