Example #1
0
def test_IterableTupleHeaders():
    '''Test that tuples are correctly turned into headers.'''
    import tableful_utils
    testTuple = (('First', 'Second', 'Third'),)
    expected = ('First', 'Second', 'Third')
    headers = tableful_utils._GetIterableHeaders(testTuple)
    assert expected == headers, \
        "Headers were {}, but should be {}".format(expected, headers)
Example #2
0
def test_IterableHeadersFailNotIterable():
    '''Test that a non iterable returns no headers.'''
    import tableful_utils
    testInt = 3
    expected = None
    headers = tableful_utils._GetIterableHeaders(testInt)
    assert headers is expected, \
        "Headers were {} but should be None".format(headers)
Example #3
0
def test_IterableListHeaders():
    '''Test that lists are correctly turned into headers.'''
    import tableful_utils
    testList = [('First', 'Second', 'Third')]
    expected = ('First', 'Second', 'Third')
    headers = tableful_utils._GetIterableHeaders(testList)
    assert expected == headers, \
        "Headers were {}, but should be {}".format(expected, headers)
Example #4
0
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)
Example #5
0
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)