X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=blobdiff_plain;f=bindings%2Fpython%2Fexamples%2Foutput_format_modules%2Fpprint_table.py;fp=bindings%2Fpython%2Fexamples%2Foutput_format_modules%2Fpprint_table.py;h=a7e8255782f78f325580f74816db390a91e26800;hp=0000000000000000000000000000000000000000;hb=24a3136a3837ee43162b1393827faa938f0789eb;hpb=2002e48a9f3f38f95de0b9cd75a6ab1f06580c95 diff --git a/bindings/python/examples/output_format_modules/pprint_table.py b/bindings/python/examples/output_format_modules/pprint_table.py new file mode 100644 index 00000000..a7e82557 --- /dev/null +++ b/bindings/python/examples/output_format_modules/pprint_table.py @@ -0,0 +1,37 @@ +# pprint_table.py +# +# This module is used to pretty-print a table +# Adapted from +# http://ginstrom.com/scribbles/2007/09/04/pretty-printing-a-table-in-python/ + +import sys + +def get_max_width(table, index): + """Get the maximum width of the given column index""" + + return max([len(str(row[index])) for row in table]) + + +def pprint_table(table, nbLeft=1, out=sys.stdout): + """ + Prints out a table of data, padded for alignment + @param table: The table to print. A list of lists. + Each row must have the same number of columns. + @param nbLeft: The number of columns aligned left + @param out: Output stream (file-like object) + """ + + col_paddings = [] + + for i in range(len(table[0])): + col_paddings.append(get_max_width(table, i)) + + for row in table: + # left cols + for i in range(nbLeft): + print >> out, str(row[i]).ljust(col_paddings[i] + 1), + # rest of the cols + for i in range(nbLeft, len(row)): + col = str(row[i]).rjust(col_paddings[i] + 2) + print >> out, col, + print >> out