comparison profilemanager/utils.py @ 5:ca57920aa223

adding better formatting for list
author Jeff Hammel <jhammel@mozilla.com>
date Tue, 06 Apr 2010 08:04:24 -0700
parents
children 145e111903d2
comparison
equal deleted inserted replaced
4:35dc297efa25 5:ca57920aa223
1 #!/usr/bin/env python
2
3 def format_tabular(table, spacer=' '):
4 columns = [0 for i in range(max([len(row) for row in table]))]
5 assert (len(columns) == min([len(row) for row in table]))
6 for row in table:
7 for index, column in enumerate(row):
8 if len(column) > columns[index]:
9 columns[index] = len(column)
10 format_string = spacer.join(['%s%s' for i in range(len(columns))])
11 retval = []
12 for row in table:
13 values = []
14 for index, column in enumerate(row):
15 values.append(column)
16 values.append(' ' * (columns[index] - len(column)))
17 retval.append(format_string % tuple(values))
18 return '\n'.join(retval)
19
20 if __name__ == '__main__':
21 test_data = [ ['fox', 'i am a fox!', 'Foxkeh'],
22 ['cat', 'meow', 'Lilly'],
23 ['elephant', 'elephants shower with their trunks', 'Elephino'] ]
24 print format_tabular(test_data)