cpdb.py 文件源码

python
阅读 19 收藏 0 点赞 0 评论 0

项目:touch-pay-client 作者: HackPucBemobi 项目源码 文件源码
def wrapTable(
        self, rows, hasHeader=False, headerChar='-', delim=' | ', justify='left',
            separateRows=False, prefix='', postfix='', wrapfunc=lambda x: x):

        def rowWrapper(row):

            '''---
                    newRows is returned like
                            [['w'], ['x'], ['y'], ['z']]
                                                                    ---'''
            newRows = [wrapfunc(item).split('\n') for item in row]
            self.rows = newRows
            '''---
                        rowList gives like newRows but
                            formatted like [[w, x, y, z]]
                                                                ---'''
            rowList = [[substr or '' for substr in item]
                       for item in map(None, *newRows)]
            return rowList

        logicalRows = [rowWrapper(row) for row in rows]

        columns = map(None, *reduce(operator.add, logicalRows))
        self.columns = columns

        maxWidths = [max(
                     [len(str
                        (item)) for
                      item in column]
                     ) for column
                     in columns]

        rowSeparator = headerChar * (len(prefix) +len(postfix) + sum(maxWidths) +
                                     len(delim) * (len(maxWidths) - 1))

        justify = {'center': str.center,
                   'right': str.rjust,
                   'left': str.ljust
                   }[justify.lower()]

        output = cStringIO.StringIO()

        if separateRows:
            print >> output, rowSeparator

        for physicalRows in logicalRows:
            for row in physicalRows:
                print >> output,\
                    prefix + delim.join([
                                        justify(str(item), width) for (
                                        item, width) in zip(row, maxWidths)]
                                        ) + postfix

            if separateRows or hasHeader:
                print >> output, rowSeparator
                hasHeader = False
        return output.getvalue()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号