geändert: README.md

geändert:       pyproject.toml
	geändert:       src/text_table/_core.py
This commit is contained in:
2026-02-09 18:57:33 +01:00
parent a622448ea5
commit a03fdeb5df
3 changed files with 30 additions and 8 deletions
+16 -1
View File
@@ -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.
- `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
+1 -1
View File
@@ -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"
+13 -6
View File
@@ -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 + ' '