view profilemanager/utils.py @ 25:fc31d3f26755

i seriously need to learn to code
author Jeff Hammel <jhammel@mozilla.com>
date Thu, 06 May 2010 15:45:33 -0700
parents ca57920aa223
children 145e111903d2
line wrap: on
line source

#!/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)