diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/profilemanager/utils.py	Tue Apr 06 08:04:24 2010 -0700
@@ -0,0 +1,24 @@
+#!/usr/bin/env python
+
+def format_tabular(table, spacer=' '):
+    columns = [0 for i in range(max([len(row) for row in table]))]
+    assert (len(columns) == min([len(row) for row in table]))
+    for row in table:
+        for index, column in enumerate(row):
+            if len(column) > columns[index]:
+                columns[index] = len(column)
+    format_string = spacer.join(['%s%s' for i in range(len(columns))])
+    retval = []
+    for row in table:
+        values = []
+        for index, column in enumerate(row):
+            values.append(column)
+            values.append(' ' * (columns[index] - len(column)))
+        retval.append(format_string % tuple(values))
+    return '\n'.join(retval)
+    
+if __name__ == '__main__':
+    test_data = [ ['fox', 'i am a fox!', 'Foxkeh'],
+                  ['cat', 'meow', 'Lilly'],
+                  ['elephant', 'elephants shower with their trunks', 'Elephino'] ]
+    print format_tabular(test_data)