Tabulator
NAME
Tabulator
DESCRIPTION
# TODO
* add valign parameters (or attribute)
CLASSES
builtins.object
Tabulator
class Tabulator(builtins.object)
| Tabulator(rows, cols=None, headers=None, len_func=<built-in function len>)
|
| Arrange data into tables.
|
| Methods defined here:
|
| __getattr__(self, attr)
|
| __init__(self, rows, cols=None, headers=None, len_func=<built-in function len>)
| rows: An iterable of rows, where each row itself is an iterable.
|
| cols: An iterable of column alignments.
|
| headers: An iterable of column alignments. If given, the first row will be
| treated as headers with the formatting of this column applied.
|
| len_func: A function for calculating the length. Use this when the output
| will contain invisible characters such as ANSI escapes or other characters
| that should be ignored for column width calculations.
|
| The alignment iterables are sequences of the characters "l", "c" and "r",
| specifying "left", "center" and "right" alignment in the column,
| respectively. The default is 'l'. If there are not enough entries to align
| all values in the row then the last entry will be repeated, so ('l', 'r')
| would left-align the first column, then right-align all columns after it.
|
| Some output formats support 'j' ("justify") as well.
|
| Cell data must already be in string format.
|
| __setattr__(self, attr, value)
| Implement setattr(self, name, value).
|
| __str__(self)
| Return str(self).
|
| get_rows(self)
| Get an iterator over the rows of formatted cells.
|
| len(self, s)
|
| to_box_table(self, double=False, top=False, bottom=False, middle=False, left=False, right=False, center=False, between_rows=False)
| Create a table drawn with UTF-8 box-drawing characters.
|
| "double" sets the secondary style to double lines. If false, thick lines
| will be used.
|
| "top", "bottom", etc. turn secondary style on or off for that border.
|
| to_json(self, strip=False, simple=True, sort_keys=True, indent=None)
| Return the table in JSON format.
|
| strip: If True, entries will be stripped of leading and trailing whitespace.
|
| simple: If True, the rows will be a list of lists. If False, each row will
| be dictionary with the headers used as field names. This obviously requires
| headers.
|
| to_latex_table(self, top='', middle='\\hline', end='\\hline', indent=' ')
| Create a LaTex representation of the table.
|
| The current implementation is still quite primitive.
|
| to_markdown_table(self)
| Create a markdown grid table as recognized by Pandoc.
|
| to_plain_table(self, spacer='\t')
|
| to_table(self, typ='plain')
| A simple wrapper around the to_*_table functions.
|
| to_xhtml_table(self, indent=' ', pre=False)
| Create an XHTML representation of the table.
|
| indent: intdnt argument to pass to toprettyxml
|
| pre: wrap cells with "pre" tags
|
| Only the table is returned, not a complete XHTML page.
|
| ----------------------------------------------------------------------
| Data descriptors defined here:
|
| __dict__
| dictionary for instance variables (if defined)
|
| __weakref__
| list of weak references to the object (if defined)
|
| ----------------------------------------------------------------------
| Data and other attributes defined here:
|
| table_types = ('plain', 'xhtml', 'box', 'markdown', 'latex', 'json')
FUNCTIONS
align_char(rows, cols=None, char='.', pad=' ', len_func=<built-in function len>)
Align elements of each column along a given character (or string). This is
useful for e.g. aligning columns along decimal points.
cols: indices of columns to align. All columns will be aligned by default.
char: the character (or string) by which to align. Default: "."
pad: the character to use for padding. Default: " "
len_func: A function for calculating the length. Use this when the output will
contain invisible characters such as ANSI escapes or other characters that
should be ignored for column width calculations.
homogenize(rows, cols=None, pad=' ', align='l', len_func=<built-in function len>)
Pad all cells in a given column to the same width.
cols: indices of columns to homogenize. All columns will be aligned by
default.
pad: character to use for padding.
align: alignment direction. l: left, r:right, c:center
len_func: A function for calculating the length. Use this when the output will
contain invisible characters such as ANSI escapes or other characters that
should be ignored for column width calculations.
This is useful for centering a column in the table body with a header that is
larger than any other cellin the table body. For example, a table of element
symbols and weights would have a column of element symbols no wider than 3
characters, but the header might be "Symbol". This function could then be used
to center the symbols in the column while still aligning the left character of
each symbol.
pad(string, width, alignment, char=' ', len_func=None)
Pad a string to a given width with a given alignment and padding character.
string: the string
width: the width
alignment: the alignment ('l': left, 'c': center, 'r': right)
char: the character to ue for padding (default: ' ')
len_func: A function for calculating the length. Use this when the output will
contain invisible characters such as ANSI escapes or other characters that
should be ignored for column width calculations.
tzip(fields, widths, alignments, default_alignment='l')
A zipper function for generating tuples of fields, widths and alignments.
fields: The list or tuple of field elements.
widths: The widths corresponding to each field.
alignments: The alignments for each field. If empty, the default alignment
will be used. If not empty but shorter than the number of fields and widths,
the last given alignment will be used for the missing values. Valid
alignments: 'l' for left, 'c' for center, 'r' for right
default_alignment: The default alignment to use if the alignments are empty.