def _try_except_from_branch(node, stmt): if stmt is node.body: return 'body', 1 if stmt is node.else_: return 'else', 1 for i, block_nodes in enumerate(node.handlers): if stmt in block_nodes: return 'except', i
def _try_except_from_branch(node, stmt): if stmt is node.body: return "body", 1 if stmt is node.else_: return "else", 1 for i, block_nodes in enumerate(node.handlers): if stmt in block_nodes: return "except", i
def _write_body(self): """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] # Write the first entry (row_name) 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")
def append_column(self, col_data, col_name): """Appends the 'col_index' column pre: type(col_data) == types.ListType len(col_data) == len(self.row_names) """ self.col_names.append(col_name) for row_index, cell_data in enumerate(col_data): self.data[row_index].append(cell_data)
def set_column(self, col_index, col_data): """sets the 'col_index' column pre: 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
def transpose(self): """Keeps the self object intact, and returns the transposed (rotated) table. """ transposed = Table() transposed.create_rows(self.col_names) transposed.create_columns(self.row_names) for col_index, column in enumerate(self.get_columns()): transposed.set_row(col_index, column) return transposed
def insert_column(self, index, col_data, col_name): """Appends col_data before 'index' in the table. To make 'insert' behave like 'list.insert', inserting in an out of range index will insert col_data to the end of the list pre: type(col_data) == types.ListType len(col_data) == len(self.row_names) """ 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 _write_body(self): """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] # Write the first entry (row_name) 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')
def pprint(self): """returns a string representing the table in a pretty printed 'text' format. """ # The maxium row name (to know the start_index of the first col) max_row_name = 0 for row_name in self.row_names: if len(row_name) > max_row_name: max_row_name = len(row_name) col_start = max_row_name + 5 lines = [] # Build the 'first' line <=> the col_names one # The first cell <=> an empty one col_names_line = [' '*col_start] for col_name in self.col_names: col_names_line.append(col_name.encode('iso-8859-1') + ' '*5) lines.append('|' + '|'.join(col_names_line) + '|') max_line_length = len(lines[0]) # Build the table for row_index, row in enumerate(self.data): line = [] # First, build the row_name's cell row_name = self.row_names[row_index].encode('iso-8859-1') line.append(row_name + ' '*(col_start-len(row_name))) # Then, build all the table's cell for this line. for col_index, cell in enumerate(row): col_name_length = len(self.col_names[col_index]) + 5 data = str(cell) line.append(data + ' '*(col_name_length - len(data))) lines.append('|' + '|'.join(line) + '|') if len(lines[-1]) > max_line_length: max_line_length = len(lines[-1]) # Wrap the table with '-' to make a frame lines.insert(0, '-'*max_line_length) lines.append('-'*max_line_length) return '\n'.join(lines)
def pprint(self): """returns a string representing the table in a pretty printed 'text' format. """ # The maxium row name (to know the start_index of the first col) max_row_name = 0 for row_name in self.row_names: if len(row_name) > max_row_name: max_row_name = len(row_name) col_start = max_row_name + 5 lines = [] # Build the 'first' line <=> the col_names one # The first cell <=> an empty one col_names_line = [' ' * col_start] for col_name in self.col_names: col_names_line.append(col_name.encode('iso-8859-1') + ' ' * 5) lines.append('|' + '|'.join(col_names_line) + '|') max_line_length = len(lines[0]) # Build the table for row_index, row in enumerate(self.data): line = [] # First, build the row_name's cell row_name = self.row_names[row_index].encode('iso-8859-1') line.append(row_name + ' ' * (col_start - len(row_name))) # Then, build all the table's cell for this line. for col_index, cell in enumerate(row): col_name_length = len(self.col_names[col_index]) + 5 data = str(cell) line.append(data + ' ' * (col_name_length - len(data))) lines.append('|' + '|'.join(line) + '|') if len(lines[-1]) > max_line_length: max_line_length = len(lines[-1]) # Wrap the table with '-' to make a frame lines.insert(0, '-' * max_line_length) lines.append('-' * max_line_length) return '\n'.join(lines)
def __getitem__(self, indices): """provided for convenience""" rows, multirows = None, False cols, multicols = None, False if isinstance(indices, tuple): rows = indices[0] if len(indices) > 1: cols = indices[1] else: rows = indices # define row slice if isinstance(rows,str): try: rows = self.row_names.index(rows) except ValueError: raise KeyError("Row (%s) not found in table" % (rows)) if isinstance(rows,int): rows = slice(rows,rows+1) multirows = False else: rows = slice(None) multirows = True # define col slice if isinstance(cols,str): try: cols = self.col_names.index(cols) except ValueError: raise KeyError("Column (%s) not found in table" % (cols)) if isinstance(cols,int): cols = slice(cols,cols+1) multicols = False else: cols = slice(None) multicols = True # get sub-table tab = Table() tab.default_value = self.default_value tab.create_rows(self.row_names[rows]) tab.create_columns(self.col_names[cols]) for idx,row in enumerate(self.data[rows]): tab.set_row(idx, row[cols]) if multirows : if multicols: return tab else: return [item[0] for item in tab.data] else: if multicols: return tab.data[0] else: return tab.data[0][0]
def __getitem__(self, indices): """provided for convenience""" rows, multirows = None, False cols, multicols = None, False if isinstance(indices, tuple): rows = indices[0] if len(indices) > 1: cols = indices[1] else: rows = indices # define row slice if isinstance(rows, str): try: rows = self.row_names.index(rows) except ValueError: raise KeyError("Row (%s) not found in table" % (rows)) if isinstance(rows, int): rows = slice(rows, rows + 1) multirows = False else: rows = slice(None) multirows = True # define col slice if isinstance(cols, str): try: cols = self.col_names.index(cols) except ValueError: raise KeyError("Column (%s) not found in table" % (cols)) if isinstance(cols, int): cols = slice(cols, cols + 1) multicols = False else: cols = slice(None) multicols = True # get sub-table tab = Table() tab.default_value = self.default_value tab.create_rows(self.row_names[rows]) tab.create_columns(self.col_names[cols]) for idx, row in enumerate(self.data[rows]): tab.set_row(idx, row[cols]) if multirows: if multicols: return tab else: return [item[0] for item in tab.data] else: if multicols: return tab.data[0] else: return tab.data[0][0]