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
__repr__()[source]

Return repr(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__() → list

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).

__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

__setattr__

Implement setattr(self, name, value).

__sizeof__() → int

size of object in memory, in bytes

__str__

Return str(self).

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
__repr__()[source]

Return repr(self).

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

return in HTML format

__class__

alias of builtins.type

__delattr__

Implement delattr(self, name).

__dir__() → list

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).

__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

__setattr__

Implement setattr(self, name, value).

__sizeof__() → int

size of object in memory, in bytes

__str__

Return str(self).

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, colstyle={}, **kwargs)[source]

HTML representation of table

Parameters:
  • head – optional column headers, .titles by default
  • foot – optional column footers, .footer by default
  • style – (list of) dict of style attributes
  • 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

__add__

Return self+value.

__class__

alias of builtins.type

__contains__

Return key in self.

__delattr__

Implement delattr(self, name).

__delitem__

Delete self[key].

__dir__() → list

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.

__iter__

Implement iter(self).

__le__

Return self<=value.

__len__

Return len(self).

__lt__

Return self<value.

__mul__

Return self*value.n

__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__()

L.__reversed__() – return a reverse iterator over the list

__rmul__

Return self*value.

__setattr__

Implement setattr(self, name, value).

__setitem__

Set self[key] to value.

__sizeof__()

L.__sizeof__() – size of L in memory, in bytes

clear() → None -- remove all items from L
copy() → list -- a shallow copy of L
count(value) → integer -- return number of occurrences of value
extend(iterable) → None -- extend list by appending elements from the iterable
insert()

L.insert(index, object) – insert object before index

pop([index]) → item -- remove and return item at index (default last).

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

remove(value) → None -- remove first occurrence of value.

Raises ValueError if the value is not present.

reverse()

L.reverse() – reverse IN PLACE

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

convert a column to time

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