The presentation videos made during the KVM forum 2010 are available under vimeo, see the red hat channel. My presentation video can be seen here:
If you are interested in KVM and didn’t make it into the conference, here’s your opportunity to catch up 😉
Ever wondered how to print a matrix (ie, a collection of rows and columns with data in it) and have it always nicely aligned? I searched for it and couldn’t find a handy function to do that, so I decided to implement my own, feel free to report any problems with it:
# -*- coding: utf-8 -*-
def matrix_to_string(matrix, header=None):
Return a pretty, aligned string representation of a nxm matrix.
This representation can be used to print any tabular data, such as
database results. It works by scanning the lengths of each element
in each column, and determining the format string dynamically.
@param matrix: Matrix representation (list with n rows of m elements).
@param header: Optional tuple or list with header elements to be displayed.
if type(header) is list:
header = tuple(header)
lengths = 
for column in header:
for row in matrix:
for column in row:
i = row.index(column)
column = str(column)
cl = len(column)
ml = lengths[i]
if cl > ml:
lengths[i] = cl
lengths = tuple(lengths)
format_string = ""
for length in lengths:
format_string += "%-" + str(length) + "s "
format_string += "\n"
matrix_str = ""
matrix_str += format_string % header
for row in matrix:
matrix_str += format_string % tuple(row)
if __name__ == "__main__":
header = ("Word1", "Word2", "Word3")
rows = [["Heeeeeeeeeeeeeey", "Hey", "Eh"],
["Orange", "Nonononono", "Pineapple"],
["Egg", "Spam", "Bacon"]]
result = matrix_to_string(rows, header)
Executing this program will give you the following result:
Word1 Word2 Word3
Heeeeeeeeeeeeeey Hey Eh
Orange Nonononono Pineapple
Egg Spam Bacon
Update: Fixed the code to work with any input data.