Skip to content

Commit 546f773

Browse files
committed
Adding support for dictionaries as row objects. attrib will be used as the column key
1 parent 4a047d2 commit 546f773

File tree

3 files changed

+81
-14
lines changed

3 files changed

+81
-14
lines changed

examples/color.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@
2929

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

32-
print("Table with colorful alternting rows")
32+
print("Table with colorful alternating rows")
3333
print(generate_table(rows, columns, grid_style=tf.AlternatingRowGrid(BACK_GREEN, BACK_BLUE)))

examples/simple_dict.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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))

tableformatter.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -948,24 +948,30 @@ def generate_table(self, entries: Iterable[Union[Iterable, object]], force_trans
948948
entry_opts = dict()
949949
if use_attribs:
950950
# if use_attribs is set, the entries can optionally be a tuple with (object, options)
951-
try:
952-
iter(entry)
953-
except TypeError:
954-
# not iterable, so we just use the object directly
951+
if isinstance(entry, dict):
955952
entry_obj = entry
956-
if self._row_tagger is not None:
957-
entry_opts = self._row_tagger(entry_obj)
958953
else:
959-
entry_obj = entry[0]
960-
if self._row_tagger is not None:
961-
entry_opts = self._row_tagger(entry_obj)
962-
if len(entry) == 2 and isinstance(entry[1], dict):
963-
entry_opts.update(entry[1])
954+
try:
955+
iter(entry)
956+
except TypeError:
957+
# not iterable, so we just use the object directly
958+
entry_obj = entry
959+
if self._row_tagger is not None:
960+
entry_opts = self._row_tagger(entry_obj)
961+
else:
962+
entry_obj = entry[0]
963+
if self._row_tagger is not None:
964+
entry_opts = self._row_tagger(entry_obj)
965+
if len(entry) == 2 and isinstance(entry[1], dict):
966+
entry_opts.update(entry[1])
964967

965968
for column_index, attrib_name in enumerate(self._column_attribs):
966969
field_obj = None
967-
if isinstance(attrib_name, str) and hasattr(entry_obj, attrib_name):
968-
field_obj = getattr(entry_obj, attrib_name, '')
970+
if isinstance(attrib_name, str):
971+
if hasattr(entry_obj, attrib_name):
972+
field_obj = getattr(entry_obj, attrib_name, '')
973+
elif isinstance(entry_obj, dict) and attrib_name in entry_obj:
974+
field_obj = entry_obj[attrib_name]
969975
# if the object attribute is callable, go ahead and call it and get the result
970976
if callable(field_obj):
971977
field_obj = field_obj()

0 commit comments

Comments
 (0)