` characters and at most one decimal point.
+ If not specified or set to :py:obj:`None`, numbers will be aligned based on the ``alignments`` argument.
+ Defaults to :py:obj:`None`.
+
+ .. versionadded:: 1.1.0
cell_padding: The minimum number of spaces to add between the cell content and the column
separator. If set to ``0``, the cell content will be flush against the column separator.
Defaults to ``1``.
@@ -608,6 +718,8 @@ def table2ascii(
zero-width space, etc.), whereas :func:`len` determines the width solely based on the number of
characters in the string. Defaults to :py:obj:`True`.
+ .. versionadded:: 1.0.0
+
Returns:
The generated ASCII table
"""
@@ -620,6 +732,7 @@ def table2ascii(
last_col_heading=last_col_heading,
column_widths=column_widths,
alignments=alignments,
+ number_alignments=number_alignments,
cell_padding=cell_padding,
style=style,
use_wcwidth=use_wcwidth,
diff --git a/tests/test_alignments.py b/tests/test_alignments.py
index 3e8b874..67aa5d3 100644
--- a/tests/test_alignments.py
+++ b/tests/test_alignments.py
@@ -1,6 +1,6 @@
import pytest
-from table2ascii import Alignment, table2ascii as t2a
+from table2ascii import Alignment, PresetStyle, table2ascii as t2a
from table2ascii.exceptions import AlignmentCountMismatchError, InvalidAlignmentError
@@ -25,7 +25,7 @@ def test_first_left_four_right():
assert text == expected
-def test_wrong_number_alignments():
+def test_wrong_number_of_alignments():
with pytest.raises(AlignmentCountMismatchError):
t2a(
header=["#", "G", "H", "R", "S"],
@@ -97,3 +97,96 @@ def test_alignments_multiline_data():
"╚═══════════════════════════════════════════╝"
)
assert text == expected
+
+
+def test_decimal_alignment():
+ text = t2a(
+ header=["1.1.1", "G", "Long Header", "H", "R", "3.8"],
+ body=[[100.00001, 2, 3.14, 33, "AB", "1.5"], [10.0001, 22.0, 2.718, 3, "CD", "3.03"]],
+ footer=[10000.01, "123", 10.0, 0, "E", "A"],
+ alignments=[Alignment.DECIMAL] * 6,
+ first_col_heading=True,
+ style=PresetStyle.double_thin_box,
+ )
+ expected = (
+ "╔═════════════╦═══════╤═════════════╤════╤════╤═════════╗\n"
+ "║ 1.1.1 ║ G │ Long Header │ H │ R │ 3.8 ║\n"
+ "╠═════════════╬═══════╪═════════════╪════╪════╪═════════╣\n"
+ "║ 100.00001 ║ 2 │ 3.14 │ 33 │ AB │ 1.5 ║\n"
+ "╟─────────────╫───────┼─────────────┼────┼────┼─────────╢\n"
+ "║ 10.0001 ║ 22.0 │ 2.718 │ 3 │ CD │ 3.03 ║\n"
+ "╠═════════════╬═══════╪═════════════╪════╪════╪═════════╣\n"
+ "║ 10000.01 ║ 123 │ 10.0 │ 0 │ E │ A ║\n"
+ "╚═════════════╩═══════╧═════════════╧════╧════╧═════════╝"
+ )
+ assert text == expected
+
+
+def test_single_decimal_alignment():
+ text = t2a(
+ header=["1.1.1", "G", "Long Header"],
+ body=[[100.00001, 2, 3.14], [10.0001, 22.0, 2.718]],
+ alignments=Alignment.DECIMAL,
+ )
+ expected = (
+ "╔════════════════════════════════╗\n"
+ "║ 1.1.1 G Long Header ║\n"
+ "╟────────────────────────────────╢\n"
+ "║ 100.00001 2 3.14 ║\n"
+ "║ 10.0001 22.0 2.718 ║\n"
+ "╚════════════════════════════════╝"
+ )
+ assert text == expected
+
+
+def test_single_left_alignment():
+ text = t2a(
+ header=["1.1.1", "G", "Long Header"],
+ body=[[100.00001, 2, 3.14], [10.0001, 22.0, 2.718]],
+ alignments=Alignment.LEFT,
+ )
+ expected = (
+ "╔════════════════════════════════╗\n"
+ "║ 1.1.1 G Long Header ║\n"
+ "╟────────────────────────────────╢\n"
+ "║ 100.00001 2 3.14 ║\n"
+ "║ 10.0001 22.0 2.718 ║\n"
+ "╚════════════════════════════════╝"
+ )
+ assert text == expected
+
+
+def test_number_alignments():
+ text = t2a(
+ header=["1.1.1", "G", "Long Header", "Another Long Header"],
+ body=[[100.00001, 2, 3.14, 6.28], [10.0001, 22.0, 2.718, 1.618]],
+ alignments=[Alignment.LEFT, Alignment.RIGHT, Alignment.CENTER, Alignment.RIGHT],
+ number_alignments=[Alignment.DECIMAL, Alignment.LEFT, Alignment.RIGHT, Alignment.DECIMAL],
+ )
+ expected = (
+ "╔══════════════════════════════════════════════════════╗\n"
+ "║ 1.1.1 G Long Header Another Long Header ║\n"
+ "╟──────────────────────────────────────────────────────╢\n"
+ "║ 100.00001 2 3.14 6.28 ║\n"
+ "║ 10.0001 22.0 2.718 1.618 ║\n"
+ "╚══════════════════════════════════════════════════════╝"
+ )
+ assert text == expected
+
+
+def test_single_number_alignments():
+ text = t2a(
+ header=["1.1.1", "G", "Long Header", "S"],
+ body=[[100.00001, 2, 3.14, 6.28], [10.0001, 22.0, 2.718, 1.618]],
+ alignments=[Alignment.LEFT, Alignment.CENTER, Alignment.CENTER, Alignment.RIGHT],
+ number_alignments=Alignment.RIGHT,
+ )
+ expected = (
+ "╔════════════════════════════════════════╗\n"
+ "║ 1.1.1 G Long Header S ║\n"
+ "╟────────────────────────────────────────╢\n"
+ "║ 100.00001 2 3.14 6.28 ║\n"
+ "║ 10.0001 22.0 2.718 1.618 ║\n"
+ "╚════════════════════════════════════════╝"
+ )
+ assert text == expected
pFad - Phonifier reborn
Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies:
Alternative Proxy
pFad Proxy
pFad v3 Proxy
pFad v4 Proxy