Skip to content

Commit 2612898

Browse files
tleonhardtanselor
authored andcommitted
Refactored grid classes to put them in an inheritance hierarchy with an abstract base class at the root
generate_table() now takes an instance of a Grid class for grid_style instead of a class type. There is a global DEFAULT_GRID which can be overriden with the set_default_grid() method. AlternatingRowGrid now takes two optional arguments in its initializer: bg_reset and bg_color for the two alternating colors. Also: - Added the start of a color.py example for showcasing colored output - Updated README - Minor formatting tweaks to the examples
1 parent 4adb978 commit 2612898

File tree

6 files changed

+210
-188
lines changed

6 files changed

+210
-188
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ Supported grid sytles are:
102102
```Python
103103
from tableformatter import FancyGrid
104104

105-
print(generate_table(rows, grid_style=FancyGrid))
105+
print(generate_table(rows, grid_style=FancyGrid()))
106106
╔════╤════╤════╤════╗
107107
║ A1 │ A2 │ A3 │ A4 ║
108108
╟────┼────┼────┼────╢

examples/color.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/usr/bin/env python
2+
# coding=utf-8
3+
"""
4+
Simple demonstration of TableFormatter with some colored output.
5+
"""
6+
import tableformatter as tf
7+
from tableformatter import generate_table
8+
9+
try:
10+
from colorama import Back
11+
BACK_RESET = Back.RESET
12+
BACK_BLUE = Back.LIGHTBLUE_EX
13+
except ImportError:
14+
try:
15+
from colored import bg
16+
BACK_RESET = bg(0)
17+
BACK_BLUE = bg(27)
18+
except ImportError:
19+
BACK_RESET = ''
20+
BACK_BLUE = ''
21+
22+
rows = [('A1', 'A2', 'A3', 'A4'),
23+
('B1', 'B2\nB2\nB2', 'B3', 'B4'),
24+
('C1', 'C2', 'C3', 'C4'),
25+
('D1', 'D2', 'D3', 'D4')]
26+
27+
columns = ('Col1', 'Col2', 'Col3', 'Col4')
28+
29+
print("Table with colorful alternting rows")
30+
print(generate_table(rows, columns, grid_style=tf.AlternatingRowGrid(BACK_RESET, BACK_BLUE)))
31+

examples/simple_object.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
#!/usr/bin/env python
22
# coding=utf-8
3-
from tableformatter import generate_table, FancyGrid, SparseGrid, Column
4-
53
"""
64
Simple demonstration of TableFormatter with a list of objects for the table entries.
7-
This approach requires providing the name of the object attribute to query
8-
for each cell (via attrib='attrib_name').
5+
This approach requires providing the name of the object attribute to query
6+
for each cell (via attrib='attrib_name').
97
TableFormatter will check if the attribute is callable. If it is, it wall be called
108
and the returned result will be displayed for that cell.
119
"""
10+
from tableformatter import generate_table, FancyGrid, SparseGrid, Column
1211

1312

1413
class MyRowObject(object):
@@ -44,10 +43,10 @@ def get_field3(self):
4443

4544

4645
print("Table with header, transposed, FancyGrid:")
47-
print(generate_table(rows, columns, grid_style=FancyGrid, transpose=True))
46+
print(generate_table(rows, columns, grid_style=FancyGrid(), transpose=True))
4847

4948
print("Table with header, transposed, SparseGrid:")
50-
print(generate_table(rows, columns, grid_style=SparseGrid, transpose=True))
49+
print(generate_table(rows, columns, grid_style=SparseGrid(), transpose=True))
5150

5251

5352
columns2 = (Column('Col1', attrib='field3'),

examples/simple_text.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
#!/usr/bin/env python
22
# coding=utf-8
3-
from tableformatter import generate_table, FancyGrid, SparseGrid
43
"""
54
Simple demonstration of TableFormatter with a list of tuples as table entries.
65
TableFormatter will automatically expand the row height to handle multi-line entries.
76
"""
8-
7+
import tableformatter as tf
8+
from tableformatter import generate_table
99

1010
rows = [('A1', 'A2', 'A3', 'A4'),
1111
('B1', 'B2\nB2\nB2', 'B3', 'B4'),
1212
('C1', 'C2', 'C3', 'C4'),
1313
('D1', 'D2', 'D3', 'D4')]
1414

15-
1615
columns = ('Col1', 'Col2', 'Col3', 'Col4')
1716

1817
print("Basic Table, default style (AlternatingRowGrid):")
@@ -24,10 +23,10 @@
2423

2524

2625
print("Basic Table, FancyGrid:")
27-
print(generate_table(rows, grid_style=FancyGrid))
26+
print(generate_table(rows, grid_style=tf.FancyGrid()))
2827

2928
print("Basic Table, SparseGrid:")
30-
print(generate_table(rows, grid_style=SparseGrid))
29+
print(generate_table(rows, grid_style=tf.SparseGrid()))
3130

3231
print("Table with header, AlteratingRowGrid:")
3332
print(generate_table(rows, columns))
@@ -38,7 +37,7 @@
3837

3938

4039
print("Table with header, transposed, FancyGrid:")
41-
print(generate_table(rows, columns, grid_style=FancyGrid, transpose=True))
40+
print(generate_table(rows, columns, grid_style=tf.FancyGrid(), transpose=True))
4241

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

0 commit comments

Comments
 (0)