Example #1
0
    def ptable(self, rows, columns, grid_args, row_stylist):
        """Format tabular data for pretty-printing as a fixed-width table and then display it using a pager.

        :param rows: required argument - can be a list-of-lists (or another iterable of iterables), a two-dimensional
                             NumPy array, or an Iterable of non-iterable objects
        :param columns: column headers and formatting options per column
        :param grid_args: argparse arguments for formatting the grid
        :param row_stylist: function to determine how each row gets styled
        """
        if grid_args.color:
            grid = tf.AlternatingRowGrid(BACK_PRI, BACK_ALT)
        elif grid_args.fancy:
            grid = tf.FancyGrid()
        elif grid_args.sparse:
            grid = tf.SparseGrid()
        else:
            grid = None

        transpose = False
        if grid_args.transpose:
            transpose = True

        formatted_table = tf.generate_table(rows=rows, columns=columns, grid_style=grid, row_tagger=row_stylist,
                                            transpose=transpose)
        self.ppaged(formatted_table, chop=True)
Example #2
0
def test_object_table_sparse_grid(obj_rows, obj_cols):
    expected = '''
 A1  A2  A3  A4 
 B1  B2  B3  B4 
     B2         
     B2         
 C1  C2  C3  C4 
 D1  D2  D3  D4 \n                \n'''.lstrip('\n')
    table = tf.generate_table(obj_rows, obj_cols, grid_style=tf.SparseGrid())
    assert table == expected
Example #3
0
def test_basic_sparse_grid(rows):
    expected = '''
 A1  A2  A3  A4 
 B1  B2  B3  B4 
     B2         
     B2         
 C1  C2  C3  C4 
 D1  D2  D3  D4 \n                \n'''.lstrip('\n')
    table = tf.generate_table(rows, grid_style=tf.SparseGrid())
    assert table == expected
Example #4
0
def test_table_with_header_transposed_sparse(rows, cols):
    expected = '''
 Col1  A1  B1  C1  D1 
 Col2  A2  B2  C2  D2 
           B2         
           B2         
 Col3  A3  B3  C3  D3 
 Col4  A4  B4  C4  D4 \n                      \n'''.lstrip('\n')
    table = tf.generate_table(rows, cols, grid_style=tf.SparseGrid(), transpose=True)
    assert table == expected
Example #5
0
rows = [('A1', 'A2', 'A3', 'A4'), ('B1', 'B2\nB2\nB2', 'B3', 'B4'),
        ('C1', 'C2', 'C3', 'C4'), ('D1', 'D2', 'D3', 'D4')]

columns = ('Col1', 'Col2', 'Col3', 'Col4')

print("Basic Table, default style (AlternatingRowGrid):")
print(generate_table(rows))

print("Basic Table, transposed, default style (AlternatingRowGrid):")
print(generate_table(rows, transpose=True))

print("Basic Table, FancyGrid:")
print(generate_table(rows, grid_style=tf.FancyGrid()))

print("Basic Table, SparseGrid:")
print(generate_table(rows, grid_style=tf.SparseGrid()))

print("Table with header, AlteratingRowGrid:")
print(generate_table(rows, columns))

print("Table with header, transposed, AlteratingRowGrid:")
print(generate_table(rows, columns, transpose=True))

print("Table with header, transposed, FancyGrid:")
print(generate_table(rows, columns, grid_style=tf.FancyGrid(), transpose=True))

print("Table with header, transposed, SparseGrid:")
print(generate_table(rows, columns, grid_style=tf.SparseGrid(),
                     transpose=True))