handle an empty data set

This commit is contained in:
Doug Hellmann 2012-04-27 19:23:26 -04:00
parent 216079ee59
commit b8f3ad548d
1 changed files with 12 additions and 8 deletions

View File

@ -32,14 +32,18 @@ class TableLister(ListFormatter):
# first row and set the alignment of the
# output accordingly.
data_iter = iter(data)
first_row = next(data_iter)
for value, name in zip(first_row, column_names):
alignment = self.ALIGNMENTS.get(type(value), 'l')
x.set_field_align(name, alignment)
# Now iterate over the data and add the rows.
x.add_row(first_row)
for row in data_iter:
x.add_row(row)
try:
first_row = next(data_iter)
except StopIteration:
pass
else:
for value, name in zip(first_row, column_names):
alignment = self.ALIGNMENTS.get(type(value), 'l')
x.set_field_align(name, alignment)
# Now iterate over the data and add the rows.
x.add_row(first_row)
for row in data_iter:
x.add_row(row)
formatted = x.get_string(fields=(parsed_args.columns or column_names))
stdout.write(formatted)
stdout.write('\n')