Add print_table.py
dblume authored 1 year ago
|
3)
4) __author__ = "David Blume"
5) __copyright__ = "Copyright 2016-2023, David Blume"
6) __license__ = "MIT"
7) __version__ = "1.0"
8) __status__ = "Development"
9)
10) def print_table(rows: Sequence[Sequence[str]],
11) underline_header: str='-',
12) col_delimiter: str="\t",
|
Add print_table.py
dblume authored 1 year ago
|
14) outside_delimiters: bool=False):
15) """Prints rows with consistent column widths."""
16) cols = zip(*rows)
17)
18) # Compute column widths by taking maximum length of values per column
19) col_widths = [max(len(value) for value in col) for col in cols]
20)
21) # Create a suitable format string for nice table output
22) fmt = col_delimiter.join(["{:<%d}" % i for i in col_widths])
23) if outside_delimiters:
24) fmt = f"{col_delimiter.lstrip()}{fmt}{col_delimiter.rstrip()}"
25)
26) # Print each row using the computed format
|
Add support for Jira-style...
dblume authored 6 months ago
|
27) if header_delimiter is not None:
28) width_diff = len(header_delimiter) - len(col_delimiter)
29) header_fmt = header_delimiter.join(["{:<%d}" % (i - width_diff) for i in col_widths])
30) if outside_delimiters:
31) header_fmt = f"{header_delimiter.lstrip()}{header_fmt}{header_delimiter.rstrip()}"
32) else:
33) header_fmt = fmt
34) print(header_fmt.format(*rows[0]))
|
Add print_table.py
dblume authored 1 year ago
|
35) if underline_header:
36) if outside_delimiters:
37) print(col_delimiter.lstrip() + col_delimiter.join([(underline_header * i) for i in col_widths]) + col_delimiter.rstrip())
38) else:
39) print(col_delimiter.join([(underline_header * i) for i in col_widths]))
40) for row in rows[1:]:
41) print(fmt.format(*row))
42)
43)
44) if __name__ == '__main__':
45) t = (("Header 1", "Header 2", "Header 3"),
46) ("Row one", "Row one, longer cell", "R1.3"),
47) ("Row two, long cell", "Row two", "R2.3"))
48) print_table(t, None)
49) print()
50) print_table(t)
51) print()
52) print_table(t, col_delimiter=" | ", outside_delimiters=True)
|