-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolumnsreport.py
72 lines (61 loc) · 2.47 KB
/
columnsreport.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
from collections.abc import Sequence
from dataclasses import dataclass
import dataclasses
from typing import List, Self
import unittest
from accountingsystemerror import AccountingSystemError
from reportcolumn import ReportColumn
@dataclass(frozen=True)
class ColumnsReport:
columns: tuple[ReportColumn] = tuple()
column_length: int = 0
def __post_init__(self):
if len(self.columns) == 0: return
for column in self.columns:
assert isinstance(column, ReportColumn)
expected_len = len(self.columns[0].items)
for column in self.columns:
assert expected_len == len(column.items)
def join(self, report_column: ReportColumn) -> Self:
assert isinstance(report_column, ReportColumn)
if self.column_length != 0:
assert self.column_length == len(report_column.items)
# new_columns has type tuple[ReportColumn, ReportColumn]
# new_columns = self.columns + (report_column,)
# work around the above typing error from pylance
new_columns = list(self.columns)
new_columns.append(report_column)
new_columns = tuple(new_columns)
return ColumnsReport(columns=new_columns, column_length=len(report_column.items))
# Return tuple of strings that are the report lines
def render(self, column_spacing=1) -> List[str]:
assert len(self.columns) > 0
rendered_columns = tuple(map(lambda x: x.render(), self.columns))
r = [] # each item will be a line
for row_index in range(self.column_length):
line = []
for rendered_column in rendered_columns:
if len(line) > 0: line.append(' '.rjust(column_spacing))
line.append(rendered_column[row_index])
rendered_line = ''.join(line)
r.append(rendered_line)
return r
class Test(unittest.TestCase):
def test_join_render(self):
tests = (
('left', ('header1', 101, 202, 'abc')),
('right', ('header2', 201, 202, 'abc')),
)
rc = ColumnsReport()
for test in tests:
alignment: str = test[0]
items: tuple = test[1]
next_column = ReportColumn(items, alignment)
rc = rc.join(next_column)
lines = rc.render(column_spacing=2)
if False:
for line in lines:
print(line)
self.assertTrue(isinstance(rc, ColumnsReport))
if __name__ == '__main__':
unittest.main()