1
1
from math import ceil , floor
2
- from typing import Any , Callable , List , Optional , Union
2
+ from typing import Callable , List , Optional , Union
3
3
4
4
from .alignment import Alignment
5
+ from .annotations import SupportsStr
5
6
from .options import Options
6
7
from .preset_style import PresetStyle
7
8
from .table_style import TableStyle
@@ -12,9 +13,9 @@ class TableToAscii:
12
13
13
14
def __init__ (
14
15
self ,
15
- header : Optional [List [Any ]],
16
- body : Optional [List [List [Any ]]],
17
- footer : Optional [List [Any ]],
16
+ header : Optional [List [SupportsStr ]],
17
+ body : Optional [List [List [SupportsStr ]]],
18
+ footer : Optional [List [SupportsStr ]],
18
19
options : Options ,
19
20
):
20
21
"""
@@ -103,7 +104,9 @@ def widest_line(text: str) -> int:
103
104
# get the width necessary for each column
104
105
for i in range (self .__columns ):
105
106
# col_widest returns the width of the widest line in the ith cell of a given list
106
- col_widest : Callable [[List [Any ], int ], int ] = lambda row , i = i : widest_line (str (row [i ]))
107
+ col_widest : Callable [[List [SupportsStr ], int ], int ] = lambda row , i = i : widest_line (
108
+ str (row [i ])
109
+ )
107
110
# number of characters in column of i of header, each body row, and footer
108
111
header_size = col_widest (self .__header ) if self .__header else 0
109
112
body_size = map (col_widest , self .__body ) if self .__body else [0 ]
@@ -112,7 +115,7 @@ def widest_line(text: str) -> int:
112
115
column_widths .append (max (header_size , * body_size , footer_size ) + 2 )
113
116
return column_widths
114
117
115
- def __pad (self , cell_value : Any , width : int , alignment : Alignment ) -> str :
118
+ def __pad (self , cell_value : SupportsStr , width : int , alignment : Alignment ) -> str :
116
119
"""
117
120
Pad a string of text to a given width with specified alignment
118
121
@@ -258,7 +261,7 @@ def __heading_sep_to_ascii(self) -> str:
258
261
filler = self .__style .heading_row_sep ,
259
262
)
260
263
261
- def __body_to_ascii (self , body : List [List [Any ]]) -> str :
264
+ def __body_to_ascii (self , body : List [List [SupportsStr ]]) -> str :
262
265
"""
263
266
Assembles the body of the ascii table
264
267
@@ -310,9 +313,9 @@ def to_ascii(self) -> str:
310
313
311
314
312
315
def table2ascii (
313
- header : Optional [List [Any ]] = None ,
314
- body : Optional [List [List [Any ]]] = None ,
315
- footer : Optional [List [Any ]] = None ,
316
+ header : Optional [List [SupportsStr ]] = None ,
317
+ body : Optional [List [List [SupportsStr ]]] = None ,
318
+ footer : Optional [List [SupportsStr ]] = None ,
316
319
* ,
317
320
first_col_heading : bool = False ,
318
321
last_col_heading : bool = False ,
@@ -324,12 +327,12 @@ def table2ascii(
324
327
Convert a 2D Python table to ASCII text
325
328
326
329
Args:
327
- header: List of column values in the table's header row. If not specified,
328
- the table will not have a header row.
329
- body: 2-dimensional list of values in the table's body. If not specified,
330
- the table will not have a body.
331
- footer: List of column values in the table's footer row. If not specified,
332
- the table will not have a footer row.
330
+ header: List of column values in the table's header row. All values should be :class:`str`
331
+ or support :class:`str` conversion. If not specified, the table will not have a header row.
332
+ body: 2-dimensional list of values in the table's body. All values should be :class:`str`
333
+ or support :class:`str` conversion. If not specified, the table will not have a body.
334
+ footer: List of column values in the table's footer row. All values should be :class:`str`
335
+ or support :class:`str` conversion. If not specified, the table will not have a footer row.
333
336
first_col_heading: Whether to add a header column separator after the first column.
334
337
Defaults to :py:obj:`False`.
335
338
last_col_heading: Whether to add a header column separator before the last column.
0 commit comments