CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
tablePrinter.py
Go to the documentation of this file.
1 #
2 # pretty table printer
3 # written by George Sakkis
4 # http://code.activestate.com/recipes/267662
5 #
6 import cStringIO,operator
7 from functools import reduce
8 def indent(rows,hasHeader=False,headerChar='-',delim=' | ',justify='center',
9  separateRows=False,prefix='',postfix='',wrapfunc=lambda x:x):
10  """
11  Indents a table by column.
12  - rows: A sequence of sequences of items, one sequence per row.
13  - hadHeader: True if the first row consists of the column's names.
14  - headerChar: Character to be used for the row separator line
15  (if hasHeader==True or separateRows==True).
16  - delim: The column delimiter.
17  - justify: Determines how are data justified in their column.
18  Valid values are 'left','right','center'.
19  - separateRows: True if rows are to be separated by a line of 'headerChar's.
20  - prefix: A string prepended to each printed row.
21  - postfix: A string appended to each printed row.
22  - wrapfunc: A function f(text) for wrapping text; each element in the table is first wrapped by this function.
23  """
24  #nested function
25  #closure for breaking logical rows to physical, using wrapfunc
26  def rowWrapper(row):
27  newRows=[wrapfunc(item).split('\n') for item in row]
28  #print 'newRows: ',newRows
29  #print 'map result: ',map(None,*newRows)
30  #print 'rowwrapped: ',[[substr or '' for substr in item] for item in map(None,*newRows)]
31  return [[substr or '' for substr in item] for item in map(None,*newRows)]
32  # break each logical row into one or more physical ones
33  logicalRows = [rowWrapper(row) for row in rows]
34  # columns of physical rows
35  columns = map(None,*reduce(operator.add,logicalRows))
36  # get the maximum of each column by the string length of its items
37  maxWidths = [max([len(str(item)) for item in column]) for column in columns]
38  rowSeparator = headerChar * (len(prefix) + len(postfix) + sum(maxWidths) + len(delim)*(len(maxWidths)-1))
39  # select the appropriate justify method
40  justify = {'center':str.center,'right':str.rjust,'left':str.ljust}[justify.lower()]
41  output=cStringIO.StringIO()
42  if separateRows: print >> output,rowSeparator
43  for physicalRows in logicalRows:
44  for row in physicalRows:
45  print >> output, prefix+delim.join([justify(str(item),width) for (item,width) in zip(row,maxWidths)])+postfix
46  if separateRows or hasHeader: print >> output, rowSeparator; hasHeader=False
47  return output.getvalue()
48 
49 if __name__ == '__main__':
50  from wordWrappers import wrap_always,wrap_onspace,wrap_onspace_strict
51  labels=('First Name','Last Name','Age','Position')
52  data="""John,Smith,24,Software Engineer
53  Mary,Brohowski,23,Sales Manager
54  Aristidis,Papageorgopoulos,28,Senior Reseacher"""
55  rows=[row.strip().split(',') for row in data.splitlines()]
56  print rows
57  print 'without wrapping function\n'
58  print 'raw input: ',[labels]+rows
59  print indent([labels]+rows,hasHeader=True)
60  width=10
61  for wrapper in (wrap_always,wrap_onspace,wrap_onspace_strict):
62  print 'Wrapping function: %s(x,width=%d)\n'%(wrapper.__name__,width)
63  print indent([labels]+rows,hasHeader=True,separateRows=True,prefix='| ',postfix=' |',wrapfunc=lambda x: wrapper(x,width))
64 
65  lumidata=[\
66  ('%-*s'%(8,'run'),'%-*s'%(8,'first'),'%-*s'%(8,'last'),'%-*s'%(10,'delivered'),'%-*s'%(10,'recorded'),'%-*s'%(20,'recorded\nmypathdfdafddafd')),\
67  ['%d'%(132440),'%d'%(23),'%d'%(99),'%.2f'%(2.345),'%.2f'%(1.23),'%.2f'%(0.5678)],\
68  ['%d'%(132442),'%d'%(1),'%d'%(20),'%.2f'%(2.345),'%.2f'%(1.23),'%.2f'%(0.5678)],\
69  ['','%d'%(27),'%d'%(43),'%.2f'%(2.345),'%.2f'%(1.23),'%.2f'%(0.5678)]\
70  ]
71  lumiheader=[('%-*s'%(30,'Lumi Sections'),'%-*s'%(46,'Luminosity'))]
72  headerwidth=46
73  print indent(lumiheader,hasHeader=True,separateRows=False,prefix='| ',postfix='',wrapfunc=lambda x: wrap_always(x,headerwidth))
74  lumifooter=[('%-*s'%(24,'189'),'%-*s'%(10,'17.89'),'%-*s'%(10,'16.1'),'%-*s'%(20,'3.47'))]
75  width=20
76  print indent(lumidata,hasHeader=True,separateRows=False,prefix='| ',postfix='',wrapfunc=lambda x: wrap_onspace_strict(x,width))
77  print
78  print indent(lumifooter,hasHeader=False,separateRows=True,prefix=' total: ',postfix='',delim=' | ',wrapfunc=lambda x: wrap_always(x,25))
def wrap_always
Definition: dataformats.py:71
OutputIterator zip(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp)
def wrap_onspace_strict
Definition: dataformats.py:64
double split
Definition: MVATrainer.cc:139
static HepMC::HEPEVT_Wrapper wrapper