This commit is contained in:
Holt59 2017-05-22 22:42:00 +02:00
parent 6599ccb39f
commit f3c3a6185a
3 changed files with 83 additions and 25 deletions

View File

@ -7,7 +7,15 @@ from collections import Iterable
class element:
""" Class representing a latex element. """
""" Class representing a latex element.
An element is a latex element (e.g. \\figure), with a parent and some
childrens. An element can have no parent and 0 childrens.
By default, all childrens of an element must be instances of element
themselves. To avoid this behavior, `raw` element can be used. `raw`
elements do not wrap their children in `wrapper_element` when they
are no subclasses of `element`. """
# Joiner for childrens
sep = '\n'
@ -15,8 +23,20 @@ class element:
# Automatically add label after element if present
autolabel = True
def __init__(self, parent=None, childrens=[], label=None):
# Do not wrap non-element in wrapper_element
raw = False
def __init__(self, parent=None, childrens=[], label=None, raw=False):
""" Create a new element with given parameters.
Parameters:
- parent Parent of the element (another element).
- childrens Childrens of the element - Single element or list of
objects (element or not).
- label Label of the element.
- raw Raw childrens (see description of `element`). """
self.parent = parent
self.raw = raw
if childrens is None:
childrens = []
if not isinstance(childrens, Iterable):
@ -29,13 +49,14 @@ class element:
if not isinstance(childrens, Iterable):
childrens = [childrens]
for child in childrens:
if not isinstance(child, element):
child = wrapper_element(child, self)
try:
child.parent.remove(child)
except:
pass
child.parent = self
if not self.raw:
if not isinstance(child, element):
child = wrapper_element(child, self)
try:
child.parent.remove(child)
except:
pass
child.parent = self
self.childrens.append(child)
def content(self):

View File

@ -3,6 +3,9 @@
from .element import element
from .tabular import tabular
import itertools
import pandas as pd
class table(element):
""" Class representing a latex figure. """
@ -47,14 +50,43 @@ class table(element):
})
def multicolumn(n, s=''):
if n == 1:
return s
return '\\multicolumn{' + str(n) + '}{c}{' + s + '}'
class tabledf(table):
def __init__(self, df, header=None, **kargs):
def __init__(self, df, header=None, multilevels=False, fmt=None, **kargs):
super().__init__(**kargs)
if header is None:
header = df.columns
tab = tabular(columns='r' * len(header))
tab.addrow(header)
header = self.create_header(df)
multilevels = True
df = df.reset_index()
if multilevels:
ncols = len(header[-1])
else:
ncols = len(header)
tab = tabular(columns='r' * ncols)
if multilevels:
tab.addrows(header)
else:
tab.addrow(header)
tab.addhline()
tab.addrows(df.as_matrix())
tab.addrows(df.as_matrix(), fmt=fmt)
self.add(tab)
def create_header(self, df):
cols = df.columns
if not isinstance(cols, pd.MultiIndex):
return df.columns
idx = df.index.names
header = []
for levels, labels in zip(cols.levels, cols.labels):
tmp = [multicolumn(len(idx))]
for i, g in itertools.groupby(labels):
tmp.append(multicolumn(len(list(g)), levels[i]))
header.append(tmp)
header[-1] = idx + header[-1][1:] # Update last row
return header

View File

@ -8,17 +8,23 @@ class row_element(element):
""" Class representing a row of a tabular element. """
def __init__(self, columns, parent, label=None,
wrapper=False, endline=False):
super().__init__(parent, columns, label=label)
wrapper=False, endline=False, fmt=None):
super().__init__(parent, columns, label=label, raw=(fmt is not None))
self.wrapper = wrapper
self.endline = endline
self.__fmt = fmt
if self.__fmt:
self.raw = True
def content(self):
wp = str
row = self.childrens
if self.__fmt:
row = map(self.__fmt, self.childrens)
if self.wrapper:
cw = 1/len(self.childrens)
wp = lambda c: str(self.wrapper(c, width=cw))
out = ' & '.join(map(wp, self.childrens))
wp = lambda c: self.wrapper(c, width=cw)
row = map(wp, row)
out = ' & '.join(map(str, row))
if self.endline:
out += '\\\\'
return out
@ -63,7 +69,7 @@ class tabular(element):
""" Add a hline to the current tabular element. """
self.addrow(hline_element(self), False)
def addrow(self, row, wrap=None):
def addrow(self, row, wrap=None, fmt=None):
""" Add a row to the tabular.
Parameters:
@ -71,21 +77,20 @@ class tabular(element):
- wrap Can contains a wrapper element (typically a resizebox) for
the cell of the row.
"""
if not isinstance(row, row_element):
row = row_element(row, parent=self)
if wrap is None:
wrap = self.autowrap
row.wrapper = wrap
if not isinstance(row, row_element):
row = row_element(row, fmt=fmt, wrapper=wrap, parent=self)
if self.childrens:
last = self.childrens[-1]
if isinstance(last, row_element):
last.endline = True
self.add(row)
def addrows(self, rows):
def addrows(self, rows, **kargs):
""" Add multiple rows to the tabular (see addrow). """
for row in rows:
self.addrow(row)
self.addrow(row, **kargs)
def content(self):
return self.fmt().format('content', {