solvebio/utils/tabulate.py
def _normalize_tabular_data(tabular_data, headers, sort=True):
    """
    Transform a supported data type to a list of lists, and a list of headers.

    Supported tabular data types:

    * list-of-lists or another iterable of iterables

    * 2D NumPy arrays

    * dict of iterables (usually used with headers="keys")

    * pandas.DataFrame (usually used with headers="keys")

    The first row can be used as headers if headers="firstrow",
    column indices can be used as headers if headers="keys".

    """

    if hasattr(tabular_data, "keys") and hasattr(tabular_data, "values"):
        # dict-like and pandas.DataFrame?
        if hasattr(tabular_data.values, "__call__"):
            # likely a conventional dict
            keys = list(tabular_data.keys())
            # columns have to be transposed
            rows = list(izip_longest(*list(tabular_data.values())))
        elif hasattr(tabular_data, "index"):
            # values is a property, has .index then
            # it's likely a pandas.DataFrame (pandas 0.11.0)
            keys = list(tabular_data.keys())
            # values matrix doesn't need to be transposed
            vals = tabular_data.values
            names = tabular_data.index
            rows = [[v] + list(row) for v, row in zip(names, vals)]
        else:
            raise ValueError("tabular data doesn't appear to be a dict "
                             "or a DataFrame")

        if headers == "keys":
            headers = list(map(_text_type, keys))  # headers should be strings

    else:  # it's, as usual, an iterable of iterables, or a NumPy array
        rows = list(tabular_data)

        if headers == "keys" and len(rows) > 0:  # keys are column indices
            headers = list(map(_text_type, list(range(len(rows[0])))))

    # take headers from the first row if necessary
    if headers == "firstrow" and len(rows) > 0:
        headers = list(map(_text_type, rows[0]))  # headers should be strings
        rows = rows[1:]

    headers = list(headers)

    rows = list(map(list, rows))

    if sort and len(rows) > 1:
        rows = sorted(rows, key=lambda x: x[0])

    # pad with empty headers for initial columns if necessary
    if headers and len(rows) > 0:
        nhs = len(headers)
        ncols = len(rows[0])
        if nhs < ncols:
            headers = [""] * (ncols - nhs) + headers

    return rows, headers
def _normalize_tabular_data(tabular_data, headers, sort=True):
    """
    Transform a supported data type to a list of lists, and a list of headers.

    Supported tabular data types:

    * list-of-lists or another iterable of iterables

    * 2D NumPy arrays

    * dict of iterables (usually used with headers="keys")

    * pandas.DataFrame (usually used with headers="keys")

    The first row can be used as headers if headers="firstrow",
    column indices can be used as headers if headers="keys".

    """

    if hasattr(tabular_data, "keys") and hasattr(tabular_data, "values"):
        # dict-like and pandas.DataFrame?
        if hasattr(tabular_data.values, "__call__"):
            # likely a conventional dict
            keys = list(tabular_data.keys())
            # columns have to be transposed
            rows = list(izip_longest(*list(tabular_data.values())))
        elif hasattr(tabular_data, "index"):
            # values is a property, has .index then
            # it's likely a pandas.DataFrame (pandas 0.11.0)
            keys = list(tabular_data.keys())
            # values matrix doesn't need to be transposed
            vals = tabular_data.values
            names = tabular_data.index
            rows = [[v] + list(row) for v, row in zip(names, vals)]
        else:
            raise ValueError("tabular data doesn't appear to be a dict "
                             "or a DataFrame")

        if headers == "keys":
            headers = list(map(_text_type, keys))  # headers should be strings

    else:  # it's, as usual, an iterable of iterables, or a NumPy array
        rows = list(tabular_data)

        if headers == "keys" and len(rows) > 0:  # keys are column indices
            headers = list(map(_text_type, list(range(len(rows[0])))))

    # take headers from the first row if necessary
    if headers == "firstrow" and len(rows) > 0:
        headers = list(map(_text_type, rows[0]))  # headers should be strings
        rows = rows[1:]

    headers = list(headers)

    rows = list(map(list, rows))

    if sort and len(rows) > 1:
        rows = sorted(rows, key=lambda x: x[0])

    # pad with empty headers for initial columns if necessary
    if headers and len(rows) > 0:
        nhs = len(headers)
        ncols = len(rows[0])
        if nhs < ncols:
            headers = [""] * (ncols - nhs) + headers

    return rows, headers
def _normalize_tabular_data ( tabular_data , headers , sort = True ) : if hasattr ( tabular_data , "keys" ) and hasattr ( tabular_data , "values" ) : # dict-like and pandas.DataFrame? if hasattr ( tabular_data . values , "__call__" ) : # likely a conventional dict keys = list ( tabular_data . keys ( ) ) # columns have to be transposed rows = list ( izip_longest ( * list ( tabular_data . values ( ) ) ) ) elif hasattr ( tabular_data , "index" ) : # values is a property, has .index then # it's likely a pandas.DataFrame (pandas 0.11.0) keys = list ( tabular_data . keys ( ) ) # values matrix doesn't need to be transposed vals = tabular_data . values names = tabular_data . index rows = [ [ v ] + list ( row ) for v , row in zip ( names , vals ) ] else : raise ValueError ( "tabular data doesn't appear to be a dict " "or a DataFrame" ) if headers == "keys" : headers = list ( map ( _text_type , keys ) ) # headers should be strings else : # it's, as usual, an iterable of iterables, or a NumPy array rows = list ( tabular_data ) if headers == "keys" and len ( rows ) > 0 : # keys are column indices headers = list ( map ( _text_type , list ( range ( len ( rows [ 0 ] ) ) ) ) ) # take headers from the first row if necessary if headers == "firstrow" and len ( rows ) > 0 : headers = list ( map ( _text_type , rows [ 0 ] ) ) # headers should be strings rows = rows [ 1 : ] headers = list ( headers ) rows = list ( map ( list , rows ) ) if sort and len ( rows ) > 1 : rows = sorted ( rows , key = lambda x : x [ 0 ] ) # pad with empty headers for initial columns if necessary if headers and len ( rows ) > 0 : nhs = len ( headers ) ncols = len ( rows [ 0 ] ) if nhs < ncols : headers = [ "" ] * ( ncols - nhs ) + headers return rows , headers
Transform a supported data type to a list of lists, and a list of headers.

    Supported tabular data types:

    * list-of-lists or another iterable of iterables

    * 2D NumPy arrays

    * dict of iterables (usually used with headers="keys")

    * pandas.DataFrame (usually used with headers="keys")

    The first row can be used as headers if headers="firstrow",
    column indices can be used as headers if headers="keys".
Transform a supported data type to a list of lists and a list of headers .