Goulib.table module

“mini pandas.DataFrame” Table class with Excel + CSV I/O, easy access to columns, HTML output, and much more.

Goulib.table.attr(args)[source]
class Goulib.table.Cell(data=None, align=None, fmt=None, tag=None, style={})[source]

Bases: object

Table cell with HTML attributes

Parameters:
  • data – cell value(s) of any type
  • align – string for HTML align attribute
  • fmt – format string applied applied to data
  • tag – called to build each cell. defaults to ‘td’
  • style – dict or string for HTML style attribute
__init__(data=None, align=None, fmt=None, tag=None, style={})[source]
Parameters:
  • data – cell value(s) of any type
  • align – string for HTML align attribute
  • fmt – format string applied applied to data
  • tag – called to build each cell. defaults to ‘td’
  • style – dict or string for HTML style attribute
__str__()[source]

Return str(self).

static read(x)[source]

interprets x as int, float, string or None

html(**kwargs)[source]
Returns:string HTML formatted cell:
  • if data is int, default align=”right”
  • if data is float, default align=”right” and fmt=’%0.2f’
  • if data is timedelta, align = “right” and formatting is done by datetime2.strftimedelta()
__class__

alias of builtins.type

__delattr__

Implement delattr(self, name).

__dir__()

Default dir() implementation.

__eq__

Return self==value.

__format__()

Default object formatter.

__ge__

Return self>=value.

__getattribute__

Return getattr(self, name).

__gt__

Return self>value.

__hash__

Return hash(self).

__init_subclass__()

This method is called when a class is subclassed.

The default implementation does nothing. It may be overridden to extend subclasses.

__le__

Return self<=value.

__lt__

Return self<value.

__ne__

Return self!=value.

__new__()

Create and return a new object. See help(type) for accurate signature.

__reduce__()

Helper for pickle.

__reduce_ex__()

Helper for pickle.

__repr__

Return repr(self).

__setattr__

Implement setattr(self, name, value).

__sizeof__()

Size of object in memory, in bytes.

class Goulib.table.Row(data, align=None, fmt=None, tag=None, style={})[source]

Bases: object

Table row with HTML attributes

Parameters:
  • data – (list of) cell value(s) of any type
  • align – (list of) string for HTML align attribute
  • fmt – (list of) format string applied applied to data
  • tag – (list of) tags called to build each cell. defaults to ‘td’
  • style – (list of) dict or string for HTML style attribute
__init__(data, align=None, fmt=None, tag=None, style={})[source]
Parameters:
  • data – (list of) cell value(s) of any type
  • align – (list of) string for HTML align attribute
  • fmt – (list of) format string applied applied to data
  • tag – (list of) tags called to build each cell. defaults to ‘td’
  • style – (list of) dict or string for HTML style attribute
__str__()[source]

Return str(self).

html(cell_args={}, **kwargs)[source]

return in HTML format

__class__

alias of builtins.type

__delattr__

Implement delattr(self, name).

__dir__()

Default dir() implementation.

__eq__

Return self==value.

__format__()

Default object formatter.

__ge__

Return self>=value.

__getattribute__

Return getattr(self, name).

__gt__

Return self>value.

__hash__

Return hash(self).

__init_subclass__()

This method is called when a class is subclassed.

The default implementation does nothing. It may be overridden to extend subclasses.

__le__

Return self<=value.

__lt__

Return self<value.

__ne__

Return self!=value.

__new__()

Create and return a new object. See help(type) for accurate signature.

__reduce__()

Helper for pickle.

__reduce_ex__()

Helper for pickle.

__repr__

Return repr(self).

__setattr__

Implement setattr(self, name, value).

__sizeof__()

Size of object in memory, in bytes.

class Goulib.table.Table(data=[], **kwargs)[source]

Bases: list

Table class with CSV I/O, easy access to columns, HTML output

inits a table, optionally by reading a Excel, csv or html file :param data: list of list of cells, or string as filename :param titles: optional list of strings used as column id :param footer: optional list of functions used as column reducers

__init__(data=[], **kwargs)[source]

inits a table, optionally by reading a Excel, csv or html file :param data: list of list of cells, or string as filename :param titles: optional list of strings used as column id :param footer: optional list of functions used as column reducers

__repr__()[source]
Returns:repr string of titles+5 first lines
__str__()[source]
Returns:string of full tables with linefeeds
html(head=None, foot=None, **kwargs)[source]

HTML representation of table

Parameters:
  • head – optional column headers, .titles by default
  • foot – optional column footers, .footer by default
  • style – style for the table
  • colstyle – list of dict of style attributes for each column
  • kwargs – optional parameters passed along to tag(‘table’… except: * start=optional start row * stop=optional end row used to display a subset of lines. in this case rows with ‘…’ cells are displayed before and/or after the lines
Returns:

string HTML representation of table

load(filename, **kwargs)[source]
read_element(element, **kwargs)[source]

read table from a DOM element. :Warning: drops all formatting

read_html(filename, **kwargs)[source]

read first table in HTML file

read_json(filename, **kwargs)[source]

appends a json file made of lines dictionaries

read_xls(filename, **kwargs)[source]

appends an Excel table

read_csv(filename, **kwargs)[source]

appends a .csv or similar file to the table

save(filename, **kwargs)[source]
write_xlsx(filename, **kwargs)[source]
json(**kwargs)[source]
Returns:string JSON representation of table
write_csv(filename, **kwargs)[source]

write the table in Excel csv format, optionally transposed

__eq__(other)[source]

compare 2 Tables contents, mainly for tests

ncols()[source]
Returns:number of columns, ignoring title
find_col(title)[source]

finds a column from a part of the title

icol(column)[source]

iterates a column

col(column, title=False)[source]
cols(title=False)[source]

iterator through columns

transpose(titles_column=0)[source]

transpose table :param: titles_column :return: Table where rows are made from self’s columns and vice-versa

index(value, column=0)[source]
Returns:int row number of first line where column contains value
__getitem__(n)[source]

x.__getitem__(y) <==> x[y]

get(row, col)[source]
set(row, col, value)[source]
setcol(col, value, i=0)[source]

set column values :param col: int or string column index :param value: single value assigned to whole column or iterable assigned to each cell :param i: optional int : index of first row to assign

append(line)[source]

appends a line to table :param line: can be either: * a list * a dict or column names:values

addcol(title, val=None, i=0)[source]

add column to the right

sort(by, reverse=False)[source]

sort by column

rowasdict(i)[source]

returns a line as a dict

asdict()[source]
groupby_gen(by, sort=True, removecol=True)[source]

generates subtables

groupby(by, sort=True, removecol=True)[source]

ordered dictionary of subtables

hierarchy(by='Level', factory=<function Table.<lambda>>, linkfct=<function Table.<lambda>>)[source]

builds a structure from a table containing a “level” column

applyf(by, f, skiperrors=False)[source]

apply a function to a column :param by: column name of number :param f: function of the form lambda cell:content :param skiperrors: bool. if True, errors while running f are ignored :return: bool True if ok, False if skiperrors==True and conversion failed

to_datetime(by, fmt='%Y-%m-%d %H:%M:%S', skiperrors=False)[source]

convert a column to datetime

to_date(by, fmt='%Y-%m-%d', skiperrors=False)[source]

convert a column to date

to_time(by, fmt='%H:%M:%S', skiperrors=False)[source]

convert a column to time

to_timedelta(by, fmt=None, skiperrors=False)[source]

convert a column to time

__add__

Return self+value.

__class__

alias of builtins.type

__contains__

Return key in self.

__delattr__

Implement delattr(self, name).

__delitem__

Delete self[key].

__dir__()

Default dir() implementation.

__format__()

Default object formatter.

__ge__

Return self>=value.

__getattribute__

Return getattr(self, name).

__gt__

Return self>value.

__hash__ = None
__iadd__

Implement self+=value.

__imul__

Implement self*=value.

__init_subclass__()

This method is called when a class is subclassed.

The default implementation does nothing. It may be overridden to extend subclasses.

__iter__

Implement iter(self).

__le__

Return self<=value.

__len__

Return len(self).

__lt__

Return self<value.

__mul__

Return self*value.

__ne__

Return self!=value.

__new__()

Create and return a new object. See help(type) for accurate signature.

__reduce__()

Helper for pickle.

__reduce_ex__()

Helper for pickle.

__reversed__()

Return a reverse iterator over the list.

__rmul__

Return value*self.

__setattr__

Implement setattr(self, name, value).

__setitem__

Set self[key] to value.

__sizeof__()

Return the size of the list in memory, in bytes.

clear()

Remove all items from list.

copy()

Return a shallow copy of the list.

count()

Return number of occurrences of value.

extend()

Extend list by appending elements from the iterable.

insert()

Insert object before index.

pop()

Remove and return item at index (default last).

Raises IndexError if list is empty or index is out of range.

remove()

Remove first occurrence of value.

Raises ValueError if the value is not present.

reverse()

Reverse IN PLACE.

total(funcs)[source]

build a footer row by appling funcs to all columns

remove_lines_where(f, value=(None, 0, ''))[source]
Parameters:f – function of the form lambda line:bool returning True if line should be removed
Returns:int number of lines removed