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
@@ -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