geändert: src/text_table/_core.py

This commit is contained in:
2026-02-09 14:50:41 +01:00
parent 98d6a00665
commit a622448ea5
+4 -4
View File
@@ -1,3 +1,5 @@
from typing import Sequence
__all__ = [ __all__ = [
'TableBorderCharset', 'TableBorderCharset',
'BORDER_THIN', 'BORDER_THIN',
@@ -81,19 +83,18 @@ BORDER_ROUND = TableBorderCharset(
'' ''
) )
def _get_max_widths(data: dict) -> dict: def _get_max_widths(data: Sequence[dict]) -> dict:
max_widths = {} max_widths = {}
for row in data: for row in data:
for col in row: for col in row:
max_widths[col] = max(len(str(row[col])), max_widths.get(col, 0)) max_widths[col] = max(len(str(row[col])), max_widths.get(col, 0))
return max_widths return max_widths
def table(data: dict, *, borders: bool = False, border_charset: TableBorderCharset = BORDER_THIN, column_space: int = 1, uppercase_column_headers: bool = True) -> str: def table(data: Sequence[dict], *, borders: bool = False, border_charset: TableBorderCharset = BORDER_THIN, column_space: int = 1, uppercase_column_headers: bool = True) -> str:
''' '''
Creates a text table. Creates a text table.
:param data: The data. :param data: The data.
:type data: dict
:param borders: If True, the table will have borders, otherwise it will not have any borders. :param borders: If True, the table will have borders, otherwise it will not have any borders.
:type borders: bool :type borders: bool
:param border_charset: The charset for the borders. :param border_charset: The charset for the borders.
@@ -130,7 +131,6 @@ def table(data: dict, *, borders: bool = False, border_charset: TableBorderChars
table += border_charset.line_vertical + ' ' table += border_charset.line_vertical + ' '
if borders: if borders:
table_without_borders = table table_without_borders = table
table_width = max([len(r) for r in table_without_borders.split('\n')])
table = border_charset.corner_top_left table = border_charset.corner_top_left
for col in columns: for col in columns:
table += border_charset.line_horizontal * (column_widths[col] + 2) table += border_charset.line_horizontal * (column_widths[col] + 2)