Add a python bindings sequence test
[babeltrace.git] / bindings / python / examples / python2 / output_format_modules / pprint_table.py
CommitLineData
24a3136a 1# pprint_table.py
300b4c33 2#
24a3136a
DS
3# This module is used to pretty-print a table
4# Adapted from
5# http://ginstrom.com/scribbles/2007/09/04/pretty-printing-a-table-in-python/
6
7import sys
8
9def get_max_width(table, index):
10 """Get the maximum width of the given column index"""
11
12 return max([len(str(row[index])) for row in table])
13
14
15def pprint_table(table, nbLeft=1, out=sys.stdout):
16 """
17 Prints out a table of data, padded for alignment
18 @param table: The table to print. A list of lists.
19 Each row must have the same number of columns.
20 @param nbLeft: The number of columns aligned left
21 @param out: Output stream (file-like object)
22 """
23
24 col_paddings = []
25
26 for i in range(len(table[0])):
27 col_paddings.append(get_max_width(table, i))
28
29 for row in table:
30 # left cols
31 for i in range(nbLeft):
32 print >> out, str(row[i]).ljust(col_paddings[i] + 1),
33 # rest of the cols
34 for i in range(nbLeft, len(row)):
35 col = str(row[i]).rjust(col_paddings[i] + 2)
36 print >> out, col,
37 print >> out
This page took 0.026597 seconds and 4 git commands to generate.