1
1
from math import ceil , floor
2
- from typing import Any , List , Optional , Union
2
+ from typing import Any , Callable , List , Optional , Union
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
@@ -85,19 +93,19 @@ def __auto_column_widths(self) -> List[int]:
85
93
List[int]: The minimum number of characters needed for each column
86
94
"""
87
95
88
- def longest_line (text : str ) -> int :
89
- """Returns the length of the longest line in a multi-line string"""
96
+ def widest_line (text : str ) -> int :
97
+ """Returns the width of the longest line in a multi-line string"""
90
98
return max (len (line ) for line in text .splitlines ()) if len (text ) else 0
91
99
92
100
column_widths = []
93
101
# get the width necessary for each column
94
102
for i in range (self .__columns ):
103
+ # col_widest returns the width of the widest line in the ith cell of a given list
104
+ col_widest : Callable [[List [Any ], int ], int ] = lambda row , i = i : widest_line (str (row [i ]))
95
105
# number of characters in column of i of header, each body row, and footer
96
- header_size = longest_line (str (self .__header [i ])) if self .__header else 0
97
- body_size = (
98
- map (lambda row , i = i : longest_line (str (row [i ])), self .__body ) if self .__body else [0 ]
99
- )
100
- footer_size = longest_line (str (self .__footer [i ])) if self .__footer else 0
106
+ header_size = col_widest (self .__header ) if self .__header else 0
107
+ body_size = map (col_widest , self .__body ) if self .__body else [0 ]
108
+ footer_size = col_widest (self .__footer ) if self .__footer else 0
101
109
# get the max and add 2 for padding each side with a space
102
110
column_widths .append (max (header_size , * body_size , footer_size ) + 2 )
103
111
return column_widths
@@ -248,7 +256,7 @@ def __heading_sep_to_ascii(self) -> str:
248
256
filler = self .__style .heading_row_sep ,
249
257
)
250
258
251
- def __body_to_ascii (self ) -> str :
259
+ def __body_to_ascii (self , body : List [ List [ Any ]] ) -> str :
252
260
"""
253
261
Assembles the body of the ascii table
254
262
@@ -270,7 +278,7 @@ def __body_to_ascii(self) -> str:
270
278
right_edge = self .__style .left_and_right_edge ,
271
279
filler = row ,
272
280
)
273
- for row in self . __body
281
+ for row in body
274
282
)
275
283
276
284
def to_ascii (self ) -> str :
@@ -288,7 +296,7 @@ def to_ascii(self) -> str:
288
296
table += self .__heading_sep_to_ascii ()
289
297
# add table body
290
298
if self .__body :
291
- table += self .__body_to_ascii ()
299
+ table += self .__body_to_ascii (self . __body )
292
300
# add table footer
293
301
if self .__footer :
294
302
table += self .__heading_sep_to_ascii ()
@@ -300,25 +308,41 @@ def to_ascii(self) -> str:
300
308
301
309
302
310
def table2ascii (
303
- header : Optional [List ] = None ,
304
- body : Optional [List [List ]] = None ,
305
- footer : Optional [List ] = None ,
306
- ** 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 ,
307
320
) -> str :
308
321
"""
309
322
Convert a 2D Python table to ASCII text
310
323
311
324
Args:
312
- header (:class:`Optional[List]`): List of column values in the table's header row
313
- body (:class:`Optional[List[List]]`): 2-dimensional list of values in the table's body
314
- footer (:class:`Optional[List]`): List of column values in the table's footer row
315
- style (:class:`TableStyle`): Table style to use for styling (preset styles can be imported)
316
- column_widths (:class:`List[int]`): List of widths in characters for each column (defaults to auto-sizing)
317
- 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
318
328
first_col_heading (:class:`bool`): Whether to add a header column separator after the first column
319
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)
320
333
321
334
Returns:
322
335
str: The generated ASCII table
323
336
"""
324
- 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