From a03fdeb5df1bb38f822c1a78b3cb5393932ef5a7 Mon Sep 17 00:00:00 2001 From: Jakob Scheid Date: Mon, 9 Feb 2026 18:57:33 +0100 Subject: [PATCH] =?UTF-8?q?=09ge=C3=A4ndert:=20=20=20=20=20=20=20README.md?= =?UTF-8?q?=20=09ge=C3=A4ndert:=20=20=20=20=20=20=20pyproject.toml=20=09ge?= =?UTF-8?q?=C3=A4ndert:=20=20=20=20=20=20=20src/text=5Ftable/=5Fcore.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 17 ++++++++++++++++- pyproject.toml | 2 +- src/text_table/_core.py | 19 +++++++++++++------ 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 36920be..78b3246 100644 --- a/README.md +++ b/README.md @@ -12,4 +12,19 @@ pip install text-table --index-url https://jcloud-services.ddns.net/simple/ ## Usage - `table`: The function which creates a text table from the params. - `TableBorderCharset`: A class for the charset of a table border. -- `BORDER_THIN`, `BORDER_THICK`, `BORDER_DOUBLE`, `BORDER_ROUND`: Prepared border charsets. \ No newline at end of file +- `BORDER_THIN`, `BORDER_THICK`, `BORDER_DOUBLE`, `BORDER_ROUND`: Prepared border charsets. + + +## Changelog + +### Version 0.1.3 +- Added support for objects with custom length and correct formatting of these + +### Version 0.1.2 +- Corrected type hints mistake + +### Version 0.1.1 +- Renamed functions + +### Version 0.1.0 +- First release \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index 54bb250..2e0f45a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,6 +4,6 @@ build-backend = "setuptools.build_meta" [project] name = "text_table" -version = "0.1.1" +version = "0.1.3" description = "A simple library for creating text tables." readme = "README.md" \ No newline at end of file diff --git a/src/text_table/_core.py b/src/text_table/_core.py index 973ad32..6e0cac3 100644 --- a/src/text_table/_core.py +++ b/src/text_table/_core.py @@ -87,9 +87,16 @@ def _get_max_widths(data: Sequence[dict]) -> dict: max_widths = {} for row in data: for col in row: - max_widths[col] = max(len(str(row[col])), max_widths.get(col, 0)) + max_widths[col] = max(_len(row[col]), max_widths.get(col, 0)) return max_widths +def _len(obj, /) -> int: + try: + len(obj) + return len(obj) + except: + return len(str(obj)) + 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. @@ -113,19 +120,19 @@ def table(data: Sequence[dict], *, borders: bool = False, border_charset: TableB table = f'''{(column_space * ' ').join([(c.upper() if uppercase_column_headers else c) + ' ' * (column_widths[c] - len(c)) for c in columns])}''' table = '' for col in columns: - column_widths[col] = max(column_widths[col], len(col)) + column_widths[col] = max(column_widths[col], _len(col)) for col in columns: table += col.upper() if uppercase_column_headers else col - table += ' ' * (column_widths[col] - len(col)) + table += ' ' * (column_widths[col] - _len(col)) table += ' ' * column_space if borders: table += border_charset.line_vertical + ' ' for row in data: table += '\n' for column in columns: - value = str(row.get(column, '')) - table += value - table += ' ' * (column_widths[column] - len(str(value))) + value = row.get(column, '') + table += str(value) + table += ' ' * (column_widths[column] - _len(value)) table += column_space * ' ' if borders: table += border_charset.line_vertical + ' '