Ejemplo n.º 1
0
def grow_table(tbl, width, tbldef, delimiters):

    column_overhead = 0
    margin = 0
    if delimiters:
        d = delimiters
        column_overhead = ulen(d['body_csep_m'])
        margin = ulen(d['body_l'] + d['body_r'])

    width = width - margin
    colwidths = [0] * len(tbldef)

    def scoresum(col, width):
        column = (r[col] for r in tbl[1:])
        return sum(tbldef[col]['fval'](c, width) for c in column)

    def nonempty_cols():
        return sum(1 for i in colwidths if i > 0) - 1

    # keep growing until width is reached
    while sum(colwidths) + column_overhead * nonempty_cols() < width:

        # determine column that benefits most from growing
        scoregain = [
            scoresum(i, w + 1) - scoresum(i, w)
            if w > 0 else float('Inf')
            for i, w in enumerate(colwidths)
        ]
        bestcol = scoregain.index(max(scoregain))

        # grow best column
        colwidths[bestcol] += 1

    def format_cell(i, c, ellipsis_fn):
        if colwidths[i]:
            cf = ellipsis_fn(c, colwidths[i])
            return cf + " " * (colwidths[i] - ulen(cf))
        else:
            return ""

    header = [
        format_cell(i, c, simple_ellipsis())
        for i, c in enumerate(tbl[0])
        if colwidths[i] > 0
    ]

    body = [
        [
            format_cell(i, c, tbldef[i]['ellipsis'])
            for i, c in enumerate(row)
            if colwidths[i] > 0
        ]
        for row in tbl[1:]
    ]

    return [header] + body, delimiters
Ejemplo n.º 2
0
def pretty_table(tbl, colordef, header_row=True, delimiters=DELIMITERS_FULL):
    from .color import COLOR_BLACK, colortext

    d = delimiters

    max_widths = [max(ulen(c) for c in col) for col in zip(*tbl)]

    tjust = [[u"{0:{1}s}".format(c, w) for w, c in zip(max_widths, row)]
             for row in tbl]

    pretty_top = d['header_tl'] + d['header_csep_t'].join(
        d['header_t'] * w for w in max_widths) + d['header_tr'] + "\n"
    pretty_bottom = d['body_bl'] + d['body_csep_b'].join(
        d['body_b'] * w for w in max_widths) + d['body_br']

    if header_row:
        header = tjust.pop(0)
        header = [colortext(h, COLOR_BLACK, True) for h in header]

        pretty_colheader = d['header_l'] + \
            d['header_csep_m'].join(header) + d['header_r']
        pretty_underline = d['header_bl'] + d['header_csep_b'].join(
            d['header_b'] * w for w in max_widths) + d['header_br']
        pretty_header = pretty_colheader + "\n" + pretty_underline + "\n"

        pretty_top = pretty_top + pretty_header

    tjust = [[colortext(t, fmt['color'](t)) for fmt, t in zip(colordef, row)]
             for row in tjust]

    return pretty_top + "".join(
        d['body_l'] + d['body_csep_m'].join(row) + d['body_r'] + "\n"
        for row in tjust) + pretty_bottom
Ejemplo n.º 3
0
def pretty_table(tbl, colordef, header_row=True, delimiters=DELIMITERS_FULL):
    from .color import COLOR_BLACK, colortext

    d = delimiters

    max_widths = [max(ulen(c) for c in col) for col in zip(*tbl)]

    tjust = [[u"{0:{1}s}".format(c, w)
              for w, c in zip(max_widths, row)] for row in tbl]

    pretty_top = d['header_tl'] + d['header_csep_t'].join(
        d['header_t'] * w for w in max_widths) + d['header_tr'] + "\n"
    pretty_bottom = d[
        'body_bl'] + d['body_csep_b'].join(d['body_b'] * w for w in max_widths) + d['body_br']

    if header_row:
        header = tjust.pop(0)
        header = [colortext(h, COLOR_BLACK, True) for h in header]

        pretty_colheader = d['header_l'] + \
            d['header_csep_m'].join(header) + d['header_r']
        pretty_underline = d[
            'header_bl'] + d['header_csep_b'].join(d['header_b'] * w for w in max_widths) + d['header_br']
        pretty_header = pretty_colheader + "\n" + pretty_underline + "\n"

        pretty_top = pretty_top + pretty_header

    tjust = [[colortext(t, fmt['color'](t))
              for fmt, t in zip(colordef, row)] for row in tjust]

    return pretty_top + "".join(d['body_l'] + d['body_csep_m'].join(row) + d['body_r'] + "\n" for row in tjust) + pretty_bottom
Ejemplo n.º 4
0
def grow_table(tbl, width, tbldef, delimiters):

    column_overhead = 0
    margin = 0
    if delimiters:
        d = delimiters
        column_overhead = ulen(d["body_csep_m"])
        margin = ulen(d["body_l"] + d["body_r"])

    width = width - margin

    column_score = lambda col, fval, width: sum(fval(c, width) for c in col)
    column = lambda i: (r[i] for r in tbl[1:])

    scoresum = lambda col, width: column_score(column(col), tbldef[col]["fval"], width)

    colwidths = [0 for col in tbldef]

    nonempty_cols = lambda: sum(1 for i in colwidths if i > 0) - 1

    while sum(colwidths) + column_overhead * nonempty_cols() < width:

        scoregain = [scoresum(i, w + 1) - scoresum(i, w) if w > 0 else float("Inf") for i, w in enumerate(colwidths)]
        # print("\t".join( "{:.3f}".format(sg) for sg in scoregain))

        bestcol = scoregain.index(max(scoregain))

        colwidths[bestcol] += 1

    def format_col(i, c):
        if colwidths[i]:
            cf = tbldef[i]["ellipsis"](c, colwidths[i])
            return cf + " " * (colwidths[i] - ulen(cf))
        else:
            return ""

    def format_hdr(i, c):
        if colwidths[i]:
            cf = simple_ellipsis()(c, colwidths[i])
            return cf + " " * (colwidths[i] - ulen(cf))
        else:
            return ""

    header = [format_hdr(i, c) for i, c in enumerate(tbl[0]) if colwidths[i] > 0]
    body = [[format_col(i, c) for i, c in enumerate(row) if colwidths[i] > 0] for row in tbl[1:]]

    return [header] + body
Ejemplo n.º 5
0
def fit_table(tbl, width, tbldef, delimiters):
    '''Pad cells to match maximum column width and stretch delimiters'''

    max_widths = [
        max(ulen(col[0]), max(ulen(tbldef[cx]['ellipsis'](c)) for c in col[1:]))
        for cx, col in enumerate(zip(*tbl))
    ]

    for rx, row in enumerate(tbl):
        for cx, (w, cell) in enumerate(zip(max_widths, row)):
            if rx > 0:
                tbl[rx][cx] = tbldef[cx]['ellipsis'](cell, width=w)
            else:
                str(cell)

            tbl[rx][cx] = "{0: <{1}}".format(tbl[rx][cx], w)

    return tbl, delimiters
Ejemplo n.º 6
0
    def sv(content, width):
        content = formatter(content)

        if max_width and width > max_width:
            width = max_width

        if width < min_width:
            return 0

        return min(width, ulen(content)) * factor + math.log(width + 1) * overflow
Ejemplo n.º 7
0
def fit_table(tbl, width, tbldef, delimiters):
    '''Pad cells to match maximum column width and stretch delimiters'''

    max_widths = [
        max(ulen(col[0]),
            max(ulen(tbldef[cx]['ellipsis'](c)) for c in col[1:]))
        for cx, col in enumerate(zip(*tbl))
    ]

    for rx, row in enumerate(tbl):
        for cx, (w, cell) in enumerate(zip(max_widths, row)):
            if rx > 0:
                tbl[rx][cx] = tbldef[cx]['ellipsis'](cell, width=w)
            else:
                str(cell)

            tbl[rx][cx] = "{0: <{1}}".format(tbl[rx][cx], w)

    return tbl, delimiters
Ejemplo n.º 8
0
    def sv(content, width):
        content = formatter(content)

        if max_width and width > max_width:
            width = max_width

        if width < min_width:
            return 0

        return min(width, ulen(content)) * factor + math.log(width + 1) * overflow
Ejemplo n.º 9
0
    def se(content, width):
        content = formatter(content)

        if ulen(content) <= width:
            return content
        if width > 2:
            return content[0 : width - 2] + u"…" + content[-1]

        elif width == 2:
            return content[0:2]
        else:
            return content[0:width]
Ejemplo n.º 10
0
    def se(content, width=None):
        content = formatter(content)
        if width is None:
            width = len(content)

        if(ulen(content) <= width):
            return content
        if width > 2:
            return content[0:width - 2] + u"…" + content[-1]

        elif width == 2:
            return content[0:2]
        else:
            return content[0:width]
Ejemplo n.º 11
0
 def format_hdr(i, c):
     if colwidths[i]:
         cf = simple_ellipsis()(c, colwidths[i])
         return cf + " " * (colwidths[i] - ulen(cf))
     else:
         return ""
Ejemplo n.º 12
0
 def format_col(i, c):
     if colwidths[i]:
         cf = tbldef[i]["ellipsis"](c, colwidths[i])
         return cf + " " * (colwidths[i] - ulen(cf))
     else:
         return ""
Ejemplo n.º 13
0
 def format_cell(i, c, ellipsis_fn):
     if colwidths[i]:
         cf = ellipsis_fn(c, colwidths[i])
         return cf + " " * (colwidths[i] - ulen(cf))
     else:
         return ""