Welcome, guest | Sign In | My Account | Store | Cart

When writing BDD code with behave, you may want to include a set of examples for scenario outline, or provide a table for setting up initial conditions. This snippet ease the pain of formatting the table properly as text

Python, 34 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import string

def as_behave_table(data):
    """ nosetests --with-doctest --with-coverage report_table.py

    >>> from report_table import as_behave_table
    >>> data = [('what', 'how', 'who'),
    ...         ('lorem', 'that is a long value', 3.1415),
    ...         ('ipsum', 89798, 0.2)]

    >>> print as_behave_table(data)
    | what  | how                  | who    |
    | lorem | that is a long value | 3.1415 |
    | ipsum |                89798 |    0.2 |

    """
    table = []
    # max size of each column
    sizes = map(max, zip(*[[len(str(elt)) for elt in member]
                           for member in data]))
    num_elts = len(sizes)

    start_of_line = '| '
    vertical_separator = ' | '
    end_of_line = ' |'
    meta_template = vertical_separator.join(['{{{{{0}:{{{0}}}}}}}'.format(i)
                                             for i in range(num_elts)])
    template = '{0}{1}{2}'.format(start_of_line,
                                  meta_template.format(*sizes),
                                  end_of_line)

    for d in data:
        table.append(template.format(*d))
    return '\n'.join(table)