finesse.utilities.tables module

Defines classes for displaying tables.

This code is inspired by the tabulate package which can be found at https://github.com/astanin/python-tabulate/ and on PyPI

class finesse.utilities.tables.NumberTable(table, colnames=None, rownames=None, colfunc=None, bgcolfunc=None, norm=None, numfmt=None, compact=False)[source]

Bases: Table

Create a table of numbers to display via pretty printing or html.

Parameters

tablearray or list-of-lists

A two dimensional object containing the values of the table.

colnamesarray or list, optional

An array or list with the same length as the second dimension of table. These are the column names displayed in the first row. If rownames are given this may contain an additional element to display a name of the rownames.

rownamesarray or list, optional

An array or list with the same length as the first dimension of table. These are the row names displayed in the first column.

colfuncfunc, optional

A function to assign colors to all values displayed. It will get called with table as argument and must return an array with shape table.shape + (4,) and type float with color values ranging from 0 to 1. The returned array will be interpreted as RGBA color data, but the the A value will be ignored. If any value in a RGB triple is negative, no color will be applied to the corresponding cell. Accepts matplotlib colormaps.

bgcolfuncfunc, optional

A function to set the backgroundcolors of the cells, works like colorfunc.

normfunc, optional

This function is applied to the table data before passing it to the color functions. This is intended for normalization functions from matplotlib.colors when using matplotlib colormaps, but can be useful with other functions like numpy.gradient.

numfmtstr or func or array, optional

Either a function to format numbers or a formatting string. The function must return a string. Can also be an array with one option per row, column or cell.

compactboolean, optional

Skip internal row separators to save space when printing as string.

make_colors(table, colorfunc, backgroundcolorfunc, norm)[source]

Calculate the colors for the table.

Produces an array with RGB color values in the range [0, 255] for every cell by calling colorfunc(norm(table)) and backgroundcolorfunc(norm(table)).

Parameters

tablearray

The data table without headers. Should contain numbers, but accepts arbitrary data as long as the color and norm functions can handle it.

colorfunccallable or None

A function producing RGB color data for every cell. Color values must be in [0,1]. If None the array is filled with (-1,-1,-1).

backgroundcolorfunccallable or None

A function producing RGB color data for every cell. Color values must be in [0,1]. If None the array is filled with (-1,-1,-1).

normcallable or None

A normalization applied before calling the color functions. If None it is replaced by a function that does nothing.

Returns

color

The array produced by colorfunc

backgroundcolor

The array produced by backgroundcolorfunc

make_string(obj, row, col)[source]

Convert obj to a string using self.number_format[row, column].

If the cell is a header or self.number_format[row, column] is None, obj is converted with str. If self.number_format[row, column] is a function self.number_format[row, column](obj) is called. If self.number_format[row, column] is a string self.number_format[row, col].format(obj) is called.

class finesse.utilities.tables.Table(table, headerrow=True, headercolumn=False, color=(-1, -1, -1), backgroundcolor=(-1, -1, -1), alignment='left', compact=False)[source]

Bases: object

Basic class to display tables via pretty printing or html.

This class is a container object for formatted tables. All entries should either be strings or have a __str__ method with readable output. The table can be pretty printed for console and displayed as html. Additionally the table can be saved to a csv file using the same syntax as the csv package or rendered as latex code.

Parameters

tablearray or list

A two dimensional array or list-of-lists containing strings.

headerrowbool, default=True

Whether the first row is a header.

headercolumnbool, default=False

Whether the first column is a header.

colorarray, optional

An array with shape table.shape + (3,) of type int containing RGB color data to control the textcolor. If any value in a RGB triple is negative, no color will be applied to the corresponding cell.

backgroundcolorarray, optional

An array to set the backgroundcolors of the cells, works like color.

alignmentarray, optional

An array with on of (“left”, “center”, “right) for each cell.

compactboolean, optional

Skip internal row separators to save space when printing as string.

Notes

Instead of arrays, color, backgroundcolor and alignment can be given only one option per row, column or total. This will be expanded for the whole table.

apply_ansi_colors(text, color, backgroundcolor)[source]

Add the color codes to the string if necessary.

Colors should be given as RGB tuples with integer values from [0,255].

escape_latex(text)[source]

Replace special characters in latex with their latex representation.

expand_string(text, width, align='right')[source]

Expand a string to have a certain length.

Add blank spaces to the string to get its visible length to width. align controls whether the spaces are added in front, at the end or both. text will be converted to str.

Parameters

textstr

Can also be an objection with __str__ defined.

widthint

Desired length

alignstr, optional

Must be one of “right”, “left” or “center”. Defaults to “right”.

Raises

ValueError

If the string is shorter than the given length or align is not one of the given options.

generate_grid_table()[source]

Create the whole table.

generate_html_cell(row, column, style=None)[source]

Generate html code for the table cell at position (row, column).

style may contain pairs of style options with their values.

generate_html_table()[source]
generate_row(i, start, headerdivision, division, end)[source]

Create the `i`th row with the given elements.

generate_seperating_row(start, line, intersection, headerintersection, end)[source]

Generate the string separating two rows.

get_max_column_width(j)[source]

Determine the maximum width of an entry in this column and add 2.

get_visible_length(text)[source]

Get the visible length of the string.

Parameters

text : str

Returns

int

The number of non combining characters after removing ANSI escape sequences.

Notes

Using code from https://stackoverflow.com/questions/14693701/how-can- i-remove-the-ansi-escape-sequences-from-a-string-in-python and https://stackoverflow.com/questions/33351599/how-do-i-get-the- visible-length-of-a-combining-unicode-string-in-python

latex()[source]

Make latex code that produces the table.

If the table cells have colored text, the xcolor package must be used in the latex file, the backgroundcolor is not used.

make_latex_colors()[source]

Define the colors used in the table for latex.

make_latex_str(row, column)[source]

Create latex code for the cell at position (row, column).

print()[source]

Show the table.

Will use html rendering if possible, otherwise pretty prints the table.

write_csv(csvfile, dialect='excel', **fmtparams)[source]

Write the table data to a csv file using the csv package.

Parameters

csvfile :

Any object with a write method. The file the data will be written to.

dialectcsv.Dialect or str, default=”excel”

The CSV dialect to use. may be an instance of csv.Dialect or any subclass thereof or any string in csv.list_dialects().

**fmtparamsdict, optional

Formatting parameters for csv.writer

Returns

The writer object used.