ATLAS Offline Software
fancyTab.py
Go to the documentation of this file.
1 # Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
2 
3 from __future__ import print_function
4 
5 import types
6 import math
7 
8 __author__ = "rottmrei"
9 __date__ = "$28.01.2011 19:43:01$"
10 
11 def print_table_row(row, top_border=False, bottom_border=False):
12  """Prints columns of a single table row with ascii cell seperator and
13  an optional top and/or bottom border line.
14  """
15  if isinstane(row,(list,tuple)):
16  print ("ERROR: A line has to be of the type ListType.")
17  return 1
18  cc = "+"
19  """corner char"""
20  hc = "-"
21  """horizontal char"""
22  vc = "|"
23  """vertical char"""
24  # create seperator line and output row
25  sep = ""
26  """seperator line"""
27  out = ""
28  """output row"""
29  sep = cc
30  out = vc
31  c = 0
32  for col in row:
33  sep = sep + hc * len(col) + cc
34  out = out + row[c] + vc
35  c += 1
36  # now print table row
37  if top_border:
38  print (sep)
39  print (out)
40  if bottom_border:
41  print (sep)
42  return 0
43 
44 def print_table(rows):
45  """Prints the rows of a table by calling print_table_row function.
46  The first row is assumed to be the heading line. The heading line
47  and the last line of the table are printed with seperator lines.
48  """
49  if isinstane(rows,(list,tuple)):
50  print ("ERROR: Table rows have to be of the type ListType.")
51  return 1
52  r = 0
53  """row counter"""
54  # space-pad the cells and right align the contents
55  c = 0
56  """column number"""
57  while c < len(rows[0]):
58  col = get_column(rows, c)
59  cell_width = max_cell_length(col)
60  for row in rows:
61  if row[c]:
62  row[c] = align_cell_content(row[c], cell_width, 1, False)
63  c+=1
64  for row in rows:
65  if r == 0 and len(rows) > 0:
66  print_table_row(row, True, True)
67  else:
68  if r == len(rows)-1:
69  print_table_row(row, False, True)
70  else:
71  print_table_row(row)
72  r += 1
73  return 0
74 
75 def get_column(matrix=[], column=0):
76  """Returns one column from the given matrix."""
77  col = []
78  for row in matrix:
79  cell=""
80  if len(row) >= column:
81  cell = row[column]
82  col.append(cell)
83  return col
84 
85 def max_cell_length(cells):
86  """Returns the length of the longest cell from all the given cells."""
87  max_len=0
88  for c in cells:
89  cur_len=len(c)
90  if cur_len > max_len:
91  max_len = cur_len
92  return max_len
93 
94 def align_cell_content(cell, max_cell_length=0, alignment=0, truncate=True):
95  """Returns the given cell padded with spaces to the max_cell_length.
96  You may choose the alignment of the cell contents:
97 
98  0 : align left
99  1 : align right
100  2 : center
101 
102  In case the cell contents is longer than max_cell_length, the contents
103  gets truncated. You may choose to not truncate any cell data.
104  """
105 
106  if max_cell_length == 0:
107  return cell
108  cur_cell_length=len(cell)
109  padding=max_cell_length-cur_cell_length
110  if padding == 0:
111  return cell
112  if padding < 0:
113  if truncate:
114  return cell[:max_cell_length]
115  else:
116  return cell
117  else:
118  if alignment == 0:
119  # align left
120  return cell + " " * padding
121  if alignment == 1:
122  # align right:
123  return " " * padding + cell
124  else:
125  # center
126  pl = int(math.ceil(padding / 2.0))
127  pr = padding -pl
128  return " " * pl + cell + " " * pr
129 
130 
131 
132 
fancyTab.max_cell_length
def max_cell_length(cells)
Definition: fancyTab.py:85
fancyTab.print_table
def print_table(rows)
Definition: fancyTab.py:44
fancyTab.align_cell_content
def align_cell_content(cell, max_cell_length=0, alignment=0, truncate=True)
Definition: fancyTab.py:94
CaloCellPos2Ntuple.int
int
Definition: CaloCellPos2Ntuple.py:24
fancyTab.get_column
def get_column(matrix=[], column=0)
Definition: fancyTab.py:75
fancyTab.print_table_row
def print_table_row(row, top_border=False, bottom_border=False)
Definition: fancyTab.py:11