diff options
| author | robot-piglet <[email protected]> | 2026-05-30 14:30:04 +0300 |
|---|---|---|
| committer | robot-piglet <[email protected]> | 2026-05-30 14:50:29 +0300 |
| commit | fc39cd91f34215be861bfcc780748ba6d152f239 (patch) | |
| tree | f9e16f27d030e9323fe0778fd95ebeb1d2606b48 /contrib/python | |
| parent | aab3b2f0ad942839fa5475c13cb87282d5c32618 (diff) | |
Intermediate changes
commit_hash:a4cc7077fb40054b0fd58f63b8656bef9aa2b07d
Diffstat (limited to 'contrib/python')
| -rw-r--r-- | contrib/python/prettytable/py3/.dist-info/METADATA | 27 | ||||
| -rw-r--r-- | contrib/python/prettytable/py3/README.md | 21 | ||||
| -rw-r--r-- | contrib/python/prettytable/py3/prettytable/__init__.py | 4 | ||||
| -rw-r--r-- | contrib/python/prettytable/py3/prettytable/__main__.py | 47 | ||||
| -rw-r--r-- | contrib/python/prettytable/py3/prettytable/_version.py | 4 | ||||
| -rw-r--r-- | contrib/python/prettytable/py3/prettytable/prettytable.py | 88 | ||||
| -rw-r--r-- | contrib/python/prettytable/py3/tests/test_prettytable.py | 449 | ||||
| -rw-r--r-- | contrib/python/prettytable/py3/tests/test_sections.py | 55 | ||||
| -rw-r--r-- | contrib/python/prettytable/py3/tests/ya.make | 5 | ||||
| -rw-r--r-- | contrib/python/prettytable/py3/ya.make | 3 |
10 files changed, 604 insertions, 99 deletions
diff --git a/contrib/python/prettytable/py3/.dist-info/METADATA b/contrib/python/prettytable/py3/.dist-info/METADATA index e44b80cd6eb..16e1235b677 100644 --- a/contrib/python/prettytable/py3/.dist-info/METADATA +++ b/contrib/python/prettytable/py3/.dist-info/METADATA @@ -1,6 +1,6 @@ -Metadata-Version: 2.3 +Metadata-Version: 2.4 Name: prettytable -Version: 3.12.0 +Version: 3.14.0 Summary: A simple Python library for easily displaying tabular data in a visually appealing ASCII table format Project-URL: Changelog, https://github.com/prettytable/prettytable/releases Project-URL: Funding, https://tidelift.com/subscription/pkg/pypi-prettytable?utm_source=pypi-prettytable&utm_medium=pypi @@ -10,7 +10,6 @@ Author-email: Luke Maurits <[email protected]> Maintainer: Hugo van Kemenade License-Expression: BSD-3-Clause License-File: LICENSE -Classifier: License :: OSI Approved :: BSD License Classifier: Programming Language :: Python Classifier: Programming Language :: Python :: 3 :: Only Classifier: Programming Language :: Python :: 3.9 @@ -18,6 +17,7 @@ Classifier: Programming Language :: Python :: 3.10 Classifier: Programming Language :: Python :: 3.11 Classifier: Programming Language :: Python :: 3.12 Classifier: Programming Language :: Python :: 3.13 +Classifier: Programming Language :: Python :: 3.14 Classifier: Programming Language :: Python :: Implementation :: CPython Classifier: Programming Language :: Python :: Implementation :: PyPy Classifier: Topic :: Text Processing @@ -70,6 +70,14 @@ Or from `requirements.txt`: -e git://github.com/prettytable/prettytable.git#egg=prettytable +## Demo + +To see demo output, run: + +```bash +python3 -m prettytable +``` + ## Tutorial on how to use the PrettyTable API ### Getting your data into (and out of) the table @@ -415,14 +423,15 @@ instance of the data in the `sort_by` column. #### Adding sections to a table -You can divide your table into different sections using the `divider` argument. This -will add a dividing line into the table under the row who has this field set. So we can -set up a table like this: +You can divide your table into different sections using the `add_divider` method or +`divider` argument . This will add a dividing line into the table under the row who has +this field set. So we can set up a table like this: ```python table = PrettyTable() table.field_names = ["City name", "Area", "Population", "Annual Rainfall"] table.add_row(["Adelaide", 1295, 1158259, 600.5]) +table.add_divider() table.add_row(["Brisbane", 5905, 1857594, 1146.4]) table.add_row(["Darwin", 112, 120900, 1714.7]) table.add_row(["Hobart", 1357, 205556, 619.5], divider=True) @@ -438,6 +447,7 @@ to get a table like this: | City name | Area | Population | Annual Rainfall | +-----------+------+------------+-----------------+ | Adelaide | 1295 | 1158259 | 600.5 | ++-----------+------+------------+-----------------+ | Brisbane | 5905 | 1857594 | 1146.4 | | Darwin | 112 | 120900 | 1714.7 | | Hobart | 1357 | 205556 | 619.5 | @@ -464,8 +474,9 @@ You can set the style for your table using the `set_style` method before any cal `print` or `get_string`. Here's how to print a table in Markdown format: ```python -from prettytable import MARKDOWN -table.set_style(MARKDOWN) +from prettytable import TableStyle + +table.set_style(TableStyle.MARKDOWN) print(table) ``` diff --git a/contrib/python/prettytable/py3/README.md b/contrib/python/prettytable/py3/README.md index 0c89cbefbca..fb67d377942 100644 --- a/contrib/python/prettytable/py3/README.md +++ b/contrib/python/prettytable/py3/README.md @@ -38,6 +38,14 @@ Or from `requirements.txt`: -e git://github.com/prettytable/prettytable.git#egg=prettytable +## Demo + +To see demo output, run: + +```bash +python3 -m prettytable +``` + ## Tutorial on how to use the PrettyTable API ### Getting your data into (and out of) the table @@ -383,14 +391,15 @@ instance of the data in the `sort_by` column. #### Adding sections to a table -You can divide your table into different sections using the `divider` argument. This -will add a dividing line into the table under the row who has this field set. So we can -set up a table like this: +You can divide your table into different sections using the `add_divider` method or +`divider` argument . This will add a dividing line into the table under the row who has +this field set. So we can set up a table like this: ```python table = PrettyTable() table.field_names = ["City name", "Area", "Population", "Annual Rainfall"] table.add_row(["Adelaide", 1295, 1158259, 600.5]) +table.add_divider() table.add_row(["Brisbane", 5905, 1857594, 1146.4]) table.add_row(["Darwin", 112, 120900, 1714.7]) table.add_row(["Hobart", 1357, 205556, 619.5], divider=True) @@ -406,6 +415,7 @@ to get a table like this: | City name | Area | Population | Annual Rainfall | +-----------+------+------------+-----------------+ | Adelaide | 1295 | 1158259 | 600.5 | ++-----------+------+------------+-----------------+ | Brisbane | 5905 | 1857594 | 1146.4 | | Darwin | 112 | 120900 | 1714.7 | | Hobart | 1357 | 205556 | 619.5 | @@ -432,8 +442,9 @@ You can set the style for your table using the `set_style` method before any cal `print` or `get_string`. Here's how to print a table in Markdown format: ```python -from prettytable import MARKDOWN -table.set_style(MARKDOWN) +from prettytable import TableStyle + +table.set_style(TableStyle.MARKDOWN) print(table) ``` diff --git a/contrib/python/prettytable/py3/prettytable/__init__.py b/contrib/python/prettytable/py3/prettytable/__init__.py index 29ffbe8d974..66be991cb8b 100644 --- a/contrib/python/prettytable/py3/prettytable/__init__.py +++ b/contrib/python/prettytable/py3/prettytable/__init__.py @@ -34,7 +34,6 @@ __all__ = [ "ALL", "DEFAULT", "DOUBLE_BORDER", - "SINGLE_BORDER", "FRAME", "HEADER", "MARKDOWN", @@ -43,18 +42,19 @@ __all__ = [ "ORGMODE", "PLAIN_COLUMNS", "RANDOM", + "SINGLE_BORDER", "HRuleStyle", "PrettyTable", "RowType", "TableHandler", "TableStyle", "VRuleStyle", + "__version__", "from_csv", "from_db_cursor", "from_html", "from_html_one", "from_json", - "__version__", ] diff --git a/contrib/python/prettytable/py3/prettytable/__main__.py b/contrib/python/prettytable/py3/prettytable/__main__.py new file mode 100644 index 00000000000..ffd1381a652 --- /dev/null +++ b/contrib/python/prettytable/py3/prettytable/__main__.py @@ -0,0 +1,47 @@ +from __future__ import annotations + +from prettytable import PrettyTable, TableStyle +from prettytable.colortable import ColorTable, Theme, Themes + +FIELD_NAMES = ["City name", "Area", "Population", "Annual Rainfall"] +ROWS = [ + ["Adelaide", 1295, 1158259, 600.5], + ["Brisbane", 5905, 1857594, 1146.4], + ["Darwin", 112, 120900, 1714.7], + ["Hobart", 1357, 205556, 619.5], + ["Melbourne", 1566, 3806092, 646.9], + ["Perth", 5386, 1554769, 869.4], + ["Sydney", 2058, 4336374, 1214.8], +] + +if __name__ == "__main__": + table = PrettyTable() + table.field_names = FIELD_NAMES + for row in ROWS: + if row[0] == "Hobart": + table.add_row(row, divider=True) + else: + table.add_row(row) + + for style in TableStyle: + print("PrettyTable style:", style.name) + print() + table.set_style(style) + print(table) + print() + + table = ColorTable() + table.field_names = FIELD_NAMES + for row in ROWS: + if row[0] == "Hobart": + table.add_row(row, divider=True) + else: + table.add_row(row) + + for name, theme in vars(Themes).items(): + if isinstance(theme, Theme): + print("ColorTable theme:", name) + print() + table.theme = theme + print(table) + print() diff --git a/contrib/python/prettytable/py3/prettytable/_version.py b/contrib/python/prettytable/py3/prettytable/_version.py index 9e1a89958d8..431bd6d8195 100644 --- a/contrib/python/prettytable/py3/prettytable/_version.py +++ b/contrib/python/prettytable/py3/prettytable/_version.py @@ -12,5 +12,5 @@ __version__: str __version_tuple__: VERSION_TUPLE version_tuple: VERSION_TUPLE -__version__ = version = '3.12.0' -__version_tuple__ = version_tuple = (3, 12, 0) +__version__ = version = '3.14.0' +__version_tuple__ = version_tuple = (3, 14, 0) diff --git a/contrib/python/prettytable/py3/prettytable/prettytable.py b/contrib/python/prettytable/py3/prettytable/prettytable.py index 818a5203bbc..5b6c396b0d1 100644 --- a/contrib/python/prettytable/py3/prettytable/prettytable.py +++ b/contrib/python/prettytable/py3/prettytable/prettytable.py @@ -276,11 +276,6 @@ class PrettyTable: self.custom_format = {} self._style = None - if field_names: - self.field_names = field_names - else: - self._widths: list[int] = [] - # Options self._options = [ "title", @@ -329,17 +324,26 @@ class PrettyTable: "escape_header", "escape_data", ] + + self._none_format: dict[str, str | None] = {} + self._kwargs = {} + if field_names: + self.field_names = field_names + else: + self._widths: list[int] = [] + for option in self._options: if option in kwargs: self._validate_option(option, kwargs[option]) + self._kwargs[option] = kwargs[option] else: kwargs[option] = None + self._kwargs[option] = None self._title = kwargs["title"] or None self._start = kwargs["start"] or 0 self._end = kwargs["end"] or None self._fields = kwargs["fields"] or None - self._none_format: dict[str, str | None] = {} if kwargs["header"] in (True, False): self._header = kwargs["header"] @@ -372,15 +376,8 @@ class PrettyTable: self._escape_header = kwargs["escape_header"] else: self._escape_header = True - # Column specific arguments, use property.setters - self.align = kwargs["align"] or {} - self.valign = kwargs["valign"] or {} - self.max_width = kwargs["max_width"] or {} - self.min_width = kwargs["min_width"] or {} - self.int_format = kwargs["int_format"] or {} - self.float_format = kwargs["float_format"] or {} - self.custom_format = kwargs["custom_format"] or {} - self.none_format = kwargs["none_format"] or {} + + self._column_specific_args() self._min_table_width = kwargs["min_table_width"] or None self._max_table_width = kwargs["max_table_width"] or None @@ -416,6 +413,22 @@ class PrettyTable: self._xhtml = kwargs["xhtml"] or False self._attributes = kwargs["attributes"] or {} + def _column_specific_args(self): + # Column specific arguments, use property.setters + for attr in ( + "align", + "valign", + "max_width", + "min_width", + "int_format", + "float_format", + "custom_format", + "none_format", + ): + setattr( + self, attr, (self._kwargs[attr] or {}) if attr in self._kwargs else {} + ) + def _justify(self, text: str, width: int, align: AlignType) -> str: excess = width - _str_block_width(text) if align == "l": @@ -761,6 +774,9 @@ class PrettyTable: if self._field_names: old_names = self._field_names[:] self._field_names = val + + self._column_specific_args() + if self._align and old_names: for old_name, new_name in zip(old_names, val): self._align[new_name] = self._align[old_name] @@ -1441,10 +1457,9 @@ class PrettyTable: ############################## def set_style(self, style: TableStyle) -> None: + self._set_default_style() self._style = style - if style == TableStyle.DEFAULT: - self._set_default_style() - elif style == TableStyle.MSWORD_FRIENDLY: + if style == TableStyle.MSWORD_FRIENDLY: self._set_msword_style() elif style == TableStyle.PLAIN_COLUMNS: self._set_columns_style() @@ -1458,12 +1473,11 @@ class PrettyTable: self._set_single_border_style() elif style == TableStyle.RANDOM: self._set_random_style() - else: + elif style != TableStyle.DEFAULT: msg = "Invalid pre-set style" raise ValueError(msg) def _set_orgmode_style(self) -> None: - self._set_default_style() self.orgmode = True def _set_markdown_style(self) -> None: @@ -1604,6 +1618,11 @@ class PrettyTable: del self._rows[row_index] del self._dividers[row_index] + def add_divider(self) -> None: + """Add a divider to the table""" + if len(self._dividers) >= 1: + self._dividers[-1] = True + def add_column( self, fieldname: str, @@ -1646,8 +1665,8 @@ class PrettyTable: Arguments: fieldname - name of the field to contain the new column of data""" self._field_names.insert(0, fieldname) - self._align[fieldname] = self.align - self._valign[fieldname] = self.valign + self._align[fieldname] = self._kwargs["align"] or "c" + self._valign[fieldname] = self._kwargs["valign"] or "t" for i, row in enumerate(self._rows): row.insert(0, i + 1) @@ -1990,7 +2009,7 @@ class PrettyTable: for row, divider in zip(formatted_rows[:-1], dividers[:-1]): lines.append(self._stringify_row(row, options, self._hrule)) if divider: - lines.append(self._stringify_hrule(options, where="bottom_")) + lines.append(self._stringify_hrule(options)) if formatted_rows: lines.append( self._stringify_row( @@ -2506,9 +2525,12 @@ class PrettyTable: if options["escape_header"]: field = escape(field) + content = field.replace("\n", linebreak) lines.append( - ' <th style="padding-left: %dem; padding-right: %dem; text-align: center">%s</th>' # noqa: E501 - % (lpad, rpad, field.replace("\n", linebreak)) + f' <th style="' + f"padding-left: {lpad}em; " + f"padding-right: {rpad}em; " + f'text-align: center">{content}</th>' ) lines.append(" </tr>") lines.append(" </thead>") @@ -2536,15 +2558,13 @@ class PrettyTable: if options["escape_data"]: datum = escape(datum) + content = datum.replace("\n", linebreak) lines.append( - ' <td style="padding-left: %dem; padding-right: %dem; text-align: %s; vertical-align: %s">%s</td>' # noqa: E501 - % ( - lpad, - rpad, - align, - valign, - datum.replace("\n", linebreak), - ) + f' <td style="' + f"padding-left: {lpad}em; " + f"padding-right: {rpad}em; " + f"text-align: {align}; " + f'vertical-align: {valign}">{content}</td>' ) lines.append(" </tr>") lines.append(" </tbody>") @@ -2856,7 +2876,7 @@ def from_html_one(html_code: str, **kwargs) -> PrettyTable: def _warn_deprecation(name: str, module_globals: dict[str, Any]) -> Any: if (val := module_globals.get(f"_DEPRECATED_{name}")) is None: - msg = f"module '{__name__}' has no attribute '{name}" + msg = f"module '{__name__}' has no attribute '{name}'" raise AttributeError(msg) module_globals[name] = val if name in {"FRAME", "ALL", "NONE", "HEADER"}: diff --git a/contrib/python/prettytable/py3/tests/test_prettytable.py b/contrib/python/prettytable/py3/tests/test_prettytable.py index 75254ca89a4..e4777eb665e 100644 --- a/contrib/python/prettytable/py3/tests/test_prettytable.py +++ b/contrib/python/prettytable/py3/tests/test_prettytable.py @@ -788,6 +788,40 @@ class TestSorting: """.strip() ) + def test_sort_key_at_class_declaration(self) -> None: + # Test sorting by length of city name + def key(vals): + vals[0] = len(vals[0]) + return vals + + table = PrettyTable( + field_names=["City name", "Area", "Population", "Annual Rainfall"], + sortby="City name", + sort_key=key, + ) + assert table.sort_key == key + table.add_row(["Adelaide", 1295, 1158259, 600.5]) + table.add_row(["Brisbane", 5905, 1857594, 1146.4]) + table.add_row(["Darwin", 112, 120900, 1714.7]) + table.add_row(["Hobart", 1357, 205556, 619.5]) + table.add_row(["Sydney", 2058, 4336374, 1214.8]) + table.add_row(["Melbourne", 1566, 3806092, 646.9]) + table.add_row(["Perth", 5386, 1554769, 869.4]) + assert ( + """+-----------+------+------------+-----------------+ +| City name | Area | Population | Annual Rainfall | ++-----------+------+------------+-----------------+ +| Perth | 5386 | 1554769 | 869.4 | +| Darwin | 112 | 120900 | 1714.7 | +| Hobart | 1357 | 205556 | 619.5 | +| Sydney | 2058 | 4336374 | 1214.8 | +| Adelaide | 1295 | 1158259 | 600.5 | +| Brisbane | 5905 | 1857594 | 1146.4 | +| Melbourne | 1566 | 3806092 | 646.9 | ++-----------+------+------------+-----------------+""" + == table.get_string().strip() + ) + def test_sort_slice(self) -> None: """Make sure sorting and slicing interact in the expected way""" table = PrettyTable(["Foo"]) @@ -800,6 +834,37 @@ class TestSorting: assert "10" not in oldstyle assert "20" in oldstyle + def test_sortby_at_class_declaration(self) -> None: + """ + Fix #354 where initialization of a table with sortby fails + """ + table = PrettyTable( + field_names=["City name", "Area", "Population", "Annual Rainfall"], + sortby="Area", + ) + assert table.sortby == "Area" + table.add_row(["Adelaide", 1295, 1158259, 600.5]) + table.add_row(["Brisbane", 5905, 1857594, 1146.4]) + table.add_row(["Darwin", 112, 120900, 1714.7]) + table.add_row(["Hobart", 1357, 205556, 619.5]) + table.add_row(["Sydney", 2058, 4336374, 1214.8]) + table.add_row(["Melbourne", 1566, 3806092, 646.9]) + table.add_row(["Perth", 5386, 1554769, 869.4]) + assert ( + """+-----------+------+------------+-----------------+ +| City name | Area | Population | Annual Rainfall | ++-----------+------+------------+-----------------+ +| Darwin | 112 | 120900 | 1714.7 | +| Adelaide | 1295 | 1158259 | 600.5 | +| Hobart | 1357 | 205556 | 619.5 | +| Melbourne | 1566 | 3806092 | 646.9 | +| Sydney | 2058 | 4336374 | 1214.8 | +| Perth | 5386 | 1554769 | 869.4 | +| Brisbane | 5905 | 1857594 | 1146.4 | ++-----------+------+------------+-----------------+""" + == table.get_string().strip() + ) + @pytest.fixture(scope="function") def float_pt() -> PrettyTable: @@ -1713,6 +1778,134 @@ class TestStyle: t.set_style(HRuleStyle.ALL) # type: ignore[arg-type] @pytest.mark.parametrize( + "original_style,style, expected", + [ + pytest.param( + TableStyle.MARKDOWN, + TableStyle.DEFAULT, + """ ++---+---------+---------+---------+ +| | Field 1 | Field 2 | Field 3 | ++---+---------+---------+---------+ +| 1 | value 1 | value2 | value3 | +| 4 | value 4 | value5 | value6 | +| 7 | value 7 | value8 | value9 | ++---+---------+---------+---------+ +""", + id="DEFAULT", + ), + pytest.param( + TableStyle.MSWORD_FRIENDLY, + TableStyle.MARKDOWN, + """ +| | Field 1 | Field 2 | Field 3 | +| :-: | :-----: | :-----: | :-----: | +| 1 | value 1 | value2 | value3 | +| 4 | value 4 | value5 | value6 | +| 7 | value 7 | value8 | value9 | +""", + id="MARKDOWN", + ), + pytest.param( + TableStyle.MARKDOWN, + TableStyle.MSWORD_FRIENDLY, + """ +| | Field 1 | Field 2 | Field 3 | +| 1 | value 1 | value2 | value3 | +| 4 | value 4 | value5 | value6 | +| 7 | value 7 | value8 | value9 | +""", + id="MSWORD_FRIENDLY", + ), + pytest.param( + TableStyle.MARKDOWN, + TableStyle.ORGMODE, + """ +|---+---------+---------+---------| +| | Field 1 | Field 2 | Field 3 | +|---+---------+---------+---------| +| 1 | value 1 | value2 | value3 | +| 4 | value 4 | value5 | value6 | +| 7 | value 7 | value8 | value9 | +|---+---------+---------+---------| +""", + id="ORGMODE", + ), + pytest.param( + TableStyle.MARKDOWN, + TableStyle.PLAIN_COLUMNS, + """ + Field 1 Field 2 Field 3 +1 value 1 value2 value3 +4 value 4 value5 value6 +7 value 7 value8 value9 +""", # noqa: W291 + id="PLAIN_COLUMNS", + ), + pytest.param( + TableStyle.MARKDOWN, + TableStyle.RANDOM, + """ +'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^' +% 1 value 1 value2 value3% +% 4 value 4 value5 value6% +% 7 value 7 value8 value9% +'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^' +""", + id="RANDOM", + ), + pytest.param( + TableStyle.MARKDOWN, + TableStyle.DOUBLE_BORDER, + """ +╔═══╦═════════╦═════════╦═════════╗ +║ ║ Field 1 ║ Field 2 ║ Field 3 ║ +╠═══╬═════════╬═════════╬═════════╣ +║ 1 ║ value 1 ║ value2 ║ value3 ║ +║ 4 ║ value 4 ║ value5 ║ value6 ║ +║ 7 ║ value 7 ║ value8 ║ value9 ║ +╚═══╩═════════╩═════════╩═════════╝ +""", + id="DOUBLE_BORDER", + ), + pytest.param( + TableStyle.MARKDOWN, + TableStyle.SINGLE_BORDER, + """ +┌───┬─────────┬─────────┬─────────┐ +│ │ Field 1 │ Field 2 │ Field 3 │ +├───┼─────────┼─────────┼─────────┤ +│ 1 │ value 1 │ value2 │ value3 │ +│ 4 │ value 4 │ value5 │ value6 │ +│ 7 │ value 7 │ value8 │ value9 │ +└───┴─────────┴─────────┴─────────┘ +""", + id="SINGLE_BORDER", + ), + ], + ) + def test_style_reset(self, original_style, style, expected) -> None: + """ + Testing to ensure that default styling is reset between changes + of styles on a PrettyTable + + Args: + style (str): Style to be used (Default, markdown, etc) + expected (str): The expected format of style as a string representation + """ + # Arrange + t = helper_table() + random.seed(1234) + + # Act + t.set_style(original_style) + t.set_style(style) + + # Assert + result = t.get_string() + assert result.strip() == expected.strip() + + @pytest.mark.parametrize( "style, expected", [ pytest.param( @@ -2354,6 +2547,169 @@ class TestMaxTableWidth: +---+-----------------+---+-----------------+---+-----------------+""".strip() ) + def test_table_width_on_init_wo_columns(self) -> None: + """See also #272""" + table = PrettyTable(max_width=10) + table.add_row( + [ + "Lorem", + "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam ", + "ipsum", + "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam ", + "dolor", + "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam ", + ] + ) + + assert ( + table.get_string().strip() + == """ ++---------+------------+---------+------------+---------+------------+ +| Field 1 | Field 2 | Field 3 | Field 4 | Field 5 | Field 6 | ++---------+------------+---------+------------+---------+------------+ +| Lorem | Lorem | ipsum | Lorem | dolor | Lorem | +| | ipsum | | ipsum | | ipsum | +| | dolor sit | | dolor sit | | dolor sit | +| | amet, | | amet, | | amet, | +| | consetetur | | consetetur | | consetetur | +| | sadipscing | | sadipscing | | sadipscing | +| | elitr, sed | | elitr, sed | | elitr, sed | +| | diam | | diam | | diam | ++---------+------------+---------+------------+---------+------------+""".strip() + ) + + def test_table_width_on_init_with_columns(self) -> None: + """See also #272""" + table = PrettyTable( + ["Field 1", "Field 2", "Field 3", "Field 4", "Field 5", "Field 6"], + max_width=10, + ) + table.add_row( + [ + "Lorem", + "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam ", + "ipsum", + "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam ", + "dolor", + "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam ", + ] + ) + + assert ( + table.get_string().strip() + == """ ++---------+------------+---------+------------+---------+------------+ +| Field 1 | Field 2 | Field 3 | Field 4 | Field 5 | Field 6 | ++---------+------------+---------+------------+---------+------------+ +| Lorem | Lorem | ipsum | Lorem | dolor | Lorem | +| | ipsum | | ipsum | | ipsum | +| | dolor sit | | dolor sit | | dolor sit | +| | amet, | | amet, | | amet, | +| | consetetur | | consetetur | | consetetur | +| | sadipscing | | sadipscing | | sadipscing | +| | elitr, sed | | elitr, sed | | elitr, sed | +| | diam | | diam | | diam | ++---------+------------+---------+------------+---------+------------+""".strip() + ) + + def test_table_minwidth_on_init_with_columns(self) -> None: + table = PrettyTable(["Field 1", "Field 2"], min_width=20) + table.add_row( + [ + "Lorem", + "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam ", + ] + ) + + assert ( + table.get_string().strip() + == """+----------------------+--------------------------------------------------------------------+ +| Field 1 | Field 2 | ++----------------------+--------------------------------------------------------------------+ +| Lorem | Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam | ++----------------------+--------------------------------------------------------------------+""" # noqa: E501 + ) + + def test_table_min_max_width_on_init_with_columns(self) -> None: + table = PrettyTable(["Field 1", "Field 2"], min_width=20, max_width=40) + table.add_row( + [ + "Lorem", + "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam ", + ] + ) + + assert ( + table.get_string().strip() + == """+----------------------+------------------------------------------+ +| Field 1 | Field 2 | ++----------------------+------------------------------------------+ +| Lorem | Lorem ipsum dolor sit amet, consetetur | +| | sadipscing elitr, sed diam | ++----------------------+------------------------------------------+""" + ) + + def test_table_float_formatting_on_init_wo_columns(self) -> None: + """See also #243""" + table = prettytable.PrettyTable(float_format="10.2") + table.field_names = ["Metric", "Initial sol.", "Best sol."] + table.add_rows([["foo", 1.0 / 3.0, 1.0 / 3.0]]) + + assert ( + table.get_string().strip() + == """ ++--------+--------------+------------+ +| Metric | Initial sol. | Best sol. | ++--------+--------------+------------+ +| foo | 0.33 | 0.33 | ++--------+--------------+------------+""".strip() + ) + + def test_table_formatted_html_autoindex(self) -> None: + """See also #199""" + table = PrettyTable(["Field 1", "Field 2", "Field 3"]) + for row in range(1, 3 * 3, 3): + table.add_row( + [f"value {row*100}", f"value {row+1*100}", f"value {row+2*100}"] + ) + table.format = True + table.add_autoindex("I") + + assert ( + table.get_html_string().strip() + == """ +<table frame="box" rules="cols"> + <thead> + <tr> + <th style="padding-left: 1em; padding-right: 1em; text-align: center">I</th> + <th style="padding-left: 1em; padding-right: 1em; text-align: center">Field 1</th> + <th style="padding-left: 1em; padding-right: 1em; text-align: center">Field 2</th> + <th style="padding-left: 1em; padding-right: 1em; text-align: center">Field 3</th> + </tr> + </thead> + <tbody> + <tr> + <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">1</td> + <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value 100</td> + <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value 101</td> + <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value 201</td> + </tr> + <tr> + <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">2</td> + <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value 400</td> + <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value 104</td> + <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value 204</td> + </tr> + <tr> + <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">3</td> + <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value 700</td> + <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value 107</td> + <td style="padding-left: 1em; padding-right: 1em; text-align: center; vertical-align: top">value 207</td> + </tr> + </tbody> +</table>""".strip() # noqa: E501 + ) + def test_max_table_width_wide_vrules_frame(self) -> None: table = PrettyTable() table.max_table_width = 52 @@ -2419,52 +2775,59 @@ class TestMaxTableWidth: ) -class TestRowEndSection: - def test_row_end_section(self) -> None: - table = PrettyTable() - v = 1 - for row in range(4): - if row % 2 == 0: - table.add_row( - [f"value {v}", f"value{v+1}", f"value{v+2}"], divider=True - ) - else: - table.add_row( - [f"value {v}", f"value{v+1}", f"value{v+2}"], divider=False - ) - v += 3 - table.del_row(0) +class TestFields: + def test_fields_at_class_declaration(self) -> None: + table = PrettyTable( + field_names=["City name", "Area", "Population", "Annual Rainfall"], + fields=["City name", "Annual Rainfall"], + ) + table.add_row(["Adelaide", 1295, 1158259, 600.5]) + table.add_row(["Brisbane", 5905, 1857594, 1146.4]) + table.add_row(["Darwin", 112, 120900, 1714.7]) + table.add_row(["Hobart", 1357, 205556, 619.5]) + table.add_row(["Sydney", 2058, 4336374, 1214.8]) + table.add_row(["Melbourne", 1566, 3806092, 646.9]) + table.add_row(["Perth", 5386, 1554769, 869.4]) assert ( - table.get_string().strip() - == """ -+----------+---------+---------+ -| Field 1 | Field 2 | Field 3 | -+----------+---------+---------+ -| value 4 | value5 | value6 | -| value 7 | value8 | value9 | -+----------+---------+---------+ -| value 10 | value11 | value12 | -+----------+---------+---------+ -""".strip() + """+-----------+-----------------+ +| City name | Annual Rainfall | ++-----------+-----------------+ +| Adelaide | 600.5 | +| Brisbane | 1146.4 | +| Darwin | 1714.7 | +| Hobart | 619.5 | +| Sydney | 1214.8 | +| Melbourne | 646.9 | +| Perth | 869.4 | ++-----------+-----------------+""" + == table.get_string().strip() ) - -class TestClearing: - def test_clear_rows(self, row_prettytable: PrettyTable) -> None: - t = helper_table() - t.add_row([0, "a", "b", "c"], divider=True) - t.clear_rows() - assert t.rows == [] - assert t.dividers == [] - assert t.field_names == ["", "Field 1", "Field 2", "Field 3"] - - def test_clear(self, row_prettytable: PrettyTable) -> None: - t = helper_table() - t.add_row([0, "a", "b", "c"], divider=True) - t.clear() - assert t.rows == [] - assert t.dividers == [] - assert t.field_names == [] + def test_fields(self) -> None: + table = PrettyTable() + table.field_names = ["City name", "Area", "Population", "Annual Rainfall"] + table.fields = ["City name", "Annual Rainfall"] + table.add_row(["Adelaide", 1295, 1158259, 600.5]) + table.add_row(["Brisbane", 5905, 1857594, 1146.4]) + table.add_row(["Darwin", 112, 120900, 1714.7]) + table.add_row(["Hobart", 1357, 205556, 619.5]) + table.add_row(["Sydney", 2058, 4336374, 1214.8]) + table.add_row(["Melbourne", 1566, 3806092, 646.9]) + table.add_row(["Perth", 5386, 1554769, 869.4]) + assert ( + """+-----------+-----------------+ +| City name | Annual Rainfall | ++-----------+-----------------+ +| Adelaide | 600.5 | +| Brisbane | 1146.4 | +| Darwin | 1714.7 | +| Hobart | 619.5 | +| Sydney | 1214.8 | +| Melbourne | 646.9 | +| Perth | 869.4 | ++-----------+-----------------+""" + == table.get_string().strip() + ) class TestPreservingInternalBorders: diff --git a/contrib/python/prettytable/py3/tests/test_sections.py b/contrib/python/prettytable/py3/tests/test_sections.py new file mode 100644 index 00000000000..12c56d0021b --- /dev/null +++ b/contrib/python/prettytable/py3/tests/test_sections.py @@ -0,0 +1,55 @@ +from __future__ import annotations + +from test_prettytable import helper_table + +from prettytable import PrettyTable, TableStyle + + +class TestRowEndSection: + EXPECTED_RESULT = """ +┌──────────┬──────────┬──────────┐ +│ Field 1 │ Field 2 │ Field 3 │ +├──────────┼──────────┼──────────┤ +│ value 4 │ value 5 │ value 6 │ +│ value 7 │ value 8 │ value 9 │ +├──────────┼──────────┼──────────┤ +│ value 10 │ value 11 │ value 12 │ +└──────────┴──────────┴──────────┘ +""".strip() + + def test_row_end_section_via_argument(self) -> None: + table = PrettyTable() + table.set_style(TableStyle.SINGLE_BORDER) + table.add_row(["value 4", "value 5", "value 6"]) + table.add_row(["value 7", "value 8", "value 9"], divider=True) + table.add_row(["value 10", "value 11", "value 12"]) + + assert table.get_string().strip() == self.EXPECTED_RESULT + + def test_row_end_section_via_method(self) -> None: + table = PrettyTable() + table.set_style(TableStyle.SINGLE_BORDER) + table.add_row(["value 4", "value 5", "value 6"]) + table.add_row(["value 7", "value 8", "value 9"]) + table.add_divider() + table.add_row(["value 10", "value 11", "value 12"]) + + assert table.get_string().strip() == self.EXPECTED_RESULT + + +class TestClearing: + def test_clear_rows(self) -> None: + t = helper_table() + t.add_row([0, "a", "b", "c"], divider=True) + t.clear_rows() + assert t.rows == [] + assert t.dividers == [] + assert t.field_names == ["", "Field 1", "Field 2", "Field 3"] + + def test_clear(self) -> None: + t = helper_table() + t.add_row([0, "a", "b", "c"], divider=True) + t.clear() + assert t.rows == [] + assert t.dividers == [] + assert t.field_names == [] diff --git a/contrib/python/prettytable/py3/tests/ya.make b/contrib/python/prettytable/py3/tests/ya.make index 0a69a3b7a8c..8375d213ed4 100644 --- a/contrib/python/prettytable/py3/tests/ya.make +++ b/contrib/python/prettytable/py3/tests/ya.make @@ -5,10 +5,7 @@ PEERDIR( contrib/python/pytest-lazy-fixtures ) -TEST_SRCS( - test_colortable.py - test_prettytable.py -) +ALL_PYTEST_SRCS(RECURSIVE) NO_LINT() diff --git a/contrib/python/prettytable/py3/ya.make b/contrib/python/prettytable/py3/ya.make index 6f495203a6b..0ed2577e62c 100644 --- a/contrib/python/prettytable/py3/ya.make +++ b/contrib/python/prettytable/py3/ya.make @@ -2,7 +2,7 @@ PY3_LIBRARY() -VERSION(3.12.0) +VERSION(3.14.0) LICENSE(BSD-3-Clause) @@ -15,6 +15,7 @@ NO_LINT() PY_SRCS( TOP_LEVEL prettytable/__init__.py + prettytable/__main__.py prettytable/_version.py prettytable/colortable.py prettytable/prettytable.py |
