@@ -12,27 +12,30 @@ class TableToAscii:
12
12
13
13
def __init__ (
14
14
self ,
15
- header_row : Optional [List ],
15
+ header : Optional [List ],
16
16
body : Optional [List [List ]],
17
- footer_row : Optional [List ],
17
+ footer : Optional [List ],
18
18
):
19
19
"""Validate arguments and initialize fields"""
20
20
# check if columns in header are different from footer
21
- if header_row and footer_row and len (header_row ) != len (footer_row ):
21
+ if header and footer and len (header ) != len (footer ):
22
22
raise ValueError ("Header row and footer row must have the same length" )
23
23
# check if columns in header are different from body
24
- if header_row and body and len (body ) > 0 and len (header_row ) != len (body [0 ]):
24
+ if header and body and len (body ) > 0 and len (header ) != len (body [0 ]):
25
25
raise ValueError ("Header row and body rows must have the same length" )
26
+ # check if columns in header are different from body
27
+ if footer and body and len (body ) > 0 and len (footer ) != len (body [0 ]):
28
+ raise ValueError ("Footer row and body rows must have the same length" )
26
29
# check if any rows in body have a different number of columns
27
30
if body and len (body ) and tuple (filter (lambda r : len (r ) != len (body [0 ]), body )):
28
31
raise ValueError ("All rows in body must have the same length" )
29
32
30
33
# initialize fields
31
- self .__header_row = header_row
34
+ self .__header = header
32
35
self .__body = body
33
- self .__footer_row = footer_row
34
- self .__columns = self .count_columns ()
35
- self .__cell_widths = self .get_column_widths ()
36
+ self .__footer = footer
37
+ self .__columns = self .__count_columns ()
38
+ self .__cell_widths = self .__get_column_widths ()
36
39
37
40
"""
38
41
╔═════╦═══════════════════════╗ ABBBBBCBBBBBDBBBBBDBBBBBDBBBBBE
@@ -69,30 +72,30 @@ def __init__(
69
72
"bottom_right_corner" : "╝" , # V
70
73
}
71
74
72
- def count_columns (self ) -> int :
75
+ def __count_columns (self ) -> int :
73
76
"""Get the number of columns in the table
74
77
based on the provided header, footer, and body lists.
75
78
"""
76
- if self .__header_row :
77
- return len (self .__header_row )
78
- if self .__footer_row :
79
- return len (self .__footer_row )
79
+ if self .__header :
80
+ return len (self .__header )
81
+ if self .__footer :
82
+ return len (self .__footer )
80
83
if self .__body and len (self .__body ) > 0 :
81
84
return len (self .__body [0 ])
82
85
return 0
83
86
84
- def get_column_widths (self ) -> List [int ]:
87
+ def __get_column_widths (self ) -> List [int ]:
85
88
"""Get the minimum number of characters needed for the values
86
89
in each column in the table with 1 space of padding on each side.
87
90
"""
88
91
col_counts = []
89
92
for i in range (self .__columns ):
90
93
# number of characters in column of i of header, each body row, and footer
91
- header_size = len (self .__header_row [i ]) if self .__header_row else 0
94
+ header_size = len (self .__header [i ]) if self .__header else 0
92
95
body_size = (
93
96
map (lambda row , i = i : len (row [i ]), self .__body ) if self .__body else [0 ]
94
97
)
95
- footer_size = len (self .__footer_row [i ]) if self .__footer_row else 0
98
+ footer_size = len (self .__footer [i ]) if self .__footer else 0
96
99
# get the max and add 2 for padding each side with a space
97
100
col_counts .append (max (header_size , * body_size , footer_size ) + 2 )
98
101
return col_counts
@@ -176,7 +179,7 @@ def __header_row_to_ascii(self) -> str:
176
179
first_col_sep = self .__parts ["first_col_sep" ],
177
180
column_seperator = self .__parts ["middle_edge" ],
178
181
right_edge = self .__parts ["left_and_right_edge" ],
179
- filler = self .__header_row ,
182
+ filler = self .__header ,
180
183
)
181
184
182
185
def __footer_row_to_ascii (self ) -> str :
@@ -186,7 +189,7 @@ def __footer_row_to_ascii(self) -> str:
186
189
first_col_sep = self .__parts ["first_col_sep" ],
187
190
column_seperator = self .__parts ["middle_edge" ],
188
191
right_edge = self .__parts ["left_and_right_edge" ],
189
- filler = self .__footer_row ,
192
+ filler = self .__footer ,
190
193
)
191
194
192
195
def __header_sep_to_ascii (self ) -> str :
@@ -225,14 +228,14 @@ def to_ascii(self) -> str:
225
228
# top row of table
226
229
table = self .__top_edge_to_ascii ()
227
230
# add table header
228
- if self .__header_row :
231
+ if self .__header :
229
232
table += self .__header_row_to_ascii ()
230
233
table += self .__header_sep_to_ascii ()
231
234
# add table body
232
235
if self .__body :
233
236
table += self .__body_to_ascii ()
234
237
# add table footer
235
- if self .__footer_row :
238
+ if self .__footer :
236
239
table += self .__footer_sep_to_ascii ()
237
240
table += self .__footer_row_to_ascii ()
238
241
# bottom row of table
@@ -242,15 +245,15 @@ def to_ascii(self) -> str:
242
245
243
246
244
247
def table2ascii (
245
- header_row : Optional [List ] = None ,
248
+ header : Optional [List ] = None ,
246
249
body : Optional [List [List ]] = None ,
247
- footer_row : Optional [List ] = None ,
250
+ footer : Optional [List ] = None ,
248
251
) -> str :
249
252
"""Convert a 2D Python table to ASCII text
250
253
251
254
### Arguments
252
- :param header_row : :class:`Optional[List]` List of column values in the table's header row
255
+ :param header : :class:`Optional[List]` List of column values in the table's header row
253
256
:param body: :class:`Optional[List[List]]` 2-dimensional list of values in the table's body
254
- :param footer_row : :class:`Optional[List]` List of column values in the table's footer row
257
+ :param footer : :class:`Optional[List]` List of column values in the table's footer row
255
258
"""
256
- return TableToAscii (header_row , body , footer_row ).to_ascii ()
259
+ return TableToAscii (header , body , footer ).to_ascii ()
0 commit comments