geändert: README.md
geändert: pyproject.toml geändert: src/text_table/_core.py
This commit is contained in:
@@ -13,3 +13,18 @@ pip install text-table --index-url https://jcloud-services.ddns.net/simple/
|
|||||||
- `table`: The function which creates a text table from the params.
|
- `table`: The function which creates a text table from the params.
|
||||||
- `TableBorderCharset`: A class for the charset of a table border.
|
- `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
@@ -4,6 +4,6 @@ build-backend = "setuptools.build_meta"
|
|||||||
|
|
||||||
[project]
|
[project]
|
||||||
name = "text_table"
|
name = "text_table"
|
||||||
version = "0.1.1"
|
version = "0.1.3"
|
||||||
description = "A simple library for creating text tables."
|
description = "A simple library for creating text tables."
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
+13
-6
@@ -87,9 +87,16 @@ 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(row[col]), max_widths.get(col, 0))
|
||||||
return max_widths
|
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:
|
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.
|
||||||
@@ -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 = f'''{(column_space * ' ').join([(c.upper() if uppercase_column_headers else c) + ' ' * (column_widths[c] - len(c)) for c in columns])}'''
|
||||||
table = ''
|
table = ''
|
||||||
for col in columns:
|
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:
|
for col in columns:
|
||||||
table += col.upper() if uppercase_column_headers else col
|
table += col.upper() if uppercase_column_headers else col
|
||||||
table += ' ' * (column_widths[col] - len(col))
|
table += ' ' * (column_widths[col] - _len(col))
|
||||||
table += ' ' * column_space
|
table += ' ' * column_space
|
||||||
if borders:
|
if borders:
|
||||||
table += border_charset.line_vertical + ' '
|
table += border_charset.line_vertical + ' '
|
||||||
for row in data:
|
for row in data:
|
||||||
table += '\n'
|
table += '\n'
|
||||||
for column in columns:
|
for column in columns:
|
||||||
value = str(row.get(column, ''))
|
value = row.get(column, '')
|
||||||
table += value
|
table += str(value)
|
||||||
table += ' ' * (column_widths[column] - len(str(value)))
|
table += ' ' * (column_widths[column] - _len(value))
|
||||||
table += column_space * ' '
|
table += column_space * ' '
|
||||||
if borders:
|
if borders:
|
||||||
table += border_charset.line_vertical + ' '
|
table += border_charset.line_vertical + ' '
|
||||||
|
|||||||
Reference in New Issue
Block a user