| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Copyright (C) 2010 Anders Logg |
| 3 |
# |
| 4 |
# This file is part of FFC. |
| 5 |
# |
| 6 |
# FFC is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU Lesser General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# FFC is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU Lesser General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU Lesser General Public License |
| 17 |
# along with FFC. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
# |
| 19 |
# First added: 2010-05-11 |
| 20 |
# Last changed: 2010-05-11 |
| 21 |
|
| 22 |
def print_table(values, title): |
| 23 |
"Print nicely formatted table." |
| 24 |
|
| 25 |
m = max([key[0] for key in values]) + 2 |
| 26 |
n = max([key[1] for key in values]) + 2 |
| 27 |
|
| 28 |
table = [] |
| 29 |
for i in range(m): |
| 30 |
table.append(["" for j in range(n)]) |
| 31 |
|
| 32 |
for i in range(m - 1): |
| 33 |
table[i + 1][0] = str(values[(i, 0)][0]) |
| 34 |
|
| 35 |
for j in range(n - 1): |
| 36 |
table[0][j + 1] = str(values[(0, j)][1]) |
| 37 |
|
| 38 |
for i in range(m - 1): |
| 39 |
for j in range(n - 1): |
| 40 |
value = values[(i, j)][2] |
| 41 |
if isinstance(value, float): |
| 42 |
value = "%.5g" % value |
| 43 |
table[i + 1][j + 1] = value |
| 44 |
|
| 45 |
table[0][0] = title |
| 46 |
|
| 47 |
column_sizes = [max([len(table[i][j]) for i in range(m)]) for j in range(n)] |
| 48 |
row_size = sum(column_sizes) + 3*(len(column_sizes) - 1) + 2 |
| 49 |
|
| 50 |
print("") |
| 51 |
for i in range(m): |
| 52 |
print(" " + "-"*row_size) |
| 53 |
print("|", end="") |
| 54 |
for j in range(n): |
| 55 |
print(table[i][j] + " "*(column_sizes[j] - len(table[i][j])), end="") |
| 56 |
print("|", end="") |
| 57 |
print("") |
| 58 |
print(" " + "-"*row_size) |
| 59 |
print("") |