Skip to content

Commit

Permalink
Add handling to show number of rows whenever truncating data
Browse files Browse the repository at this point in the history
  • Loading branch information
EntilZha committed Feb 1, 2017
1 parent 52c1c96 commit ff09913
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
20 changes: 14 additions & 6 deletions functional/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -1600,9 +1600,9 @@ def show(self, n=10, headers=(), tablefmt="simple", floatfmt="g", numalign="deci
:param stralign: Passed to tabulate
:param missingval: Passed to tabulate
"""
formatted_seq = tabulate(self.take(n).list(), headers=headers, tablefmt=tablefmt,
floatfmt=floatfmt, numalign=numalign, stralign=stralign,
missingval=missingval)
formatted_seq = self.tabulate(n=n, headers=headers, tablefmt=tablefmt,
floatfmt=floatfmt, numalign=numalign, stralign=stralign,
missingval=missingval)
print(formatted_seq)

def _repr_html_(self):
Expand All @@ -1627,17 +1627,25 @@ def tabulate(self, n=None, headers=(), tablefmt="simple", floatfmt="g", numalign
:param missingval: Passed to tabulate
"""
self.cache()
if self.len() == 0 or not is_tabulatable(self[0]):
length = self.len()
if length == 0 or not is_tabulatable(self[0]):
return None

if n is None:
if n is None or n >= length:
rows = self.list()
message = ''
else:
rows = self.take(n).list()
if tablefmt == 'simple':
message = '\nShowing {} of {} rows'.format(n, length)
elif tablefmt == 'html':
message = '<p>Showing {} of {} rows'.format(n, length)
else:
message = ''
if len(headers) == 0 and is_namedtuple(rows[0]):
headers = rows[0]._fields
return tabulate(rows, headers=headers, tablefmt=tablefmt, floatfmt=floatfmt,
numalign=numalign, stralign=stralign, missingval=missingval)
numalign=numalign, stralign=stralign, missingval=missingval) + message


def _wrap(value):
Expand Down
5 changes: 5 additions & 0 deletions functional/test/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,11 @@ class NotTabulatable(object):
sequence = seq(NotTabulatable(), NotTabulatable(), NotTabulatable())
self.assertEqual(sequence.tabulate(), None)

long_data = seq([(i, i + 1) for i in range(30)])
self.assertTrue('Showing 10 of 30 rows' in long_data.tabulate(n=10))
self.assertTrue('Showing 10 of 30 rows' in long_data._repr_html_())
self.assertTrue('Showing 10 of 30 rows' not in long_data.tabulate(n=10, tablefmt='plain'))

def test_tabulate_namedtuple(self):
sequence_tabulated = seq([Data(1, 2), Data(6, 7)]).tabulate()
self.assertEqual(sequence_tabulated, ' x y\n--- ---\n 1 2\n 6 7')
Expand Down

0 comments on commit ff09913

Please sign in to comment.