예제 #1
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
예제 #2
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
예제 #3
0
def test_intlen_without_decimal():
    """Test that intlen() counts correctly without a decimal place."""
    assert utils.intlen('11') == 2
예제 #4
0
def test_intlen_with_decimal():
    """Test that intlen() counts correctly with a decimal place."""
    assert utils.intlen("11.1") == 2
    assert utils.intlen("1.1") == 1