""" Provides a simple table class. A SimpleTable is essentially a list of lists plus some formatting functionality. Dependencies: the Python 2.5+ standard library. Installation: just copy this module into your working directory (or anywhere in your pythonpath). Basic use:: mydata = [[11,12],[21,22]] # data MUST be 2-dimensional myheaders = [ "Column 1", "Column 2" ] mystubs = [ "Row 1", "Row 2" ] tbl = SimpleTable(mydata, myheaders, mystubs, title="Title") print( tbl ) print( tbl.as_csv() ) A SimpleTable is inherently (but not rigidly) rectangular. You should create it from a *rectangular* (2d!) iterable of data. Each item in your rectangular iterable will become the data of a single Cell. In principle, items can be any object, not just numbers and strings. However, default conversion during table production is by simple string interpolation. (So you cannot have a tuple as a data item *and* rely on the default conversion.) A SimpleTable allows only one column (the first) of stubs at initilization, concatenation of tables allows you to produce tables with interior stubs. (You can also assign the datatype 'stub' to the cells in any column, or use ``insert_stubs``.) A SimpleTable can be concatenated with another SimpleTable or extended by another SimpleTable. :: table1.extend_right(table2) table1.extend(table2) A SimpleTable can be initialized with `datatypes`: a list of ints that provide indexes into `data_fmts` and `data_aligns`. Each data cell is assigned a datatype, which will control formatting. If you do not specify the `datatypes` list, it will be set to ``range(ncols)`` where `ncols` is the number of columns in the data. (I.e., cells in a column have their own datatype.) This means that you can just specify `data_fmts` without bothering to provide a `datatypes` list. If ``len(datatypes)'] if self.title: title = '%s' % self.title formatted_rows.append(title) formatted_rows.extend(row.as_string('html', **fmt) for row in self) formatted_rows.append('') return '\n'.join(formatted_rows) def as_latex_tabular(self, center=True, **fmt_dict): '''Return string, the table as a LaTeX tabular environment. Note: will require the booktabs package.''' # fetch the text format, override with fmt_dict fmt = self._get_fmt('latex', **fmt_dict) formatted_rows = [] if center: formatted_rows.append(r'\begin{center}') table_dec_above = fmt['table_dec_above'] or '' table_dec_below = fmt['table_dec_below'] or '' prev_aligns = None last = None for row in self + [last]: if row == last: aligns = None else: aligns = row.get_aligns('latex', **fmt) if aligns != prev_aligns: # When the number/type of columns changes... if prev_aligns: # ... if there is a tabular to close, close it... formatted_rows.append(table_dec_below) formatted_rows.append(r'\end{tabular}') if aligns: # ... and if there are more lines, open a new one: formatted_rows.append(r'\begin{tabular}{%s}' % aligns) if not prev_aligns: # (with a nice line if it's the top of the whole table) formatted_rows.append(table_dec_above) if row != last: formatted_rows.append( row.as_string(output_format='latex', **fmt)) prev_aligns = aligns # tabular does not support caption, but make it available for # figure environment if self.title: title = r'%%\caption{%s}' % self.title formatted_rows.append(title) if center: formatted_rows.append(r'\end{center}') # Replace $$ due to bug in GH 5444 return '\n'.join(formatted_rows).replace('$$', ' ') def extend_right(self, table): """Return None. Extend each row of `self` with corresponding row of `table`. Does **not** import formatting from ``table``. This generally makes sense only if the two tables have the same number of rows, but that is not enforced. :note: To extend append a table below, just use `extend`, which is the ordinary list method. This generally makes sense only if the two tables have the same number of columns, but that is not enforced. """ for row1, row2 in zip(self, table): row1.extend(row2) def label_cells(self, func): """Return None. Labels cells based on `func`. If ``func(cell) is None`` then its datatype is not changed; otherwise it is set to ``func(cell)``. """ for row in self: for cell in row: label = func(cell) if label is not None: cell.datatype = label @property def data(self): return [row.data for row in self] def pad(s, width, align): """Return string padded with spaces, based on alignment parameter.""" if align == 'l': s = s.ljust(width) elif align == 'r': s = s.rjust(width) else: s = s.center(width) return s class Row(list): """Provides a table row as a list of cells. A row can belong to a SimpleTable, but does not have to. """ def __init__(self, seq, datatype='data', table=None, celltype=None, dec_below='row_dec_below', **fmt_dict): """ Parameters ---------- seq : sequence of data or cells table : SimpleTable datatype : str ('data' or 'header') dec_below : str (e.g., 'header_dec_below' or 'row_dec_below') decoration tag, identifies the decoration to go below the row. (Decoration is repeated as needed for text formats.) """ self.datatype = datatype self.table = table if celltype is None: if table is None: celltype = Cell else: celltype = table._Cell self._Cell = celltype self._fmt = fmt_dict self.special_fmts = dict() # special formatting for any output format self.dec_below = dec_below list.__init__(self, (celltype(cell, row=self) for cell in seq)) def add_format(self, output_format, **fmt_dict): """ Return None. Adds row-instance specific formatting for the specified output format. Example: myrow.add_format('txt', row_dec_below='+-') """ output_format = get_output_format(output_format) if output_format not in self.special_fmts: self.special_fmts[output_format] = dict() self.special_fmts[output_format].update(fmt_dict) def insert_stub(self, loc, stub): """Return None. Inserts a stub cell in the row at `loc`. """ _Cell = self._Cell if not isinstance(stub, _Cell): stub = stub stub = _Cell(stub, datatype='stub', row=self) self.insert(loc, stub) def _get_fmt(self, output_format, **fmt_dict): """Return dict, the formatting options. """ output_format = get_output_format(output_format) # first get the default formatting try: fmt = default_fmts[output_format].copy() except KeyError: raise ValueError('Unknown format: %s' % output_format) # second get table specific formatting (if possible) try: fmt.update(self.table.output_formats[output_format]) except AttributeError: pass # finally, add formatting for this row and this call fmt.update(self._fmt) fmt.update(fmt_dict) special_fmt = self.special_fmts.get(output_format, None) if special_fmt is not None: fmt.update(special_fmt) return fmt def get_aligns(self, output_format, **fmt_dict): """Return string, sequence of column alignments. Ensure comformable data_aligns in `fmt_dict`.""" fmt = self._get_fmt(output_format, **fmt_dict) return ''.join(cell.alignment(output_format, **fmt) for cell in self) def as_string(self, output_format='txt', **fmt_dict): """Return string: the formatted row. This is the default formatter for rows. Override this to get different formatting. A row formatter must accept as arguments a row (self) and an output format, one of ('html', 'txt', 'csv', 'latex'). """ fmt = self._get_fmt(output_format, **fmt_dict) # get column widths try: colwidths = self.table.get_colwidths(output_format, **fmt) except AttributeError: colwidths = fmt.get('colwidths') if colwidths is None: colwidths = (0,) * len(self) colsep = fmt['colsep'] row_pre = fmt.get('row_pre', '') row_post = fmt.get('row_post', '') formatted_cells = [] for cell, width in zip(self, colwidths): content = cell.format(width, output_format=output_format, **fmt) formatted_cells.append(content) formatted_row = row_pre + colsep.join(formatted_cells) + row_post formatted_row = self._decorate_below(formatted_row, output_format, **fmt) return formatted_row def _decorate_below(self, row_as_string, output_format, **fmt_dict): """This really only makes sense for the text and latex output formats. """ dec_below = fmt_dict.get(self.dec_below, None) if dec_below is None: result = row_as_string else: output_format = get_output_format(output_format) if output_format == 'txt': row0len = len(row_as_string) dec_len = len(dec_below) repeat, addon = divmod(row0len, dec_len) result = row_as_string + "\n" + (dec_below * repeat + dec_below[:addon]) elif output_format == 'latex': result = row_as_string + "\n" + dec_below else: raise ValueError("I cannot decorate a %s header." % output_format) return result @property def data(self): return [cell.data for cell in self] class Cell: """Provides a table cell. A cell can belong to a Row, but does not have to. """ def __init__(self, data='', datatype=None, row=None, **fmt_dict): if isinstance(data, Cell): # might have passed a Cell instance self.data = data.data self._datatype = data.datatype self._fmt = data._fmt else: self.data = data self._datatype = datatype self._fmt = dict() self._fmt.update(fmt_dict) self.row = row def __str__(self): return '%s' % self.data def _get_fmt(self, output_format, **fmt_dict): """Return dict, the formatting options. """ output_format = get_output_format(output_format) # first get the default formatting try: fmt = default_fmts[output_format].copy() except KeyError: raise ValueError('Unknown format: %s' % output_format) # then get any table specific formtting try: fmt.update(self.row.table.output_formats[output_format]) except AttributeError: pass # then get any row specific formtting try: fmt.update(self.row._fmt) except AttributeError: pass # finally add formatting for this instance and call fmt.update(self._fmt) fmt.update(fmt_dict) return fmt def alignment(self, output_format, **fmt_dict): fmt = self._get_fmt(output_format, **fmt_dict) datatype = self.datatype data_aligns = fmt.get('data_aligns', 'c') if isinstance(datatype, int): align = data_aligns[datatype % len(data_aligns)] elif datatype == 'stub': # still support deprecated `stubs_align` align = fmt.get('stubs_align') or fmt.get('stub_align', 'l') elif datatype in fmt: label_align = '%s_align' % datatype align = fmt.get(label_align, 'c') else: raise ValueError('Unknown cell datatype: %s' % datatype) return align @staticmethod def _latex_escape(data, fmt, output_format): if output_format != 'latex': return data if "replacements" in fmt: if isinstance(data, str): for repl in sorted(fmt["replacements"]): data = data.replace(repl, fmt["replacements"][repl]) return data def format(self, width, output_format='txt', **fmt_dict): """Return string. This is the default formatter for cells. Override this to get different formating. A cell formatter must accept as arguments a cell (self) and an output format, one of ('html', 'txt', 'csv', 'latex'). It will generally respond to the datatype, one of (int, 'header', 'stub'). """ fmt = self._get_fmt(output_format, **fmt_dict) data = self.data datatype = self.datatype data_fmts = fmt.get('data_fmts') if data_fmts is None: # chk allow for deprecated use of data_fmt data_fmt = fmt.get('data_fmt') if data_fmt is None: data_fmt = '%s' data_fmts = [data_fmt] if isinstance(datatype, int): datatype = datatype % len(data_fmts) # constrain to indexes data_fmt = data_fmts[datatype] if isinstance(data_fmt, str): content = data_fmt % (data,) elif callable(data_fmt): content = data_fmt(data) else: raise TypeError("Must be a string or a callable") if datatype == 0: content = self._latex_escape(content, fmt, output_format) elif datatype in fmt: data = self._latex_escape(data, fmt, output_format) dfmt = fmt.get(datatype) try: content = dfmt % (data,) except TypeError: # dfmt is not a substitution string content = dfmt else: raise ValueError('Unknown cell datatype: %s' % datatype) align = self.alignment(output_format, **fmt) return pad(content, width, align) def get_datatype(self): if self._datatype is None: dtype = self.row.datatype else: dtype = self._datatype return dtype def set_datatype(self, val): # TODO: add checking self._datatype = val datatype = property(get_datatype, set_datatype) # begin: default formats for SimpleTable """ Some formatting suggestions: - if you want rows to have no extra spacing, set colwidths=0 and colsep=''. (Naturally the columns will not align.) - if you want rows to have minimal extra spacing, set colwidths=1. The columns will align. - to get consistent formatting, you should leave all field width handling to SimpleTable: use 0 as the field width in data_fmts. E.g., :: data_fmts = ["%#0.6g","%#0.6g","%#0.4g","%#0.4g"], colwidths = 14, data_aligns = "r", """ default_txt_fmt = dict( fmt='txt', # basic table formatting table_dec_above='=', table_dec_below='-', title_align='c', # basic row formatting row_pre='', row_post='', header_dec_below='-', row_dec_below=None, colwidths=None, colsep=' ', data_aligns="r", # GH 1477 # data formats # data_fmt="%s", #deprecated; use data_fmts data_fmts=["%s"], # labeled alignments # stubs_align='l', #deprecated; use data_fmts stub_align='l', header_align='c', # labeled formats header_fmt='%s', # deprecated; just use 'header' stub_fmt='%s', # deprecated; just use 'stub' header='%s', stub='%s', empty_cell='', # deprecated; just use 'empty' empty='', missing='--', ) default_csv_fmt = dict( fmt='csv', table_dec_above=None, # '', table_dec_below=None, # '', # basic row formatting row_pre='', row_post='', header_dec_below=None, # '', row_dec_below=None, title_align='', data_aligns="l", colwidths=None, colsep=',', # data formats data_fmt='%s', # deprecated; use data_fmts data_fmts=['%s'], # labeled alignments # stubs_align='l', # deprecated; use data_fmts stub_align="l", header_align='c', # labeled formats header_fmt='"%s"', # deprecated; just use 'header' stub_fmt='"%s"', # deprecated; just use 'stub' empty_cell='', # deprecated; just use 'empty' header='%s', stub='%s', empty='', missing='--', ) default_html_fmt = dict( # basic table formatting table_dec_above=None, table_dec_below=None, header_dec_below=None, row_dec_below=None, title_align='c', # basic row formatting colwidths=None, colsep=' ', row_pre='\n ', row_post='\n', data_aligns="c", # data formats data_fmts=['%s'], data_fmt="%s", # deprecated; use data_fmts # labeled alignments # stubs_align='l', #deprecated; use data_fmts stub_align='l', header_align='c', # labeled formats header_fmt='%s', # deprecated; just use `header` stub_fmt='%s', # deprecated; just use `stub` empty_cell='', # deprecated; just use `empty` header='%s', stub='%s', empty='', missing='--', ) default_latex_fmt = dict( fmt='ltx', # basic table formatting table_dec_above=r'\toprule', table_dec_below=r'\bottomrule', header_dec_below=r'\midrule', row_dec_below=None, strip_backslash=True, # NotImplemented # row formatting row_post=r' \\', data_aligns='c', colwidths=None, colsep=' & ', # data formats data_fmts=['%s'], data_fmt='%s', # deprecated; use data_fmts # labeled alignments # stubs_align='l', # deprecated; use data_fmts stub_align='l', header_align='c', empty_align='l', # labeled formats header_fmt=r'\textbf{%s}', # deprecated; just use 'header' stub_fmt=r'\textbf{%s}', # deprecated; just use 'stub' empty_cell='', # deprecated; just use 'empty' header=r'\textbf{%s}', stub=r'\textbf{%s}', empty='', missing='--', # replacements will be processed in lexicographical order replacements={"#": r"\#", "$": r"\$", "%": r"\%", "&": r"\&", ">": r"$>$", "_": r"\_", "|": r"$|$"} ) default_fmts = dict( html=default_html_fmt, txt=default_txt_fmt, latex=default_latex_fmt, csv=default_csv_fmt ) output_format_translations = dict( htm='html', text='txt', ltx='latex' ) def get_output_format(output_format): if output_format not in ('html', 'txt', 'latex', 'csv'): try: output_format = output_format_translations[output_format] except KeyError: raise ValueError('unknown output format %s' % output_format) return output_format