NeoPZ
rdt.py
Go to the documentation of this file.
1 #! /usr/bin/env python2.7
2 #***************************************************************************
3 #* Copyright (C) 2013 by Edson Borin *
4 #* edson@ic.unicamp.br *
5 #* *
6 #* This program is free software; you can redistribute it and/or modify *
7 #* it under the terms of the GNU General Public License as published by *
8 #* the Free Software Foundation; either version 2 of the License, or *
9 #* (at your option) any later version. *
10 #* *
11 #* This program is distributed in the hope that it will be useful, *
12 #* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 #* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 #* GNU General Public License for more details. *
15 #* *
16 #* You should have received a copy of the GNU General Public License *
17 #* along with this program; if not, write to the *
18 #* Free Software Foundation, Inc., *
19 #* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
20 #**************************************************************************/
21 
22 import sys
23 import os.path
24 
25 class RdtError(Exception):
26  def __init__(self, msg):
27  self.msg = msg
28  def __str__(self):
29  return self.msg
30 
31 # Handles RDT (raw data table) files
32 # TODO: Improve the description...
33 #
34 # The RDT file is a CSV (comma separated value) file.
35 # The first row contains the headers of the columns.
36 # The remaining rows contains the data for each row. Example:
37 # RUN, ELAPSED
38 # 1, 12.2
39 # 2, 43.2
40 # 3, 87.3
41 
42 # Read a RDT file into a dictionary.
43 # The dictionary is indexed by the column index.
44 # The value is a 2-entry tuple describing the column header and the list of
45 # values at the column
46 def read(filename):
47  # Check configuration file
48  if not os.path.isfile(filename):
49  raise RdtError(filename+' is not a valid rdt file.')
50  d={}
51  with open(filename) as f:
52  # Process the first row to setup the headers
53  hlist=f.readline().split(',')
54  hi=1
55  for h in hlist:
56  header = h.strip()
57  d[hi] = (header,[])
58  hi=hi+1
59  # Process the remaining rows
60  for line in f:
61  vlist = line.split(',')
62  first = vlist[1]
63  if first.strip() != "#":
64  hi=1
65  for v in vlist:
66  d[hi][1].append(float(v.strip()))
67  hi=hi+1
68  return d
69 
70 def get_column_values(rdt_d,field):
71  for k, v in rdt_d.iteritems():
72  if v[0] == field :
73  return v[1]
74  return []
75 
76 # Functions for stand alone tests
77 def usage():
78  print "\nUsage: rdt.py -i input.rdt [-a] [-f field] [-h]\n"
79  print "\nARGUMENTS"
80  print "\t-i input.rdt: input rdt file."
81  print "\t-f field: print selected column."
82  print "\t-a : print all columns."
83  print "\t-h : this help :-)."
84  print "\nDESCRIPTION"
85  print "\tReads the contents of a RDT file and prints its contents."
86  sys.exit(1)
87 
88 def error(message, status):
89  sys.stderr.write('ERROR: '+message+'\n')
90  sys.exit(status)
91 
92 # Main - for stand alone tests only
93 if __name__ == "__main__":
94  import getopt
95  inputfn=0
96  field=0
97  printa=False
98  opts, extra_args = getopt.getopt(sys.argv[1:], 'i:f:ah')
99  for f, v in opts:
100  if f == '-i': inputfn=v
101  elif f == '-f': field=v
102  elif f == '-a': printa=True
103  elif f == '-h': usage()
104 
105  if inputfn == 0:
106  error("An input file name must be provided.", 1)
107 
108  try:
109  rdt_d = read(inputfn)
110  except RdtError, e:
111  error(str(e), 1)
112 
113  if printa :
114  print "-- All values ---------"
115  print rdt_d
116  print "-----------------------"
117 
118  if field != 0 :
119  values = get_column_values(rdt_d,field)
120  print field, " : ", values
def __str__(self)
Definition: rdt.py:28
def error(message, status)
Definition: rdt.py:88
def get_column_values(rdt_d, field)
Definition: rdt.py:70
def read(filename)
Definition: rdt.py:46
def __init__(self, msg)
Definition: rdt.py:26
def usage()
Definition: rdt.py:77