コード例 #1
0
ファイル: Test-Utils.py プロジェクト: ajnelson/tableful
def test_GetColumnWidthsRowValueLongest():
    '''Tests that _GetColumnWidths returns the largest cell's length.'''
    import tableful_utils
    headers = ("F", "S")
    rows = ((1000, 2), (300, 4000))
    expected = (len(str(rows[0][0])), len(str(rows[1][1])))
    lengths = tableful_utils._GetColumnWidths(headers, rows)
    assert lengths == expected, \
        "Lengths were {} but should be {}".format(lengths, expected)
コード例 #2
0
ファイル: Test-Utils.py プロジェクト: ajnelson/tableful
def test_GetColumnWidthsHeadersLongest():
    '''Tests that _GetColumnWidths returns the header when it is longest part of the column.'''
    import tableful_utils
    headers = ("First", "Second")
    rows = ((1, 2), (3, 4))
    expected = (len(headers[0]), len(headers[1]))
    lengths = tableful_utils._GetColumnWidths(headers, rows)
    assert lengths == expected, \
        "Lengths were {} but should be {}".format(lengths, expected)
コード例 #3
0
ファイル: tableful.py プロジェクト: ajnelson/tableful
def Tableful(iterable, *, headers=None, file=None):
    '''
    Prints an iterable in a tableful way!

    keyword args:
    headers - an optional headers iterable that will be used in place of main iterables
    file - any object that can be written to
    '''
    tableprinter = print
    embeddedHeaders = True if not headers else False

    if file is not None:
        tableprinter = partial(print, file=file)

    if not headers:
        headers = tableful_utils._GetIterableHeaders(iterable)

    if not headers:
        raise ValueError("No headers available")

    rows = tableful_utils._GetIterableRows(iterable, embeddedHeaders=embeddedHeaders)

    if not rows:
        raise ValueError("No rows available")

    columnWidths = tableful_utils._GetColumnWidths(headers, rows)
    divider = tableful_utils._GetDivider(columnWidths)
    tableprinter(divider)

    # create then print the header cells
    headerCells = []
    for header, width in zip(headers, columnWidths):
        headerCells.append(tableful_utils._BuildCellString(header, width))
    tableprinter("|{}|".format("|".join(headerCells)))
    tableprinter(divider)

    # create row cells then print them
    for row in rows:
        currentRow = []
        for cell, width in zip(row, columnWidths):
            currentRow.append(tableful_utils._BuildCellString(cell, width))
        tableprinter("|{}|".format("|".join(currentRow)))
    tableprinter(divider)
コード例 #4
0
ファイル: tableful.py プロジェクト: bahostetterlewis/tableful
def Tableful(iterable, *, headers=None, file=None, delim="default"):
    '''
    Prints an iterable in a tableful way!

    keyword args:
    headers - an optional headers iterable that will be used in place of main iterables
    file - any object that can be written to
    delim - two value types are excepted
        1) default - creates the default table using +, |, and - to draw everything
        2) html - creates a table with class tableful and td class tablefultd and th has class tablefulth
                  The th are centered
                  The td are centered
                  table has border=1
                  The classes allow for overiding these settings in css
        3) a dictionary that has the following keywords to create a custom table
                - outer_left: what will appear on the outer most left side of rows default is |
                - inbetween: seperates each cell from the one next to it in the rows and headers, default is |
                - outer_right: same as outer_left but on the very outer right edge of the table  default is |
                - outer_header_left: similar to outer_left only changes the headers outer most delim default is |
                - header_inbetween: simlar to inbetween, appears between each header cell defaults is |
                - outer_header_right: similar to outer_header_right but on the right side default is |
                - corners: appears on all corners, and between cell dividers default is +
                - divider: creates the divider for the top, after header, and bottom of table
                    combined with corners default is -
                - before_all: printed before the table default is blank, used for things like <table> tag
                - after_all: printed after the whole table default is blank, used for things like a </table> tag
    '''
    try:
        delimiters = _delimiter_values[delim]
    except KeyError:
        delimiters = _delimiter(**delim)

    tableprinter = print
    embeddedHeaders = True if not headers else False

    if file is not None:
        tableprinter = partial(tableprinter, file=file)

    if not headers:
        headers = tableful_utils._GetIterableHeaders(iterable)

    if not headers:
        raise ValueError("No headers available")

    rows = tableful_utils._GetIterableRows(iterable, embeddedHeaders=embeddedHeaders)

    if not rows:
        raise ValueError("No rows available")

    columnWidths = tableful_utils._GetColumnWidths(headers, rows)
    divider = tableful_utils._GetDivider(columnWidths, corners=delimiters.corners, divider=delimiters.divider)

    tableprinter(delimiters.before_all)
    tableprinter(divider)
    # create then print the header cells
    headerCells = []
    for header, width in zip(headers, columnWidths):
        headerCells.append(tableful_utils._BuildCellString(header, width))
    tableprinter("{}{}{}".format(delimiters.outer_header_left, delimiters.header_inbetween.join(headerCells), delimiters.outer_header_right))
    tableprinter(divider)

    # create row cells then print them
    for row in rows:
        currentRow = []
        for cell, width in zip(row, columnWidths):
            currentRow.append(tableful_utils._BuildCellString(cell, width))
        tableprinter("{}{}{}".format(delimiters.outer_left, delimiters.inbetween.join(currentRow), delimiters.outer_right))
    tableprinter(divider)
    tableprinter(delimiters.after_all)