Skip to content

Commit

Permalink
Fill in any missing data columns when printing data tables.
Browse files Browse the repository at this point in the history
We only combine data tables if they contain the same data to be tested.
However, if a column is missing but is not included in the tests, this
led to an exception when we attempted to print out data not present in
one or other of the test and benchmark.

Instead, in such cases, fill in the data with 'n/a' entries.

Reported by: Nick Blunt.
  • Loading branch information
James Spencer committed Feb 20, 2015
1 parent de4ed74 commit 58d9a6c
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions lib/testcode2/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,22 @@ def wrap_list_strings(word_list, width):

def pretty_print_table(labels, dicts):
'''Print data in dictionaries of identical size in a tabular format.'''
# Fill in the dicts with missing data.
# This can be hit if the missing data fields are ignored...
for dict1 in dicts:
for key in dict1.keys():
if type(dict1[key]) is tuple or type(dict1[key]) is list:
nitems = len(dict1[key])
val = ('n/a',)*nitems
iterable = True
else:
val = 'n/a'
iterable = False
for dict2 in dicts:
if key not in dict2:
dict2[key] = val
elif iterable and nitems != len(dict2[key]):
dict2[key] += nitems - len(dict2[key])
# Loop through all elements in order to calculate the field width.
# Create header line as we go.
fmt = dict(_tc_label='%%-%is' % (max(len(str(label)) for label in labels)))
Expand Down

0 comments on commit 58d9a6c

Please sign in to comment.