From 922b9fc8b084f39461b7a7fbc008e857a25e16e1 Mon Sep 17 00:00:00 2001 From: Jonah Lawrence Date: Thu, 14 Oct 2021 19:21:26 +0300 Subject: [PATCH] fix: convert integers to strings to allow in tables --- table2ascii/table_to_ascii.py | 17 ++++++++++------- tests/test_alignments.py | 23 ++++++++++++++++++++++- tests/test_convert.py | 22 +++++++++++++++++++++- 3 files changed, 53 insertions(+), 9 deletions(-) diff --git a/table2ascii/table_to_ascii.py b/table2ascii/table_to_ascii.py index c443f58..2d331ba 100644 --- a/table2ascii/table_to_ascii.py +++ b/table2ascii/table_to_ascii.py @@ -1,5 +1,5 @@ from math import ceil, floor -from typing import List, Optional, Union +from typing import Any, List, Optional, Union from .alignment import Alignment from .options import Options @@ -93,27 +93,30 @@ def __auto_column_widths(self) -> List[int]: column_widths = [] for i in range(self.__columns): # number of characters in column of i of header, each body row, and footer - header_size = len(self.__header[i]) if self.__header else 0 + header_size = len(str(self.__header[i])) if self.__header else 0 body_size = ( - map(lambda row, i=i: len(row[i]), self.__body) if self.__body else [0] + map(lambda row, i=i: len(str(row[i])), self.__body) + if self.__body + else [0] ) - footer_size = len(self.__footer[i]) if self.__footer else 0 + footer_size = len(str(self.__footer[i])) if self.__footer else 0 # get the max and add 2 for padding each side with a space column_widths.append(max(header_size, *body_size, footer_size) + 2) return column_widths - def __pad(self, text: str, width: int, alignment: Alignment) -> str: + def __pad(self, cell_value: Any, width: int, alignment: Alignment) -> str: """ Pad a string of text to a given width with specified alignment Args: - text (str): The text to pad + cell_value (Any): The text in the cell to pad width (int): The width in characters to pad to alignment (Alignment): The alignment to use Returns: str: The padded text """ + text = str(cell_value) if alignment == Alignment.LEFT: # pad with spaces on the end return f" {text} " + (" " * (width - len(text) - 2)) @@ -152,7 +155,7 @@ def __row_to_ascii( if isinstance(filler, str) # otherwise, use the column content else self.__pad( - str(filler[i]), self.__column_widths[i], self.__alignments[i] + filler[i], self.__column_widths[i], self.__alignments[i] ) ) # column seperator diff --git a/tests/test_alignments.py b/tests/test_alignments.py index de34d37..f2754ae 100644 --- a/tests/test_alignments.py +++ b/tests/test_alignments.py @@ -1,4 +1,4 @@ -from table2ascii import table2ascii as t2a, Alignment +from table2ascii import alignment, table2ascii as t2a, Alignment import pytest @@ -44,3 +44,24 @@ def test_invalid_alignments(): first_col_heading=True, alignments=[9999, -1, Alignment.RIGHT, Alignment.CENTER, Alignment.RIGHT], ) + + +def test_alignment_numeric_data(): + text = t2a( + header=[1, "G", "H", "R", "S"], + body=[[1, 2, 3, 4, 5]], + footer=["A", "B", 1, 2, 3], + column_widths=[4, 5, 5, 4, 5], + alignments=[Alignment.RIGHT] + [Alignment.CENTER] * 4, + first_col_heading=True, + ) + expected = ( + "╔════╦══════════════════════╗\n" + "║ 1 ║ G H R S ║\n" + "╟────╫──────────────────────╢\n" + "║ 1 ║ 2 3 4 5 ║\n" + "╟────╫──────────────────────╢\n" + "║ A ║ B 1 2 3 ║\n" + "╚════╩══════════════════════╝" + ) + assert text == expected diff --git a/tests/test_convert.py b/tests/test_convert.py index 9142b49..e549392 100644 --- a/tests/test_convert.py +++ b/tests/test_convert.py @@ -1,4 +1,4 @@ -from table2ascii import table2ascii as t2a +from table2ascii import alignment, table2ascii as t2a import pytest @@ -179,3 +179,23 @@ def test_empty_body(): "╚═══╩═══════════════╝" ) assert text == expected + + +def test_numeric_data(): + text = t2a( + header=[1, "G", "H", "R", "S"], + body=[[1, 2, 3, 4, 5]], + footer=["A", "B", 1, 2, 3], + column_widths=[4, 5, 5, 4, 5], + first_col_heading=True, + ) + expected = ( + "╔════╦══════════════════════╗\n" + "║ 1 ║ G H R S ║\n" + "╟────╫──────────────────────╢\n" + "║ 1 ║ 2 3 4 5 ║\n" + "╟────╫──────────────────────╢\n" + "║ A ║ B 1 2 3 ║\n" + "╚════╩══════════════════════╝" + ) + assert text == expected