-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqualifier.py
71 lines (56 loc) · 2.57 KB
/
qualifier.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
#!/bin/env python3.9
from typing import Any, List, Optional
def make_table(rows: List[List[Any]], labels: Optional[List[Any]] = None, centered: bool = False) -> str:
"""
:param rows: 2D list containing objects that have a single-line representation (via `str`).
All rows must be of the same length.
:param labels: List containing the column labels. If present, the length must equal to that of each row.
:param centered: If the items should be aligned to the center, else they are left aligned.
:return: A table representing the rows passed in.
"""
...
def width_of_column(column_index:int) -> int:
from operator import itemgetter
column = list( map( itemgetter(column_index), rows ) )
column = list( map( str, column ) )
column_width = len(max(column, key=len))
if labels is not None:
column_width = max(column_width, len(str(labels[column_index])))
return column_width
column_widths = [width_of_column(column_index) for column_index in range(len(rows[0]))]
def spaces_in_row(row:list) -> list:
_spaces = []
for column_index in range(len(row)):
column_width = column_widths[column_index]
cell_content_width = len(str(row[column_index]))
if centered:
left_space = ( ( column_width - cell_content_width ) // 2 ) + 1
else:
left_space = 1
right_space = ( column_width - ( cell_content_width + left_space - 1 ) ) + 1
_spaces.append((left_space, right_space))
return _spaces
def tablify_row(row:list) -> str:
table_row = '│'
for (left_space,right_space),cell in zip(spaces_in_row(row), row):
table_cell = ' '*left_space + str(cell) + ' '*right_space + '│'
table_row += table_cell
table_row += '\n'
return table_row
def make_border(first_piece:str, middle_piece:str, last_piece:str):
_border = ''
for dash_line in border_dashes:
_border += dash_line + middle_piece
_border = first_piece + _border[:-1] + last_piece
return _border
border_dashes = ['─'*( width + 2 ) for width in column_widths]
top_border = make_border('┌','┬','┐\n')
bottom_border = make_border('└','┴','┘')
table = ''+top_border
if labels is not None:
label_border = make_border('├','┼','┤\n')
table += tablify_row(labels) + label_border
for row in rows:
table += tablify_row(row)
table += bottom_border
return table