annotate profilemanager/utils.py @ 70:6e282cacd5f0

split the string
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 07 May 2010 16:06:05 -0700
parents ca57920aa223
children 145e111903d2
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/usr/bin/env python
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 def format_tabular(table, spacer=' '):
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 columns = [0 for i in range(max([len(row) for row in table]))]
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5 assert (len(columns) == min([len(row) for row in table]))
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6 for row in table:
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7 for index, column in enumerate(row):
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8 if len(column) > columns[index]:
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
9 columns[index] = len(column)
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
10 format_string = spacer.join(['%s%s' for i in range(len(columns))])
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
11 retval = []
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
12 for row in table:
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
13 values = []
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
14 for index, column in enumerate(row):
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15 values.append(column)
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16 values.append(' ' * (columns[index] - len(column)))
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 retval.append(format_string % tuple(values))
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18 return '\n'.join(retval)
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 if __name__ == '__main__':
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 test_data = [ ['fox', 'i am a fox!', 'Foxkeh'],
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22 ['cat', 'meow', 'Lilly'],
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 ['elephant', 'elephants shower with their trunks', 'Elephino'] ]
ca57920aa223 adding better formatting for list
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24 print format_tabular(test_data)