Looks like sphinx docstring uses rst, ok.

This commit is contained in:
Julien Palard
2018-10-23 18:45:55 +02:00
parent d527497b1f
commit 512c885011

View File

@@ -55,46 +55,48 @@ class Cell:
class Table:
"""Represents a table, handling cells that can span on multiple lines
or rows, like:
+-----------+-----+
| AAA | BBB |
+-----+-----+ |
| | XXX | |
| +-----+-----+
| DDD | CCC |
+-----+-----------+
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(...),
- Either with absolute positions: call ``table[line, col] = Cell(...)``,
this overwrite an existing cell if any.
- Either with relative positions: call the add_row() and
add_cell(Cell(...)) as needed.
- Either with relative positions: call the ``add_row()`` and
``add_cell(Cell(...))`` as needed.
Cell spanning on multiple rows or multiple columns (having a
colspan or rowspan greater than one) are automatically referenced
by all the table cells they covers. This is a usefull
representation as we can simply check `if self[x, y] is self[x,
y+1]` to recognize a rowspan.
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, either 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(str(table))
+--------+--------+
| foo | bar |
|========|========|
| FOO | BAR |
+--------+--------+
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(str(table))
+--------+--------+
| foo | bar |
|========|========|
| FOO | BAR |
+--------+--------+
"""
def __init__(self, colwidth=None):
@@ -106,8 +108,8 @@ class Table:
self.current_col = 0
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().
"""Add a row to the table, to use with ``add_cell()``. It is not needed
to call ``add_row()`` before the first ``add_cell()``.
"""
self.current_line += 1
self.current_col = 0
@@ -118,9 +120,10 @@ class Table:
self.separator = len(self.lines)
def add_cell(self, cell):
"""Add a cell to the current line, to use with add_row(). To add a
cell spanning on multiple lines or rows, simply set the
cell.colspan or cell.rowspan BEFORE inserting it to the table.
"""Add a cell to the current line, to use with ``add_row()``. To add
a cell spanning on multiple lines or rows, simply set the
``cell.colspan`` or ``cell.rowspan`` BEFORE inserting it to
the table.
"""
while self[self.current_line, self.current_col]:
self.current_col += 1
@@ -160,7 +163,7 @@ class Table:
def cell_width(self, cell, source):
"""Give the cell width, according to the given source (either
self.colwidth or self.measured_widths).
``self.colwidth`` or ``self.measured_widths``).
This take into account cells spanning on multiple columns.
"""
width = 0
@@ -178,8 +181,8 @@ class Table:
seen.add(cell)
def rewrap(self):
"""Call cell.wrap() on all cells, and measure each column width after
wrapping (result written in self.measured_widths).
"""Call ``cell.wrap()`` on all cells, and measure each column width
after wrapping (result written in ``self.measured_widths``).
"""
self.measured_widths = self.colwidth[:]
for cell in self.cells:
@@ -206,7 +209,7 @@ class Table:
def writesep(char="-", lineno=None):
# type: (unicode, Optional[int]) -> unicode
"""Called on the line *before* lineno.
Called with no lineno for the last sep.
Called with no *lineno* for the last sep.
"""
out = [] # type: List[unicode]
for colno, width in enumerate(self.measured_widths):