Skip to content

Commit

Permalink
Small improvements (#328)
Browse files Browse the repository at this point in the history
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
  • Loading branch information
cdce8p and hugovk authored Oct 16, 2024
1 parent d80f523 commit 66c884f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 21 deletions.
35 changes: 24 additions & 11 deletions src/prettytable/prettytable.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ class PrettyTable:
_xhtml: bool
_align: dict[str, AlignType]
_valign: dict[str, VAlignType]
_min_width: dict[str, int]
_max_width: dict[str, int]
_min_table_width: int | None
_max_table_width: int | None
_fields: Sequence[str | None] | None
Expand Down Expand Up @@ -132,6 +134,7 @@ class PrettyTable:
_escape_data: bool
_style: TableStyle | None
orgmode: bool
_widths: list[int]
_hrule: str

def __init__(self, field_names: Sequence[str] | None = None, **kwargs) -> None:
Expand Down Expand Up @@ -239,7 +242,6 @@ def __init__(self, field_names: Sequence[str] | None = None, **kwargs) -> None:
"horizontal_align_char",
"junction_char",
"header_style",
"valign",
"xhtml",
"print_empty",
"oldsortslice",
Expand Down Expand Up @@ -269,7 +271,7 @@ def __init__(self, field_names: Sequence[str] | None = None, **kwargs) -> None:
self._start = kwargs["start"] or 0
self._end = kwargs["end"] or None
self._fields = kwargs["fields"] or None
self._none_format: dict[None, None] = {}
self._none_format: dict[str, str | None] = {}

if kwargs["header"] in (True, False):
self._header = kwargs["header"]
Expand Down Expand Up @@ -1687,9 +1689,11 @@ def _compute_widths(self, rows: list[list[str]], options) -> None:
for row in rows:
for index, value in enumerate(row):
fieldname = self.field_names[index]
if self.none_format.get(fieldname) is not None:
if value == "None" or value is None:
value = self.none_format.get(fieldname)
if (
value == "None"
and (none_val := self.none_format.get(fieldname)) is not None
):
value = none_val
if fieldname in self.max_width:
widths[index] = max(
widths[index],
Expand Down Expand Up @@ -1934,7 +1938,9 @@ def get_string(self, **kwargs) -> str:

return "\n".join(lines)

def _stringify_hrule(self, options, where: str = "") -> str:
def _stringify_hrule(
self, options, where: Literal["top_", "bottom_", ""] = ""
) -> str:
if not options["border"] and not options["preserve_internal_border"]:
return ""
lpad, rpad = self._get_padding_widths(options)
Expand Down Expand Up @@ -1974,7 +1980,7 @@ def _stringify_hrule(self, options, where: str = "") -> str:
return "".join(bits)

def _stringify_title(self, title: str, options) -> str:
lines = []
lines: list[str] = []
lpad, rpad = self._get_padding_widths(options)
if options["border"]:
if options["vrules"] == ALL:
Expand Down Expand Up @@ -2075,10 +2081,13 @@ def _stringify_row(self, row: list[str], options, hrule: str) -> str:
):
# Enforce max widths
lines = value.split("\n")
new_lines = []
new_lines: list[str] = []
for line in lines:
if line == "None" and self.none_format.get(field) is not None:
line = self.none_format[field]
if (
line == "None"
and (none_val := self.none_format.get(field)) is not None
):
line = none_val
if _str_block_width(line) > width:
line = textwrap.fill(line, width)
new_lines.append(line)
Expand Down Expand Up @@ -2220,7 +2229,11 @@ def get_json_string(self, **kwargs) -> str:
import json

options = self._get_options(kwargs)
json_options: Any = {"indent": 4, "separators": (",", ": "), "sort_keys": True}
json_options: dict[str, Any] = {
"indent": 4,
"separators": (",", ": "),
"sort_keys": True,
}
json_options.update(
{key: value for key, value in kwargs.items() if key not in options}
)
Expand Down
20 changes: 10 additions & 10 deletions tests/test_prettytable.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,22 +314,22 @@ def field_name_less_table() -> PrettyTable:
class TestFieldNameLessTable:
"""Make sure that building and stringing a table with no fieldnames works fine"""

def test_can_string_ascii(self, field_name_less_table: prettytable) -> None:
def test_can_string_ascii(self, field_name_less_table: PrettyTable) -> None:
output = field_name_less_table.get_string()
assert "| Field 1 | Field 2 | Field 3 | Field 4 |" in output
assert "| Adelaide | 1295 | 1158259 | 600.5 |" in output

def test_can_string_html(self, field_name_less_table: prettytable) -> None:
def test_can_string_html(self, field_name_less_table: PrettyTable) -> None:
output = field_name_less_table.get_html_string()
assert "<th>Field 1</th>" in output
assert "<td>Adelaide</td>" in output

def test_can_string_latex(self, field_name_less_table: prettytable) -> None:
def test_can_string_latex(self, field_name_less_table: PrettyTable) -> None:
output = field_name_less_table.get_latex_string()
assert "Field 1 & Field 2 & Field 3 & Field 4 \\\\" in output
assert "Adelaide & 1295 & 1158259 & 600.5 \\\\" in output

def test_add_field_names_later(self, field_name_less_table: prettytable) -> None:
def test_add_field_names_later(self, field_name_less_table: PrettyTable) -> None:
field_name_less_table.field_names = [
"City name",
"Area",
Expand Down Expand Up @@ -376,21 +376,21 @@ class TestAlignment:
"""Make sure alignment works regardless of when it was set"""

def test_aligned_ascii(
self, aligned_before_table: prettytable, aligned_after_table: prettytable
self, aligned_before_table: PrettyTable, aligned_after_table: PrettyTable
) -> None:
before = aligned_before_table.get_string()
after = aligned_after_table.get_string()
assert before == after

def test_aligned_html(
self, aligned_before_table: prettytable, aligned_after_table: prettytable
self, aligned_before_table: PrettyTable, aligned_after_table: PrettyTable
) -> None:
before = aligned_before_table.get_html_string()
after = aligned_after_table.get_html_string()
assert before == after

def test_aligned_latex(
self, aligned_before_table: prettytable, aligned_after_table: prettytable
self, aligned_before_table: PrettyTable, aligned_after_table: PrettyTable
) -> None:
before = aligned_before_table.get_latex_string()
after = aligned_after_table.get_latex_string()
Expand Down Expand Up @@ -428,7 +428,7 @@ def city_data_from_csv() -> PrettyTable:
class TestOptionOverride:
"""Make sure all options are properly overwritten by get_string."""

def test_border(self, city_data_prettytable: prettytable) -> None:
def test_border(self, city_data_prettytable: PrettyTable) -> None:
default = city_data_prettytable.get_string()
override = city_data_prettytable.get_string(border=False)
assert default != override
Expand Down Expand Up @@ -535,12 +535,12 @@ def test_table_rows(self, city_data_prettytable: PrettyTable) -> None:
assert len(rows) == 7
assert rows[0] == ["Adelaide", 1295, 1158259, 600.5]

def _test_no_blank_lines(self, table: prettytable) -> None:
def _test_no_blank_lines(self, table: PrettyTable) -> None:
string = table.get_string()
lines = string.split("\n")
assert "" not in lines

def _test_all_length_equal(self, table: prettytable) -> None:
def _test_all_length_equal(self, table: PrettyTable) -> None:
string = table.get_string()
lines = string.split("\n")
lengths = [len(line) for line in lines]
Expand Down

0 comments on commit 66c884f

Please sign in to comment.