6 import cStringIO,operator
7 def indent(rows,hasHeader=False,headerChar='-',delim=' | ',justify='center',
8 separateRows=
False,prefix=
'',postfix=
'',wrapfunc=
lambda x:x):
10 Indents a table by column.
11 - rows: A sequence of sequences of items, one sequence per row.
12 - hadHeader: True if the first row consists of the column's names.
13 - headerChar: Character to be used for the row separator line
14 (if hasHeader==True or separateRows==True).
15 - delim: The column delimiter.
16 - justify: Determines how are data justified in their column.
17 Valid values are 'left','right','center'.
18 - separateRows: True if rows are to be separated by a line of 'headerChar's.
19 - prefix: A string prepended to each printed row.
20 - postfix: A string appended to each printed row.
21 - wrapfunc: A function f(text) for wrapping text; each element in the table is first wrapped by this function.
26 newRows=[wrapfunc(item).
split(
'\n')
for item
in row]
30 return [[substr
or '' for substr
in item]
for item
in map(
None,*newRows)]
32 logicalRows = [rowWrapper(row)
for row
in rows]
34 columns =
map(
None,*reduce(operator.add,logicalRows))
36 maxWidths = [
max([len(str(item))
for item
in column])
for column
in columns]
37 rowSeparator = headerChar * (len(prefix) + len(postfix) + sum(maxWidths) + len(delim)*(len(maxWidths)-1))
39 justify = {
'center':str.center,
'right':str.rjust,
'left':str.ljust}[justify.lower()]
40 output=cStringIO.StringIO()
41 if separateRows:
print >> output,rowSeparator
42 for physicalRows
in logicalRows:
43 for row
in physicalRows:
44 print >> output, prefix+delim.join([justify(str(item),width)
for (item,width)
in zip(row,maxWidths)])+postfix
45 if separateRows
or hasHeader:
print >> output, rowSeparator; hasHeader=
False
46 return output.getvalue()
48 if __name__ ==
'__main__':
49 from wordWrappers
import wrap_always,wrap_onspace,wrap_onspace_strict
50 labels=(
'First Name',
'Last Name',
'Age',
'Position')
51 data=
"""John,Smith,24,Software Engineer
52 Mary,Brohowski,23,Sales Manager
53 Aristidis,Papageorgopoulos,28,Senior Reseacher"""
54 rows=[row.strip().
split(
',')
for row
in data.splitlines()]
56 print 'without wrapping function\n'
57 print 'raw input: ',[labels]+rows
58 print indent([labels]+rows,hasHeader=
True)
60 for wrapper
in (wrap_always,wrap_onspace,wrap_onspace_strict):
61 print 'Wrapping function: %s(x,width=%d)\n'%(wrapper.__name__,width)
62 print indent([labels]+rows,hasHeader=
True,separateRows=
True,prefix=
'| ',postfix=
' |',wrapfunc=
lambda x:
wrapper(x,width))
65 (
'%-*s'%(8,
'run'),
'%-*s'%(8,
'first'),
'%-*s'%(8,
'last'),
'%-*s'%(10,
'delivered'),
'%-*s'%(10,
'recorded'),
'%-*s'%(20,
'recorded\nmypathdfdafddafd')),\
66 [
'%d'%(132440),
'%d'%(23),
'%d'%(99),
'%.2f'%(2.345),
'%.2f'%(1.23),
'%.2f'%(0.5678)],\
67 [
'%d'%(132442),
'%d'%(1),
'%d'%(20),
'%.2f'%(2.345),
'%.2f'%(1.23),
'%.2f'%(0.5678)],\
68 [
'',
'%d'%(27),
'%d'%(43),
'%.2f'%(2.345),
'%.2f'%(1.23),
'%.2f'%(0.5678)]\
70 lumiheader=[(
'%-*s'%(30,
'Lumi Sections'),
'%-*s'%(46,
'Luminosity'))]
72 print indent(lumiheader,hasHeader=
True,separateRows=
False,prefix=
'| ',postfix=
'',wrapfunc=
lambda x:
wrap_always(x,headerwidth))
73 lumifooter=[(
'%-*s'%(24,
'189'),
'%-*s'%(10,
'17.89'),
'%-*s'%(10,
'16.1'),
'%-*s'%(20,
'3.47'))]
75 print indent(lumidata,hasHeader=
True,separateRows=
False,prefix=
'| ',postfix=
'',wrapfunc=
lambda x:
wrap_onspace_strict(x,width))
77 print indent(lumifooter,hasHeader=
False,separateRows=
True,prefix=
' total: ',postfix=
'',delim=
' | ',wrapfunc=
lambda x:
wrap_always(x,25))
const T & max(const T &a, const T &b)
static HepMC::HEPEVT_Wrapper wrapper