I am trying to write a function that prints tables of various dimensions. May not be the best attempt the way I did it, but almost there.
Last piece of the puzzle is creating the strings with a variable amount of input lines. Since .format() wants input of the form .format(s1, s2, s3) and does not take a list as in .format( (line) ) I have trouble making this work for variable number of columns. Is there any way to do this (in 2.7) without brute force attempts like eval? I always prefer to not use those when void it.
def print_table(self, lines, sum_columns = [], extra_width = 5):
maxLength = [0] * len(lines)
for line in lines:
maxLength = [max(l1,l2) for l1, l2 in zip(maxLength, [len(str(l)) for l in line])]
if sum_columns:
total = [0] * len(lines)
for line in lines:
for c in sum_columns:
total[c-1] += line[c-1]
string = ''
for c in xrange(len(maxLength)): string = string + '{%s:%s} ' %(c, maxLength[c] + extra_width)
string = string[:-1]
print string
for line in lines:
print string.format(XXXXXXXXX)