Mercurial > hg > TextShaper
comparison textshaper/tablify.py @ 35:5ffa0dbcb7fe
from k0s.org/hg/config
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Thu, 13 Mar 2014 13:53:41 -0700 |
parents | |
children | 8cf9a26b9aaa |
comparison
equal
deleted
inserted
replaced
34:88a69d587326 | 35:5ffa0dbcb7fe |
---|---|
1 def format_cols(rows, header=None, right_align=()): | |
2 if header: | |
3 raise NotImplementedError('TODO') # -> record TODO items | |
4 if not rows: | |
5 return # XXX header | |
6 assert len(set([len(row) for row in rows])) == 1 | |
7 rows = [[str(col) for col in row] | |
8 for row in rows] | |
9 lengths = [max([len(row[i]) for row in rows]) | |
10 for i in range(len(rows[0]))] | |
11 rows = [[col + ' '*(length-len(col)) for col, length in zip(row, lengths)] | |
12 for row in rows] | |
13 return rows | |
14 | |
15 | |
16 def tablify(table_lines, header=True): | |
17 """=> HTML table""" | |
18 table = '<table>\n' | |
19 if header: | |
20 tag, invtag = '<th> ', ' </th>' | |
21 else: | |
22 tag, invtag = '<td> ', ' </td>' | |
23 if not hasattr(table_lines, '__iter__'): | |
24 table_lines = ( table_lines, ) | |
25 for i in table_lines: | |
26 table += '<tr>' | |
27 if not hasattr(i, '__iter__'): | |
28 i = (i,) | |
29 for j in i: | |
30 table += tag + str(j) + invtag | |
31 table += '</tr>\n' | |
32 tag = '<td> ' | |
33 invtag = ' </td>' | |
34 table += '</table>' | |
35 return table |