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()
评论列表
文章目录