Package glue :: Package ligolw :: Package utils :: Module print_tables
[hide private]
[frames] | no frames]

Source Code for Module glue.ligolw.utils.print_tables

  1  # xml_convert.py 
  2   
  3  # 
  4  # ============================================================================= 
  5  # 
  6  #                                   Preamble 
  7  # 
  8  # ============================================================================= 
  9  # 
 10   
 11   
 12  """ 
 13  A collection of utilities to convert xml-tables to other formats, such as 
 14  wiki or html. 
 15  """ 
 16  import sys, re 
 17   
 18  from .. import ligolw 
 19  from .. import table 
 20  import six 
 21   
 22  __author__ = "Collin Capano <cdcapano@ligo.caltech.edu>" 
 23  from glue import git_version 
 24  __date__ = git_version.date 
 25  __version__ = git_version.id 
 26   
 27   
 28  # 
 29  # ============================================================================= 
 30  # 
 31  #                                 Utilities 
 32  # 
 33  # ============================================================================= 
 34  # 
 35   
36 -def set_output_format( output_format ):
37 """ 38 Sets output format; returns standard bits of table. These are: 39 ttx: how to start a title for a set of tables 40 xtt: how to end a title for a set of tables 41 tx: how to start a table 42 xt: how to close a table 43 capx: how to start a caption for the table 44 xcap: how to close a caption for the table 45 rx: how to start a row and the first cell in the row 46 xr: how to close a row and the last cell in the row 47 rspx: how to start a cell with a row span argument 48 xrsp: how to close the row span argument 49 cx: how to open a cell 50 xc: how to close a cell 51 """ 52 if output_format == 'wiki': 53 ttx = '== ' 54 xtt = ' ==' 55 tx = '' 56 xt = '' 57 capx = "'''" 58 xcap = "'''" 59 rx = '|' 60 xr = '|' 61 rspx = '|<|' 62 xrsp = '>' 63 cx = '|' 64 xc = '|' 65 hlx = '[' 66 hxl = ' ' 67 xhl = ']' 68 69 elif output_format == "html": 70 ttx = '<b>' 71 xtt = '</b><hr>' 72 tx = '<table border = "1">' 73 xt = '</table><br><br>' 74 capx = '<caption>' 75 xcap = '</caption>' 76 rx = '<tr>' 77 xr = '</tr>' 78 rspx = '<td rowspan=' 79 xrsp = '>' 80 cx = '<td>' 81 xc = '</td>' 82 hlx = '<a href="' 83 hxl = '">' 84 xhl = "</a>" 85 86 else: 87 raise ValueError("unrecognized output_format %s" % output_format) 88 89 return ttx, xtt, tx, xt, capx, xcap, rx, xr, cx, xc, rspx, xrsp, hlx, hxl, xhl
90 91
92 -def smart_round( val, decimal_places = 2 ):
93 """ 94 For floats >= 10.**-(decimal_places - 1), rounds off to the valber of decimal places specified. 95 For floats < 10.**-(decimal_places - 1), puts in exponential form then rounds off to the decimal 96 places specified. 97 @val: value to round; if val is not a float, just returns val 98 @decimal_places: number of decimal places to round to 99 """ 100 if isinstance(val, float) and val != 0.0: 101 if val >= 10.**-(decimal_places - 1): 102 conv_str = ''.join([ '%.', str(decimal_places), 'f' ]) 103 else: 104 conv_str = ''.join([ '%.', str(decimal_places), 'e' ]) 105 val = float( conv_str % val ) 106 107 return val
108 119
120 -def format_cell(val, round_floats = False, decimal_places = 2, format_links = False, 121 hlx = '', hxl = '', xhl = ''):
122 """ 123 Applys smart_round and format_hyperlink to values in a cell if desired. 124 """ 125 if round_floats: 126 val = smart_round(val, decimal_places = decimal_places) 127 if format_links: 128 val = format_hyperlink(val, hlx, hxl, xhl) 129 130 return val
131
132 -def format_header_cell(val):
133 """ 134 Formats given header column. This involves changing '_Px_' to '(', '_xP_' to ')' and 135 all other '_' to spaces. 136 """ 137 return re.sub('_', ' ', re.sub(r'(_Px_)', '(', re.sub(r'(_xP_)', ')', str(val) )))
138
139 -def get_row_data(row, column_name, cat_time_ns = True):
140 """ 141 Retrieves the requested column's data from the given row. 142 143 @cat_time_ns: If the column_name has "_time" in it, will concatenate 144 the column with any column having the same name but "_time_ns". 145 """ 146 column_name_ns = re.sub(r'_time', r'_time_ns', column_name) 147 try: 148 rowattrs = [attr for attr in row.__slots__] 149 except AttributeError: 150 rowattrs = [attr for attr in six.iterkeys(row.__dict__)] 151 152 if cat_time_ns and "_time" in column_name and column_name_ns in rowattrs: 153 return int(getattr(row, column_name)) + 10**(-9.)*int(getattr(row, column_name_ns)) 154 else: 155 return getattr(row, column_name)
156 # 157 # ============================================================================= 158 # 159 # Library API 160 # 161 # ============================================================================= 162 # 163 290