Skip to content

Commit

Permalink
Add max_precision argument to writer class constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
thombashi committed Jul 18, 2021
1 parent 07947de commit aca3cc3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pytablewriter/writer/_table_writer.py
Expand Up @@ -42,6 +42,9 @@ class AbstractTableWriter(TableWriterInterface, metaclass=abc.ABCMeta):
"""
An abstract base class of table writer classes.
Args:
max_precision (int): Maximum decimal places for real number values.
.. py:attribute:: stream
Stream to write tables.
Expand Down Expand Up @@ -240,7 +243,7 @@ def __init__(self, **kwargs) -> None:

self._use_default_header = False

self._dp_extractor = DataPropertyExtractor()
self._dp_extractor = DataPropertyExtractor(max_precision=kwargs.get("max_precision"))
self._dp_extractor.min_column_width = 1
self._dp_extractor.strip_str_header = '"'
self._dp_extractor.preprocessor = Preprocessor(strip_str='"')
Expand Down
27 changes: 27 additions & 0 deletions test/writer/text/test_unicode_writer.py
Expand Up @@ -86,6 +86,33 @@ def test_normal_numbers(self):
print_test_result(expected=expected, actual=output)
assert output == expected

def test_normal_max_precision(self):
writer = UnicodeTableWriter(
headers=["realnumber", "long"],
value_matrix=[
["0.000000000000001", "1,000,000,000,000"],
["1", "1"],
],
margin=1,
max_precision=3,
)

expected = dedent(
"""\
┌────────────┬───────────────┐
│ realnumber │ long │
├────────────┼───────────────┤
│ 0.000 │ 1000000000000 │
├────────────┼───────────────┤
│ 1.000 │ 1 │
└────────────┴───────────────┘
"""
)

output = writer.dumps()
print_test_result(expected=expected, actual=output)
assert output == expected


class Test_BoldUnicodeTableWriter_write_table:
def test_normal_styles(self, capsys):
Expand Down

0 comments on commit aca3cc3

Please sign in to comment.