Skip to content

Commit 79c121f

Browse files
authored
Merge pull request wireservice#633 from orsharir/master
Add a global ‘max_precision’ argument to print_table
2 parents 6c5f82a + b350cd0 commit 79c121f

File tree

4 files changed

+55
-6
lines changed

4 files changed

+55
-6
lines changed

AUTHORS.rst

+1
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ agate is made by a community. The following individuals have contributed code, d
1717
* `Ben Welsh <https://github.com/palewire>`_
1818
* `Kevin Schaul <https://github.com/kevinschaul>`_
1919
* `Eli Murray <https://github.com/ejmurra>`_
20+
* `Or Sharir <https://github.com/orsharir>`_

agate/table/print_table.py

+13-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from agate import utils
1212

1313

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

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

45+
if max_precision is None:
46+
max_precision = float('inf')
47+
4148
ellipsis = config.get_option('ellipsis_chars')
4249
vertical_line = config.get_option('vertical_line_char')
4350
locale = locale or config.get_option('default_locale')
@@ -66,7 +73,11 @@ def print_table(self, max_rows=20, max_columns=6, output=sys.stdout, max_column_
6673

6774
if isinstance(c.data_type, Number):
6875
max_places = utils.max_precision(c[:max_rows])
69-
number_formatters.append(utils.make_number_formatter(max_places))
76+
add_ellipsis = False
77+
if max_places > max_precision:
78+
add_ellipsis = True
79+
max_places = max_precision
80+
number_formatters.append(utils.make_number_formatter(max_places, add_ellipsis))
7081
else:
7182
number_formatters.append(None)
7283

agate/utils.py

+9-4
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,19 @@ def max_precision(values):
190190
return max_decimal_places
191191

192192

193-
def make_number_formatter(decimal_places):
193+
def make_number_formatter(decimal_places, add_ellipsis=False):
194194
"""
195195
Given a number of decimal places creates a formatting string that will
196196
display numbers with that precision.
197-
"""
198-
fraction = '0' * decimal_places
199197
200-
return ''.join(['#,##0.', fraction, ';-#,##0.', fraction])
198+
:param decimal_places:
199+
The number of decimal places
200+
:param add_ellipsis:
201+
Optionally add an ellipsis symbol at the end of a number
202+
"""
203+
fraction = u'0' * decimal_places
204+
ellipsis = u'…' if add_ellipsis else u''
205+
return u''.join([u'#,##0.', fraction, ellipsis, u';-#,##0.', fraction, ellipsis])
201206

202207

203208
def round_limits(minimum, maximum):

tests/test_table/test_print_table.py

+32
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,38 @@ def test_print_table_max_columns(self):
5757
self.assertEqual(len(lines), 8)
5858
self.assertEqual(len(lines[0]), 25)
5959

60+
def test_print_table_max_precision(self):
61+
rows = (
62+
('1.745', 1.745, 1.72),
63+
('11.123456', 11.123456, 5.10),
64+
('0', 0, 0.10)
65+
)
66+
67+
column_names = ['text_number', 'real_long_number', 'real_short_number']
68+
column_types = [
69+
self.text_type,
70+
self.number_type,
71+
self.number_type
72+
]
73+
table = Table(rows, column_names, column_types)
74+
75+
output = six.StringIO()
76+
table.print_table(output=output, max_precision=2)
77+
lines = output.getvalue().split('\n')
78+
79+
# Text shouldn't be affected
80+
self.assertIn(u' 1.745 ', lines[3])
81+
self.assertIn(u' 11.123456 ', lines[4])
82+
self.assertIn(u' 0 ', lines[5])
83+
# Test real precision above max
84+
self.assertIn(u' 1.74… ', lines[3])
85+
self.assertIn(u' 11.12… ', lines[4])
86+
self.assertIn(u' 0.00… ', lines[5])
87+
# Test real precision below max
88+
self.assertIn(u' 1.72 ', lines[3])
89+
self.assertIn(u' 5.10 ', lines[4])
90+
self.assertIn(u' 0.10 ', lines[5])
91+
6092
def test_print_table_max_column_width(self):
6193
rows = (
6294
('1.7', 2, 'this is long'),

0 commit comments

Comments
 (0)