-
Notifications
You must be signed in to change notification settings - Fork 6
/
table.py
88 lines (73 loc) · 3.29 KB
/
table.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# Generates tables for Doxygen flavored Markdown. See the Doxygen
# documentation for details:
# http://www.doxygen.nl/manual/markdown.html#md_tables
# Translation dictionaries for table alignment
left_rule = {"<": ":", "^": ":", ">": "-"}
right_rule = {"<": "-", "^": ":", ">": ":"}
def evalute_field(record, field_spec):
"""
Evalute a field of a record using the type of the field_spec as a guide.
"""
if type(field_spec) is int:
return str(record[field_spec])
elif type(field_spec) is str:
return str(getattr(record, field_spec))
else:
return str(field_spec(record))
def table(file, records, fields, headings, alignment=None):
"""
Generate a Doxygen-flavor Markdown table from records.
file -- Any object with a 'write' method that takes a single string
parameter.
records -- Iterable. Rows will be generated from this.
fields -- List of fields for each row. Each entry may be an integer,
string or a function. If the entry is an integer, it is assumed to be
an index of each record. If the entry is a string, it is assumed to be
a field of each record. If the entry is a function, it is called with
the record and its return value is taken as the value of the field.
headings -- List of column headings.
alignment - List of pairs alignment characters. The first of the pair
specifies the alignment of the header, (Doxygen won't respect this, but
it might look good, the second specifies the alignment of the cells in
the column.
Possible alignment characters are:
'<' = Left align (default for cells)
'>' = Right align
'^' = Center (default for column headings)
"""
num_columns = len(fields)
assert len(headings) == num_columns
# Compute the table cell data
columns = [[] for i in range(num_columns)]
for record in records:
for i, field in enumerate(fields):
columns[i].append(evalute_field(record, field))
# Fill out any missing alignment characters.
extended_align = alignment if alignment is not None else []
if len(extended_align) > num_columns:
extended_align = extended_align[0:num_columns]
elif len(extended_align) < num_columns:
extended_align += [("^", "<") for i in range(num_columns - len(extended_align))]
heading_align, cell_align = [x for x in zip(*extended_align)]
field_widths = [
len(max(column, key=len)) if len(column) > 0 else 0 for column in columns
]
heading_widths = [max(len(head), 2) for head in headings]
column_widths = [max(x) for x in zip(field_widths, heading_widths)]
_ = " | ".join(
["{:" + a + str(w) + "}" for a, w in zip(heading_align, column_widths)]
)
heading_template = "| " + _ + " |"
_ = " | ".join(["{:" + a + str(w) + "}" for a, w in zip(cell_align, column_widths)])
row_template = "| " + _ + " |"
_ = " | ".join(
[
left_rule[a] + "-" * (w - 2) + right_rule[a]
for a, w in zip(cell_align, column_widths)
]
)
ruling = "| " + _ + " |"
file.write(heading_template.format(*headings).rstrip() + "\n")
file.write(ruling.rstrip() + "\n")
for row in zip(*columns):
file.write(row_template.format(*row).rstrip() + "\n")