|
| 1 | +#!/usr/bin/env python |
| 2 | +# coding=utf-8 |
| 3 | +""" |
| 4 | +Simple demonstration of TableFormatter with a list of dicts for the table entries. |
| 5 | +This approach requires providing the dictionary key to query |
| 6 | +for each cell (via attrib='attrib_name'). |
| 7 | +""" |
| 8 | +from tableformatter import generate_table, FancyGrid, SparseGrid, Column |
| 9 | + |
| 10 | + |
| 11 | +class MyRowObject(object): |
| 12 | + """Simple object to demonstrate using a list of objects with TableFormatter""" |
| 13 | + def __init__(self, field1: str, field2: str, field3: str, field4: str): |
| 14 | + self.field1 = field1 |
| 15 | + self.field2 = field2 |
| 16 | + self._field3 = field3 |
| 17 | + self.field4 = field4 |
| 18 | + |
| 19 | + def get_field3(self): |
| 20 | + """Demonstrates accessing object functions""" |
| 21 | + return self._field3 |
| 22 | + |
| 23 | +KEY1 = "Key1" |
| 24 | +KEY2 = "Key2" |
| 25 | +KEY3 = "Key3" |
| 26 | +KEY4 = "Key4" |
| 27 | + |
| 28 | +rows = [{KEY1:'A1', KEY2:'A2', KEY3:'A3', KEY4:'A4'}, |
| 29 | + {KEY1:'B1', KEY2:'B2\nB2\nB2', KEY3:'B3', KEY4:'B4'}, |
| 30 | + {KEY1:'C1', KEY2:'C2', KEY3:'C3', KEY4:'C4'}, |
| 31 | + {KEY1:'D1', KEY2:'D2', KEY3:'D3', KEY4:'D4'}] |
| 32 | + |
| 33 | + |
| 34 | +columns = (Column('Col1', attrib=KEY1), |
| 35 | + Column('Col2', attrib=KEY2), |
| 36 | + Column('Col3', attrib=KEY3), |
| 37 | + Column('Col4', attrib=KEY4)) |
| 38 | + |
| 39 | +print("Table with header, AlteratingRowGrid:") |
| 40 | +print(generate_table(rows, columns)) |
| 41 | + |
| 42 | + |
| 43 | +print("Table with header, transposed, AlteratingRowGrid:") |
| 44 | +print(generate_table(rows, columns, transpose=True)) |
| 45 | + |
| 46 | + |
| 47 | +print("Table with header, transposed, FancyGrid:") |
| 48 | +print(generate_table(rows, columns, grid_style=FancyGrid(), transpose=True)) |
| 49 | + |
| 50 | +print("Table with header, transposed, SparseGrid:") |
| 51 | +print(generate_table(rows, columns, grid_style=SparseGrid(), transpose=True)) |
| 52 | + |
| 53 | + |
| 54 | +columns2 = (Column('Col1', attrib=KEY3), |
| 55 | + Column('Col2', attrib=KEY2), |
| 56 | + Column('Col3', attrib=KEY1), |
| 57 | + Column('Col4', attrib=KEY4)) |
| 58 | + |
| 59 | + |
| 60 | +print("Table with header, Columns rearranged") |
| 61 | +print(generate_table(rows, columns2)) |
0 commit comments