Skip to content

Commit

Permalink
[tools] add generic method for formatting ascii tables
Browse files Browse the repository at this point in the history
  • Loading branch information
sdrave committed Apr 21, 2017
1 parent 6bbdba8 commit 3e1e69e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 30 deletions.
6 changes: 2 additions & 4 deletions src/pymor/algorithms/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from pymor.core.exceptions import NoMatchingRuleError, RuleNotMatchingError
from pymor.core.interfaces import abstractmethod, classinstancemethod
from pymor.operators.interfaces import OperatorInterface
from pymor.tools.table import format_table


class rule():
Expand Down Expand Up @@ -112,10 +113,7 @@ def __repr__(cls):
r.condition_type,
r.condition_description,
r.action_description])
column_widths = [max(map(len, c)) for c in zip(*rows)]
rows.insert(1, ['-' * cw for cw in column_widths])
return '\n'.join(' '.join('{:<{}}'.format(c, cw) for c, cw in zip(r, column_widths))
for r in rows)
return format_table(rows)

def __getitem__(cls, idx):
return cls._rules[idx]
Expand Down
33 changes: 7 additions & 26 deletions src/pymor/core/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ def method_called_by_user(V, option_for_algorithm=None):
import textwrap

from pymor.core.config import config
from pymor.tools.table import format_table


_default_container = None

Expand Down Expand Up @@ -359,36 +361,15 @@ def print_defaults(import_all=True, shorten_paths=2):
keys[int(i)].append('.'.join(k_parts))
values[int(i)].append(repr(v))
comments[int(i)].append(c)
key_width = max(max([0] + list(map(len, ks))) for ks in keys)
value_width = max(max([0] + list(map(len, vls))) for vls in values)
key_string = 'path (shortened)' if shorten_paths else 'path'
header = '''
{:{key_width}} {:{value_width}} source'''[1:].format(key_string, 'value',
key_width=key_width, value_width=value_width)
header_width = len(header)

for i, (ks, vls, cs) in enumerate(zip(keys, values, comments)):

description = 'defaults not affecting state id calculation' if i else 'defaults affecting state id calcuation'
print('=' * header_width)
print('{:^{width}}'.format(description, width=header_width))
print()
print(header)
print('=' * header_width)

lks = ks[0].split('.')[:-1] if ks else ''
for k, v, c in zip(ks, vls, cs):

ks = k.split('.')[:-1]
if lks != ks:
print('')
lks = ks

print('{:{key_width}} {:{value_width}} {}'.format(k, v, c,
key_width=key_width,
value_width=value_width))

print()
rows = [[key_string, 'value', 'source']] + list(zip(ks, vls, cs))
print(format_table(rows, title=description))
if not i:
print()
print()
print()


Expand Down
42 changes: 42 additions & 0 deletions src/pymor/tools/table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# This file is part of the pyMOR project (http://www.pymor.org).
# Copyright 2013-2016 pyMOR developers and contributors. All rights reserved.
# License: BSD 2-Clause License (http://opensource.org/licenses/BSD-2-Clause)

from itertools import zip_longest
from textwrap import wrap

import numpy as np


def format_table(rows, width='AUTO', title=None):
if width == 'AUTO':
try:
from shutil import get_terminal_size
width = get_terminal_size()[0] - 1
except ImportError:
width = 1000000
rows = rows.copy()
column_widths = [max(map(len, c)) for c in zip(*rows)]
if sum(column_widths) + 2*(len(column_widths) - 1) > width:
largest_column = np.argmax(column_widths)
column_widths[largest_column] = 0
min_width = max(column_widths)
column_widths[largest_column] = max(min_width, width - 2*(len(column_widths) - 1) - sum(column_widths))
total_width = sum(column_widths) + 2*(len(column_widths) - 1)

wrapped_rows = []
for row in rows:
cols = [wrap(c, width=cw) for c, cw in zip(row, column_widths)]
for r in zip_longest(*cols, fillvalue=""):
wrapped_rows.append(r)
rows = wrapped_rows

rows.insert(1, ['-' * cw for cw in column_widths])

if title is not None:
title = '{:^{}}\n{:^{}}\n\n'.format(title, total_width, '='*len(title), total_width)
else:
title = ''

return title + '\n'.join(' '.join('{:<{}}'.format(c, cw) for c, cw in zip(r, column_widths))
for r in rows)

0 comments on commit 3e1e69e

Please sign in to comment.