From a4098fbce1b8c5fbd0a3be60a02373e088dab545 Mon Sep 17 00:00:00 2001 From: Jonah Lawrence Date: Fri, 16 Dec 2022 16:40:48 -0700 Subject: [PATCH 1/2] feat: add invalid column width error --- table2ascii/__init__.py | 2 +- table2ascii/exceptions.py | 20 +++++++++++++++++--- table2ascii/table_to_ascii.py | 3 +++ tests/test_column_widths.py | 8 ++++++-- 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/table2ascii/__init__.py b/table2ascii/__init__.py index c70aad5..ebd9744 100644 --- a/table2ascii/__init__.py +++ b/table2ascii/__init__.py @@ -8,7 +8,7 @@ from .table_style import TableStyle from .table_to_ascii import table2ascii -__version__ = "1.0.1" +__version__ = "1.0.2" __all__ = [ "Alignment", diff --git a/table2ascii/exceptions.py b/table2ascii/exceptions.py index 86be92a..84b31db 100644 --- a/table2ascii/exceptions.py +++ b/table2ascii/exceptions.py @@ -171,7 +171,7 @@ class ColumnWidthTooSmallError(TableOptionError): min_width (int): The minimum width that is allowed """ - def __init__(self, column_index: int, column_width: int, min_width: int): + def __init__(self, column_index: int, column_width: int, min_width: int | None = None): self.column_index = column_index self.column_width = column_width self.min_width = min_width @@ -179,12 +179,26 @@ def __init__(self, column_index: int, column_width: int, min_width: int): def _message(self) -> str: return ( - f"Column width too small: The column width for column index {self.column_index} " - f" of `column_widths` is {self.column_width}, but the minimum width " + f"Column width too small: The column width for index {self.column_index} " + f"of `column_widths` is {self.column_width}, but the minimum width " f"required to display the content is {self.min_width}." ) +class InvalidColumnWidthError(ColumnWidthTooSmallError): + """Exception raised when the column width is invalid + + This class is a subclass of :class:`ColumnWidthTooSmallError`. + """ + + def _message(self) -> str: + return ( + f"Invalid column width: The column width for index {self.column_index} " + f"of `column_widths` is {self.column_width}, but the column width " + f"must be a positive integer." + ) + + class InvalidAlignmentError(TableOptionError): """Exception raised when an invalid value is passed for an :class:`Alignment` diff --git a/table2ascii/table_to_ascii.py b/table2ascii/table_to_ascii.py index 1b3a523..8116467 100644 --- a/table2ascii/table_to_ascii.py +++ b/table2ascii/table_to_ascii.py @@ -16,6 +16,7 @@ FooterColumnCountMismatchError, InvalidAlignmentError, InvalidCellPaddingError, + InvalidColumnWidthError, NoHeaderBodyOrFooterError, ) from .merge import Merge @@ -150,6 +151,8 @@ def __calculate_column_widths( minimum = column_widths[i] if option is None: option = minimum + elif option < 0: + raise InvalidColumnWidthError(i, option) elif option < minimum: raise ColumnWidthTooSmallError(i, option, minimum) column_widths[i] = option diff --git a/tests/test_column_widths.py b/tests/test_column_widths.py index 5a3f1ba..55840eb 100644 --- a/tests/test_column_widths.py +++ b/tests/test_column_widths.py @@ -1,7 +1,11 @@ import pytest from table2ascii import table2ascii as t2a -from table2ascii.exceptions import ColumnWidthsCountMismatchError, ColumnWidthTooSmallError +from table2ascii.exceptions import ( + ColumnWidthsCountMismatchError, + ColumnWidthTooSmallError, + InvalidColumnWidthError, +) def test_column_widths(): @@ -83,7 +87,7 @@ def test_wrong_number_column_widths(): def test_negative_column_widths(): - with pytest.raises(ColumnWidthTooSmallError): + with pytest.raises(InvalidColumnWidthError): t2a( header=["#", "G", "H", "R", "S"], body=[["1", "30", "40", "35", "30"], ["2", "30", "40", "35", "30"]], From 481ccf7f0ddfade38090ff89aacf9541553f41c5 Mon Sep 17 00:00:00 2001 From: Jonah Lawrence Date: Sat, 17 Dec 2022 17:58:30 -0700 Subject: [PATCH 2/2] Documentation, wording --- docs/source/api.rst | 2 ++ table2ascii/exceptions.py | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/docs/source/api.rst b/docs/source/api.rst index 6b16e63..170e480 100644 --- a/docs/source/api.rst +++ b/docs/source/api.rst @@ -57,6 +57,8 @@ Exceptions .. autoexception:: table2ascii.exceptions.ColumnWidthTooSmallError +.. autoexception:: table2ascii.exceptions.InvalidColumnWidthError + .. autoexception:: table2ascii.exceptions.InvalidAlignmentError .. autoexception:: table2ascii.exceptions.TableStyleTooLongError diff --git a/table2ascii/exceptions.py b/table2ascii/exceptions.py index 84b31db..0c57134 100644 --- a/table2ascii/exceptions.py +++ b/table2ascii/exceptions.py @@ -156,7 +156,10 @@ def __init__(self, padding: int): super().__init__(self._message()) def _message(self) -> str: - return f"Invalid cell padding: {self.padding} is not a positive integer." + return ( + f"Invalid cell padding: The cell padding provided was {self.padding} " + f"but it must be a non-negative integer." + ) class ColumnWidthTooSmallError(TableOptionError): 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