33
44from .alignment import Alignment
55from .options import Options
6+ from .preset_style import PresetStyle
7+ from .table_style import TableStyle
68
79
810class TableToAscii :
911 """Class used to convert a 2D Python table to ASCII text"""
1012
11- def __init__ (self , header : List , body : List [List ], footer : List , options : Options ):
13+ def __init__ (
14+ self ,
15+ header : Optional [List [Any ]],
16+ body : Optional [List [List [Any ]]],
17+ footer : Optional [List [Any ]],
18+ options : Options ,
19+ ):
1220 """
1321 Validate arguments and initialize fields
1422
1523 Args:
16- header (List): The values in the header of the table
17- body (List[List]): The rows of values in the body of the table
18- footer (List): The values in the footer of the table
24+ header (Optional[ List[Any]] ): The values in the header of the table
25+ body (Optional[ List[List[Any]] ]): The rows of values in the body of the table
26+ footer (Optional[ List[Any]] ): The values in the footer of the table
1927 options (Options): The options for the table
2028 """
2129 # initialize fields
@@ -31,9 +39,7 @@ def __init__(self, header: List, body: List[List], footer: List, options: Option
3139
3240 # check if footer has a different number of columns
3341 if footer and len (footer ) != self .__columns :
34- raise ValueError (
35- "Footer must have the same number of columns as the other rows"
36- )
42+ raise ValueError ("Footer must have the same number of columns as the other rows" )
3743 # check if any rows in body have a different number of columns
3844 if body and any (len (row ) != self .__columns for row in body ):
3945 raise ValueError (
@@ -45,9 +51,7 @@ def __init__(self, header: List, body: List[List], footer: List, options: Option
4551 if options .column_widths :
4652 # check that the right number of columns were specified
4753 if len (options .column_widths ) != self .__columns :
48- raise ValueError (
49- "Length of `column_widths` list must equal the number of columns"
50- )
54+ raise ValueError ("Length of `column_widths` list must equal the number of columns" )
5155 # check that each column is at least as large as the minimum size
5256 for i in range (len (options .column_widths )):
5357 option = options .column_widths [i ]
@@ -62,9 +66,7 @@ def __init__(self, header: List, body: List[List], footer: List, options: Option
6266
6367 # check if alignments specified have a different number of columns
6468 if options .alignments and len (options .alignments ) != self .__columns :
65- raise ValueError (
66- "Length of `alignments` list must equal the number of columns"
67- )
69+ raise ValueError ("Length of `alignments` list must equal the number of columns" )
6870
6971 def __count_columns (self ) -> int :
7072 """
@@ -101,9 +103,7 @@ def longest_line(text: str) -> int:
101103 # number of characters in column of i of header, each body row, and footer
102104 header_size = longest_line (str (self .__header [i ])) if self .__header else 0
103105 body_size = (
104- map (lambda row , i = i : longest_line (str (row [i ])), self .__body )
105- if self .__body
106- else [0 ]
106+ map (lambda row , i = i : longest_line (str (row [i ])), self .__body ) if self .__body else [0 ]
107107 )
108108 footer_size = longest_line (str (self .__footer [i ])) if self .__footer else 0
109109 # get the max and add 2 for padding each side with a space
@@ -256,7 +256,7 @@ def __heading_sep_to_ascii(self) -> str:
256256 filler = self .__style .heading_row_sep ,
257257 )
258258
259- def __body_to_ascii (self ) -> str :
259+ def __body_to_ascii (self , body : List [ List [ Any ]] ) -> str :
260260 """
261261 Assembles the body of the ascii table
262262
@@ -278,7 +278,7 @@ def __body_to_ascii(self) -> str:
278278 right_edge = self .__style .left_and_right_edge ,
279279 filler = row ,
280280 )
281- for row in self . __body
281+ for row in body
282282 )
283283
284284 def to_ascii (self ) -> str :
@@ -296,7 +296,7 @@ def to_ascii(self) -> str:
296296 table += self .__heading_sep_to_ascii ()
297297 # add table body
298298 if self .__body :
299- table += self .__body_to_ascii ()
299+ table += self .__body_to_ascii (self . __body )
300300 # add table footer
301301 if self .__footer :
302302 table += self .__heading_sep_to_ascii ()
@@ -308,25 +308,41 @@ def to_ascii(self) -> str:
308308
309309
310310def table2ascii (
311- header : Optional [List ] = None ,
312- body : Optional [List [List ]] = None ,
313- footer : Optional [List ] = None ,
314- ** options ,
311+ header : Optional [List [Any ]] = None ,
312+ body : Optional [List [List [Any ]]] = None ,
313+ footer : Optional [List [Any ]] = None ,
314+ * ,
315+ first_col_heading : bool = False ,
316+ last_col_heading : bool = False ,
317+ column_widths : Optional [List [int ]] = None ,
318+ alignments : Optional [List [Alignment ]] = None ,
319+ style : TableStyle = PresetStyle .double_thin_compact ,
315320) -> str :
316321 """
317322 Convert a 2D Python table to ASCII text
318323
319324 Args:
320- header (:class:`Optional[List]`): List of column values in the table's header row
321- body (:class:`Optional[List[List]]`): 2-dimensional list of values in the table's body
322- footer (:class:`Optional[List]`): List of column values in the table's footer row
323- style (:class:`TableStyle`): Table style to use for styling (preset styles can be imported)
324- column_widths (:class:`List[int]`): List of widths in characters for each column (defaults to auto-sizing)
325- alignments (:class:`List[Alignment]`): List of alignments (ex. `[Alignment.LEFT, Alignment.CENTER, Alignment.RIGHT]`)
325+ header (:class:`Optional[List[Any]]`): List of column values in the table's header row
326+ body (:class:`Optional[List[List[Any]]]`): 2-dimensional list of values in the table's body
327+ footer (:class:`Optional[List[Any]]`): List of column values in the table's footer row
326328 first_col_heading (:class:`bool`): Whether to add a header column separator after the first column
327329 last_col_heading (:class:`bool`): Whether to add a header column separator before the last column
330+ column_widths (:class:`List[int]`): List of widths in characters for each column (defaults to auto-sizing)
331+ alignments (:class:`List[Alignment]`): List of alignments (ex. `[Alignment.LEFT, Alignment.CENTER, Alignment.RIGHT]`)
332+ style (:class:`TableStyle`): Table style to use for styling (preset styles can be imported)
328333
329334 Returns:
330335 str: The generated ASCII table
331336 """
332- return TableToAscii (header , body , footer , Options (** options )).to_ascii ()
337+ return TableToAscii (
338+ header ,
339+ body ,
340+ footer ,
341+ Options (
342+ first_col_heading = first_col_heading ,
343+ last_col_heading = last_col_heading ,
344+ column_widths = column_widths ,
345+ alignments = alignments ,
346+ style = style ,
347+ ),
348+ ).to_ascii ()
0 commit comments