Пример #1
0
def test_vertical_table():
    """Test the default settings for vertical_table()."""
    results = [('hello', text_type(123)), ('world', text_type(456))]

    expected = dedent("""\
        ***************************[ 1. row ]***************************
        name | hello
        age  | 123
        ***************************[ 2. row ]***************************
        name | world
        age  | 456
        """)
    assert expected == vertical_table_adapter.adapter(results, ('name', 'age'))
def test_vertical_table():
    """Test the default settings for vertical_table()."""
    results = [("hello", text_type(123)), ("world", text_type(456))]

    expected = dedent("""\
        ***************************[ 1. row ]***************************
        name | hello
        age  | 123
        ***************************[ 2. row ]***************************
        name | world
        age  | 456""")
    assert expected == "\n".join(
        vertical_table_adapter.adapter(results, ("name", "age")))
def test_vertical_table_customized():
    """Test customized settings for vertical_table()."""
    results = [('john', text_type(47)), ('jill', text_type(50))]

    expected = dedent("""\
        -[ PERSON 1 ]-----
        name | john
        age  | 47
        -[ PERSON 2 ]-----
        name | jill
        age  | 50""")
    assert expected == "\n".join(vertical_table_adapter.adapter(
        results, ('name', 'age'), sep_title='PERSON {n}',
        sep_character='-', sep_length=(1, 5)))
Пример #4
0
 def results(data):
     for row in data:
         result = []
         for i, v in enumerate(row):
             if column_types[i] is float and type(v) in float_types:
                 v = text_type(v)
                 result.append((pointpos[i] - utils.intlen(v)) * " " + v)
             else:
                 result.append(v)
         yield result
def test_vertical_table_customized():
    """Test customized settings for vertical_table()."""
    results = [("john", text_type(47)), ("jill", text_type(50))]

    expected = dedent("""\
        -[ PERSON 1 ]-----
        name | john
        age  | 47
        -[ PERSON 2 ]-----
        name | jill
        age  | 50""")
    assert expected == "\n".join(
        vertical_table_adapter.adapter(
            results,
            ("name", "age"),
            sep_title="PERSON {n}",
            sep_character="-",
            sep_length=(1, 5),
        ))
Пример #6
0
def align_decimals(data, headers, column_types=(), **_):
    """Align numbers in *data* on their decimal points.

    Whitespace padding is added before a number so that all numbers in a
    column are aligned.

    Outputting data before aligning the decimals::

        1
        2.1
        10.59

    Outputting data after aligning the decimals::

         1
         2.1
        10.59

    :param iterable data: An :term:`iterable` (e.g. list) of rows.
    :param iterable headers: The column headers.
    :param iterable column_types: The columns' type objects (e.g. int or float).
    :return: The processed data and headers.
    :rtype: tuple

    """
    pointpos = len(headers) * [0]
    for row in data:
        for i, v in enumerate(row):
            if column_types[i] is float and type(v) in float_types:
                v = text_type(v)
                pointpos[i] = max(utils.intlen(v), pointpos[i])
    results = []
    for row in data:
        result = []
        for i, v in enumerate(row):
            if column_types[i] is float and type(v) in float_types:
                v = text_type(v)
                result.append((pointpos[i] - utils.intlen(v)) * " " + v)
            else:
                result.append(v)
        results.append(result)
    return results, headers
Пример #7
0
def quote_whitespaces(data, headers, quotestyle="'", **_):
    """Quote leading/trailing whitespace in *data*.

    When outputing data with leading or trailing whitespace, it can be useful
    to put quotation marks around the value so the whitespace is more
    apparent. If one value in a column needs quoted, then all values in that
    column are quoted to keep things consistent.

    .. NOTE::
       :data:`string.whitespace` is used to determine which characters are
       whitespace.

    :param iterable data: An :term:`iterable` (e.g. list) of rows.
    :param iterable headers: The column headers.
    :param str quotestyle: The quotation mark to use (defaults to ``'``).
    :return: The processed data and headers.
    :rtype: tuple

    """
    whitespace = tuple(string.whitespace)
    quote = len(headers) * [False]
    data = list(data)
    for row in data:
        for i, v in enumerate(row):
            v = text_type(v)
            if v.startswith(whitespace) or v.endswith(whitespace):
                quote[i] = True

    def results(data):
        for row in data:
            result = []
            for i, v in enumerate(row):
                quotation = quotestyle if quote[i] else ""
                result.append(
                    "{quotestyle}{value}{quotestyle}".format(
                        quotestyle=quotation, value=v
                    )
                )
            yield result

    return results(data), headers
Пример #8
0
def to_string(value):
    """Convert *value* to a string."""
    if isinstance(value, binary_type):
        return bytes_to_string(value)
    else:
        return text_type(value)