@@ -12,27 +12,30 @@ class TableToAscii:
1212
1313 def __init__ (
1414 self ,
15- header_row : Optional [List ],
15+ header : Optional [List ],
1616 body : Optional [List [List ]],
17- footer_row : Optional [List ],
17+ footer : Optional [List ],
1818 ):
1919 """Validate arguments and initialize fields"""
2020 # 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 ):
2222 raise ValueError ("Header row and footer row must have the same length" )
2323 # 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 ]):
2525 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" )
2629 # check if any rows in body have a different number of columns
2730 if body and len (body ) and tuple (filter (lambda r : len (r ) != len (body [0 ]), body )):
2831 raise ValueError ("All rows in body must have the same length" )
2932
3033 # initialize fields
31- self .__header_row = header_row
34+ self .__header = header
3235 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 ()
3639
3740 """
3841 ╔═════╦═══════════════════════╗ ABBBBBCBBBBBDBBBBBDBBBBBDBBBBBE
@@ -69,30 +72,30 @@ def __init__(
6972 "bottom_right_corner" : "╝" , # V
7073 }
7174
72- def count_columns (self ) -> int :
75+ def __count_columns (self ) -> int :
7376 """Get the number of columns in the table
7477 based on the provided header, footer, and body lists.
7578 """
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 )
8083 if self .__body and len (self .__body ) > 0 :
8184 return len (self .__body [0 ])
8285 return 0
8386
84- def get_column_widths (self ) -> List [int ]:
87+ def __get_column_widths (self ) -> List [int ]:
8588 """Get the minimum number of characters needed for the values
8689 in each column in the table with 1 space of padding on each side.
8790 """
8891 col_counts = []
8992 for i in range (self .__columns ):
9093 # 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
9295 body_size = (
9396 map (lambda row , i = i : len (row [i ]), self .__body ) if self .__body else [0 ]
9497 )
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
9699 # get the max and add 2 for padding each side with a space
97100 col_counts .append (max (header_size , * body_size , footer_size ) + 2 )
98101 return col_counts
@@ -176,7 +179,7 @@ def __header_row_to_ascii(self) -> str:
176179 first_col_sep = self .__parts ["first_col_sep" ],
177180 column_seperator = self .__parts ["middle_edge" ],
178181 right_edge = self .__parts ["left_and_right_edge" ],
179- filler = self .__header_row ,
182+ filler = self .__header ,
180183 )
181184
182185 def __footer_row_to_ascii (self ) -> str :
@@ -186,7 +189,7 @@ def __footer_row_to_ascii(self) -> str:
186189 first_col_sep = self .__parts ["first_col_sep" ],
187190 column_seperator = self .__parts ["middle_edge" ],
188191 right_edge = self .__parts ["left_and_right_edge" ],
189- filler = self .__footer_row ,
192+ filler = self .__footer ,
190193 )
191194
192195 def __header_sep_to_ascii (self ) -> str :
@@ -225,14 +228,14 @@ def to_ascii(self) -> str:
225228 # top row of table
226229 table = self .__top_edge_to_ascii ()
227230 # add table header
228- if self .__header_row :
231+ if self .__header :
229232 table += self .__header_row_to_ascii ()
230233 table += self .__header_sep_to_ascii ()
231234 # add table body
232235 if self .__body :
233236 table += self .__body_to_ascii ()
234237 # add table footer
235- if self .__footer_row :
238+ if self .__footer :
236239 table += self .__footer_sep_to_ascii ()
237240 table += self .__footer_row_to_ascii ()
238241 # bottom row of table
@@ -242,15 +245,15 @@ def to_ascii(self) -> str:
242245
243246
244247def table2ascii (
245- header_row : Optional [List ] = None ,
248+ header : Optional [List ] = None ,
246249 body : Optional [List [List ]] = None ,
247- footer_row : Optional [List ] = None ,
250+ footer : Optional [List ] = None ,
248251) -> str :
249252 """Convert a 2D Python table to ASCII text
250253
251254 ### 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
253256 :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
255258 """
256- return TableToAscii (header_row , body , footer_row ).to_ascii ()
259+ return TableToAscii (header , body , footer ).to_ascii ()
0 commit comments