Skip to content

Commit

Permalink
Merge pull request #633 from orsharir/master
Browse files Browse the repository at this point in the history
Add a global ‘max_precision’ argument to print_table
  • Loading branch information
nbedi committed Sep 21, 2016
2 parents 6c5f82a + b350cd0 commit 79c121f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ agate is made by a community. The following individuals have contributed code, d
* `Ben Welsh <https://github.com/palewire>`_
* `Kevin Schaul <https://github.com/kevinschaul>`_
* `Eli Murray <https://github.com/ejmurra>`_
* `Or Sharir <https://github.com/orsharir>`_
15 changes: 13 additions & 2 deletions agate/table/print_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from agate import utils


def print_table(self, max_rows=20, max_columns=6, output=sys.stdout, max_column_width=20, locale=None):
def print_table(self, max_rows=20, max_columns=6, output=sys.stdout, max_column_width=20, locale=None, max_precision=3):
"""
Print a text-based view of the data in this table.
Expand All @@ -31,13 +31,20 @@ def print_table(self, max_rows=20, max_columns=6, output=sys.stdout, max_column_
:param locale:
Provide a locale you would like to be used to format the output.
By default it will use the system's setting.
:max_precision:
Puts a limit on the maximum precision displayed for number types.
Numbers with lesser precision won't be affected.
This defaults to :code:`3`. Pass :code:`None` to disable limit.
"""
if max_rows is None:
max_rows = len(self._rows)

if max_columns is None:
max_columns = len(self._columns)

if max_precision is None:
max_precision = float('inf')

ellipsis = config.get_option('ellipsis_chars')
vertical_line = config.get_option('vertical_line_char')
locale = locale or config.get_option('default_locale')
Expand Down Expand Up @@ -66,7 +73,11 @@ def print_table(self, max_rows=20, max_columns=6, output=sys.stdout, max_column_

if isinstance(c.data_type, Number):
max_places = utils.max_precision(c[:max_rows])
number_formatters.append(utils.make_number_formatter(max_places))
add_ellipsis = False
if max_places > max_precision:
add_ellipsis = True
max_places = max_precision
number_formatters.append(utils.make_number_formatter(max_places, add_ellipsis))
else:
number_formatters.append(None)

Expand Down
13 changes: 9 additions & 4 deletions agate/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,14 +190,19 @@ def max_precision(values):
return max_decimal_places


def make_number_formatter(decimal_places):
def make_number_formatter(decimal_places, add_ellipsis=False):
"""
Given a number of decimal places creates a formatting string that will
display numbers with that precision.
"""
fraction = '0' * decimal_places
return ''.join(['#,##0.', fraction, ';-#,##0.', fraction])
:param decimal_places:
The number of decimal places
:param add_ellipsis:
Optionally add an ellipsis symbol at the end of a number
"""
fraction = u'0' * decimal_places
ellipsis = u'…' if add_ellipsis else u''
return u''.join([u'#,##0.', fraction, ellipsis, u';-#,##0.', fraction, ellipsis])


def round_limits(minimum, maximum):
Expand Down
32 changes: 32 additions & 0 deletions tests/test_table/test_print_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,38 @@ def test_print_table_max_columns(self):
self.assertEqual(len(lines), 8)
self.assertEqual(len(lines[0]), 25)

def test_print_table_max_precision(self):
rows = (
('1.745', 1.745, 1.72),
('11.123456', 11.123456, 5.10),
('0', 0, 0.10)
)

column_names = ['text_number', 'real_long_number', 'real_short_number']
column_types = [
self.text_type,
self.number_type,
self.number_type
]
table = Table(rows, column_names, column_types)

output = six.StringIO()
table.print_table(output=output, max_precision=2)
lines = output.getvalue().split('\n')

# Text shouldn't be affected
self.assertIn(u' 1.745 ', lines[3])
self.assertIn(u' 11.123456 ', lines[4])
self.assertIn(u' 0 ', lines[5])
# Test real precision above max
self.assertIn(u' 1.74… ', lines[3])
self.assertIn(u' 11.12… ', lines[4])
self.assertIn(u' 0.00… ', lines[5])
# Test real precision below max
self.assertIn(u' 1.72 ', lines[3])
self.assertIn(u' 5.10 ', lines[4])
self.assertIn(u' 0.10 ', lines[5])

def test_print_table_max_column_width(self):
rows = (
('1.7', 2, 'this is long'),
Expand Down

0 comments on commit 79c121f

Please sign in to comment.