diff options
Diffstat (limited to 'table.py')
-rw-r--r-- | table.py | 140 |
1 files changed, 70 insertions, 70 deletions
@@ -26,13 +26,13 @@ class Table(object): self.create_columns(col_names) if row_names: self.create_rows(row_names) - + def _next_row_name(self): return 'row%s' % (len(self.row_names)+1) def __iter__(self): return iter(self.data) - + def __eq__(self, other): if other is None: return False @@ -44,7 +44,7 @@ class Table(object): def __len__(self): return len(self.row_names) - + ## Rows / Columns creation ################################################# def create_rows(self, row_names): """Appends row_names to the list of existing rows @@ -52,7 +52,7 @@ class Table(object): self.row_names.extend(row_names) for row_name in row_names: self.data.append([self.default_value]*len(self.col_names)) - + def create_columns(self, col_names): """Appends col_names to the list of existing columns """ @@ -66,7 +66,7 @@ class Table(object): self.row_names.append(row_name) self.data.append([self.default_value]*len(self.col_names)) - + def create_column(self, col_name): """Creates a colname to the col_names list """ @@ -83,7 +83,7 @@ class Table(object): self.sort_by_column_index(col_index, method) except ValueError: raise KeyError("Col (%s) not found in table" % (col_id)) - + def sort_by_column_index(self, col_index, method = 'asc'): """Sorts the table 'in-place' according to data stored in col_index @@ -97,7 +97,7 @@ class Table(object): # If we want reverse sort, then reverse list if method.lower() == 'desc': sort_list.reverse() - + # Rebuild data / row names self.data = [] self.row_names = [] @@ -134,14 +134,14 @@ class Table(object): for row in self.data[:]: if row[col_index] == value: self.data.remove(row) - - + + ## The 'setter' part ####################################################### def set_cell(self, row_index, col_index, data): """sets value of cell 'row_indew', 'col_index' to data """ self.data[row_index][col_index] = data - + def set_cell_by_ids(self, row_id, col_id, data): """sets value of cell mapped by row_id and col_id to data @@ -157,8 +157,8 @@ class Table(object): self.data[row_index][col_index] = data except ValueError: raise KeyError("Column (%s) not found in table" % (col_id)) - - + + def set_row(self, row_index, row_data): """sets the 'row_index' row pre: @@ -167,7 +167,7 @@ class Table(object): """ self.data[row_index] = row_data - + def set_row_by_id(self, row_id, row_data): """sets the 'row_id' column pre: @@ -180,7 +180,7 @@ class Table(object): self.set_row(row_index, row_data) except ValueError: raise KeyError('Row (%s) not found in table' % (row_id)) - + def append_row(self, row_data, row_name=None): """Appends a row to the table @@ -204,7 +204,7 @@ class Table(object): row_name = row_name or self._next_row_name() self.row_names.insert(index, row_name) self.data.insert(index, row_data) - + def delete_row(self, index): """Deletes the 'index' row in the table, and returns it. @@ -212,7 +212,7 @@ class Table(object): """ self.row_names.pop(index) return self.data.pop(index) - + def delete_row_by_id(self, row_id): """Deletes the 'row_id' row in the table. @@ -223,7 +223,7 @@ class Table(object): self.delete_row(row_index) except ValueError: raise KeyError('Row (%s) not found in table' % (row_id)) - + def set_column(self, col_index, col_data): """sets the 'col_index' column @@ -231,7 +231,7 @@ class Table(object): type(col_data) == types.ListType len(col_data) == len(self.row_names) """ - + for row_index, cell_data in enumerate(col_data): self.data[row_index][col_index] = cell_data @@ -248,7 +248,7 @@ class Table(object): self.set_column(col_index, col_data) except ValueError: raise KeyError('Column (%s) not found in table' % (col_id)) - + def append_column(self, col_data, col_name): """Appends the 'col_index' column @@ -259,7 +259,7 @@ class Table(object): self.col_names.append(col_name) for row_index, cell_data in enumerate(col_data): self.data[row_index].append(cell_data) - + def insert_column(self, index, col_data, col_name): """Appends col_data before 'index' in the table. To make 'insert' @@ -272,7 +272,7 @@ class Table(object): self.col_names.insert(index, col_name) for row_index, cell_data in enumerate(col_data): self.data[row_index].insert(index, cell_data) - + def delete_column(self, index): """Deletes the 'index' column in the table, and returns it. @@ -292,15 +292,15 @@ class Table(object): except ValueError: raise KeyError('Column (%s) not found in table' % (col_id)) - + ## The 'getter' part ####################################################### def get_shape(self): """Returns a tuple which represents the table's shape - """ + """ return len(self.row_names), len(self.col_names) shape = property(get_shape) - + def __getitem__(self, indices): """provided for convenience""" rows, multirows = None, False @@ -371,7 +371,7 @@ class Table(object): warn('table.get_cell(i,j) is deprecated, use table[i,j] instead', DeprecationWarning, stacklevel=2) return self.data[row_index][col_index] - + def get_cell_by_ids(self, row_id, col_id): """Returns the element at [row_id][col_id] """ @@ -416,7 +416,7 @@ class Table(object): return set(col) else: return col - + def get_column_by_id(self, col_id, distinct=False): """Returns the 'col_id' col """ @@ -427,7 +427,7 @@ class Table(object): except ValueError: raise KeyError("Column (%s) not found in table" % (col_id)) return self.get_column(col_index, distinct) - + def get_rows(self): """Returns all the rows in the table @@ -442,13 +442,13 @@ class Table(object): """ return [self[:,index] for index in range(len(self.col_names))] - + def apply_stylesheet(self, stylesheet): """Applies the stylesheet to this table """ for instruction in stylesheet.instructions: eval(instruction) - + def transpose(self): """Keeps the self object intact, and returns the transposed (rotated) @@ -502,7 +502,7 @@ class Table(object): lines.insert(0, '-'*max_line_length) lines.append('-'*max_line_length) return '\n'.join(lines) - + def __repr__(self): return repr(self.data) @@ -514,9 +514,9 @@ class Table(object): data.append([str(cell) for cell in row]) lines = ['\t'.join(row) for row in data] return '\n'.join(lines) - - + + class TableStyle: """Defines a table's style """ @@ -535,8 +535,8 @@ class TableStyle: # We shouldn't have to create an entry for # the 1st col (the row_column one) self.units = dict([(col_name,'') for col_name in table.col_names]) - self.units['__row_column__'] = '' - + self.units['__row_column__'] = '' + # XXX FIXME : params order should be reversed for all set() methods def set_size(self, value, col_id): """sets the size of the specified col_id to value @@ -580,7 +580,7 @@ class TableStyle: """ self.units[col_id] = value - + def set_unit_by_index(self, value, col_index): """Allows to set the unit according to the column index rather than using the column's id. @@ -594,13 +594,13 @@ class TableStyle: col_id = self._table.col_names[col_index-1] self.units[col_id] = value - + def get_size(self, col_id): """Returns the size of the specified col_id """ return self.size[col_id] - + def get_size_by_index(self, col_index): """Allows to get the size according to the column index rather than @@ -653,7 +653,7 @@ class TableStyle: return self.units[col_id] -import re +import re CELL_PROG = re.compile("([0-9]+)_([0-9]+)") class TableStyleSheet: @@ -665,9 +665,9 @@ class TableStyleSheet: the following rule : 2_5 = 2_3 + 2_4 You can also use all the math.* operations you want. For example: - 2_5 = sqrt(2_3**2 + 2_4**2) + 2_5 = sqrt(2_3**2 + 2_4**2) """ - + def __init__(self, rules = None): rules = rules or [] self.rules = [] @@ -688,7 +688,7 @@ class TableStyleSheet: except SyntaxError: print "Bad Stylesheet Rule : %s [skipped]"%rule - + def add_rowsum_rule(self, dest_cell, row_index, start_col, end_col): """Creates and adds a rule to sum over the row at row_index from start_col to end_col. @@ -702,7 +702,7 @@ class TableStyleSheet: end_col + 1)] rule = '%d_%d=' % dest_cell + '+'.join(cell_list) self.add_rule(rule) - + def add_rowavg_rule(self, dest_cell, row_index, start_col, end_col): """Creates and adds a rule to make the row average (from start_col @@ -718,7 +718,7 @@ class TableStyleSheet: num = (end_col - start_col + 1) rule = '%d_%d=' % dest_cell + '('+'+'.join(cell_list)+')/%f'%num self.add_rule(rule) - + def add_colsum_rule(self, dest_cell, col_index, start_row, end_row): """Creates and adds a rule to sum over the col at col_index from @@ -728,13 +728,13 @@ class TableStyleSheet: pre: start_row >= 0 end_row > start_row - """ + """ cell_list = ['%d_%d'%(index, col_index) for index in range(start_row, end_row + 1)] rule = '%d_%d=' % dest_cell + '+'.join(cell_list) self.add_rule(rule) - - + + def add_colavg_rule(self, dest_cell, col_index, start_row, end_row): """Creates and adds a rule to make the col average (from start_row to end_row) @@ -776,7 +776,7 @@ class TableCellRenderer: table_style, col_index +1) return self._render_cell_content(final_content, table_style, col_index + 1) - + def render_row_cell(self, row_name, table, table_style): """Renders the cell for 'row_id' row @@ -792,7 +792,7 @@ class TableCellRenderer: col_index = table.col_names.index(col_name) return self._render_cell_content(cell_value, table_style, col_index +1) - + def _render_cell_content(self, content, table_style, col_index): """Makes the appropriate rendering for this cell content. @@ -803,7 +803,7 @@ class TableCellRenderer: """ return content - + def _make_cell_content(self, cell_content, table_style, col_index): """Makes the cell content (adds decoration data, like units for example) @@ -815,7 +815,7 @@ class TableCellRenderer: replacement_char = 0 if replacement_char and final_content == 0: return replacement_char - + try: units_on = self.properties['units'] if units_on: @@ -823,17 +823,17 @@ class TableCellRenderer: cell_content, table_style, col_index) except KeyError: pass - + return final_content - - + + def _add_unit(self, cell_content, table_style, col_index): """Adds unit to the cell_content if needed """ unit = table_style.get_unit_by_index(col_index) return str(cell_content) + " " + unit - - + + class DocbookRenderer(TableCellRenderer): """Defines how to render a cell for a docboook table @@ -845,8 +845,8 @@ class DocbookRenderer(TableCellRenderer): size = table_style.get_size_by_index(col_index) return '<colspec colname="c%d" colwidth="%s"/>\n' % \ (col_index, size) - - + + def _render_cell_content(self, cell_content, table_style, col_index): """Makes the appropriate rendering for this cell content. Rendering properties will be searched using the @@ -866,14 +866,14 @@ class DocbookRenderer(TableCellRenderer): class TableWriter: """A class to write tables """ - + def __init__(self, stream, table, style, **properties): self._stream = stream self.style = style or TableStyle(table) self._table = table self.properties = properties self.renderer = None - + def set_style(self, style): """sets the table's associated style @@ -885,8 +885,8 @@ class TableWriter: """sets the way to render cell """ self.renderer = renderer - - + + def update_properties(self, **properties): """Updates writer's properties (for cell rendering) """ @@ -897,7 +897,7 @@ class TableWriter: """Writes the table """ raise NotImplementedError("write_table must be implemented !") - + class DocbookTableWriter(TableWriter): @@ -911,7 +911,7 @@ class DocbookTableWriter(TableWriter): for col_index in range(len(self._table.col_names)+1): self._stream.write(self.renderer.define_col_header(col_index, self.style)) - + self._stream.write("<thead>\n<row>\n") # XXX FIXME : write an empty entry <=> the first (__row_column) column self._stream.write('<entry></entry>\n') @@ -919,7 +919,7 @@ class DocbookTableWriter(TableWriter): self._stream.write(self.renderer.render_col_cell( col_name, self._table, self.style)) - + self._stream.write("</row>\n</thead>\n") @@ -927,7 +927,7 @@ class DocbookTableWriter(TableWriter): """Writes the table body """ self._stream.write('<tbody>\n') - + for row_index, row in enumerate(self._table.data): self._stream.write('<row>\n') row_name = self._table.row_names[row_index] @@ -935,14 +935,14 @@ class DocbookTableWriter(TableWriter): self._stream.write(self.renderer.render_row_cell(row_name, self._table, self.style)) - + for col_index, cell in enumerate(row): self._stream.write(self.renderer.render_cell( (row_index, col_index), self._table, self.style)) - + self._stream.write('</row>\n') - + self._stream.write('</tbody>\n') @@ -955,7 +955,7 @@ class DocbookTableWriter(TableWriter): (len(self._table.col_names)+1)) self._write_headers() self._write_body() - + self._stream.write('</tgroup>\n</table>\n') - + |