ATLAS Offline Software
table_printer.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
2 #coding: UTF8
3 """
4 Prints out a table, padded to make it pretty.
5 
6 call pprint_table with an output (e.g. sys.stdout, cStringIO, file)
7 and table as a list of lists. Make sure table is "rectangular" -- each
8 row has the same number of columns.
9 
10 MIT License
11 
12 Taken from:
13 http://ginstrom.com/scribbles/2007/09/04/pretty-printing-a-table-in-python/
14 
15 Found with the google search "python print table" March 2010
16 
17 Minor modifications by <peter.waller@cern.ch> to include a "-" seperator for
18 the header, and to change the number format code.
19 
20 """
21 
22 __version__ = "0.1"
23 __author__ = "Ryan Ginstrom"
24 
25 import sys
26 
27 from DQUtils.ext.thousands import splitThousands
28 
29 from six import print_
30 
31 def format_num(num):
32  """Format a number according to given places.
33  Adds commas, etc."""
34  from builtins import int
35  if isinstance(num, int):
36  return splitThousands(num)
37 
38  return str(num)
39 
40 def get_max_width(table, index):
41  """Get the maximum width of the given column index
42  """
43 
44  return max([len(format_num(row[index])) for row in table])
45 
46 def pprint_table_to(out, table, header_loc=1):
47  """Prints out a table of data, padded for alignment
48 
49  @param out: Output stream ("file-like object")
50  @param table: The table to print. A list of lists. Each row must have the same
51  number of columns.
52 
53  """
54 
55  col_paddings = []
56 
57  for i in range(len(table[0])):
58  col_paddings.append(get_max_width(table, i))
59 
60  for i, row in enumerate(table):
61  if i == header_loc:
62  print_("-" * (sum(col_paddings) + (len(col_paddings)*3-1)), end='', file=out)
63  # left col
64  print_(row[0].ljust(col_paddings[0] + 2), end='', file=out)
65  # rest of the cols
66  for i in range(1, len(row)):
67  col = format_num(row[i]).rjust(col_paddings[i] + 2)
68  print_(col, end='', file=out)
69  print_(file=out)
70 
71 def pprint_table(table, header_loc=1):
72  pprint_table_to(sys.stdout, table, header_loc)
73 
74 def pprint_table_string(table, header_loc=1):
75  from cStringIO import StringIO
76  sio = StringIO()
77  pprint_table_to(sio, table, header_loc)
78  return sio.getvalue()
79 
80 if __name__ == "__main__":
81  table = [["", "taste", "land speed", "life"],
82  ["spam", 300101, 4, 1003],
83  ["eggs", 105, 13, 42],
84  ["lumberjacks", 13, 105, 10]]
85 
86  pprint_table(table)
max
#define max(a, b)
Definition: cfImp.cxx:41
python.ext.table_printer.pprint_table
def pprint_table(table, header_loc=1)
Definition: table_printer.py:71
python.ext.table_printer.get_max_width
def get_max_width(table, index)
Definition: table_printer.py:40
python.ext.table_printer.pprint_table_string
def pprint_table_string(table, header_loc=1)
Definition: table_printer.py:74
python.ext.table_printer.format_num
def format_num(num)
Definition: table_printer.py:31
convertTimingResiduals.sum
sum
Definition: convertTimingResiduals.py:55
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
python.ext.thousands.splitThousands
def splitThousands(s, tSep=',', dSep='.')
Definition: thousands.py:12
python.ext.table_printer.pprint_table_to
def pprint_table_to(out, table, header_loc=1)
Definition: table_printer.py:46
str
Definition: BTagTrackIpAccessor.cxx:11