3
3
4
4
from .alignment import Alignment
5
5
from .options import Options
6
+ from .preset_style import PresetStyle
7
+ from .table_style import TableStyle
6
8
7
9
8
10
class TableToAscii :
9
11
"""Class used to convert a 2D Python table to ASCII text"""
10
12
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
+ ):
12
20
"""
13
21
Validate arguments and initialize fields
14
22
15
23
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
19
27
options (Options): The options for the table
20
28
"""
21
29
# initialize fields
@@ -31,9 +39,7 @@ def __init__(self, header: List, body: List[List], footer: List, options: Option
31
39
32
40
# check if footer has a different number of columns
33
41
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" )
37
43
# check if any rows in body have a different number of columns
38
44
if body and any (len (row ) != self .__columns for row in body ):
39
45
raise ValueError (
@@ -45,9 +51,7 @@ def __init__(self, header: List, body: List[List], footer: List, options: Option
45
51
if options .column_widths :
46
52
# check that the right number of columns were specified
47
53
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" )
51
55
# check that each column is at least as large as the minimum size
52
56
for i in range (len (options .column_widths )):
53
57
option = options .column_widths [i ]
@@ -62,9 +66,7 @@ def __init__(self, header: List, body: List[List], footer: List, options: Option
62
66
63
67
# check if alignments specified have a different number of columns
64
68
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" )
68
70
69
71
def __count_columns (self ) -> int :
70
72
"""
@@ -101,9 +103,7 @@ def longest_line(text: str) -> int:
101
103
# number of characters in column of i of header, each body row, and footer
102
104
header_size = longest_line (str (self .__header [i ])) if self .__header else 0
103
105
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 ]
107
107
)
108
108
footer_size = longest_line (str (self .__footer [i ])) if self .__footer else 0
109
109
# get the max and add 2 for padding each side with a space
@@ -256,7 +256,7 @@ def __heading_sep_to_ascii(self) -> str:
256
256
filler = self .__style .heading_row_sep ,
257
257
)
258
258
259
- def __body_to_ascii (self ) -> str :
259
+ def __body_to_ascii (self , body : List [ List [ Any ]] ) -> str :
260
260
"""
261
261
Assembles the body of the ascii table
262
262
@@ -278,7 +278,7 @@ def __body_to_ascii(self) -> str:
278
278
right_edge = self .__style .left_and_right_edge ,
279
279
filler = row ,
280
280
)
281
- for row in self . __body
281
+ for row in body
282
282
)
283
283
284
284
def to_ascii (self ) -> str :
@@ -296,7 +296,7 @@ def to_ascii(self) -> str:
296
296
table += self .__heading_sep_to_ascii ()
297
297
# add table body
298
298
if self .__body :
299
- table += self .__body_to_ascii ()
299
+ table += self .__body_to_ascii (self . __body )
300
300
# add table footer
301
301
if self .__footer :
302
302
table += self .__heading_sep_to_ascii ()
@@ -308,25 +308,41 @@ def to_ascii(self) -> str:
308
308
309
309
310
310
def 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 ,
315
320
) -> str :
316
321
"""
317
322
Convert a 2D Python table to ASCII text
318
323
319
324
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
326
328
first_col_heading (:class:`bool`): Whether to add a header column separator after the first column
327
329
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)
328
333
329
334
Returns:
330
335
str: The generated ASCII table
331
336
"""
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