Package PyFoam :: Package LogAnalysis :: Module RegExpLineAnalyzer
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.LogAnalysis.RegExpLineAnalyzer

  1  #  ICE Revision: $Id: /local/openfoam/Python/PyFoam/PyFoam/LogAnalysis/RegExpLineAnalyzer.py 6470 2010-04-16T13:30:45.122106Z bgschaid  $  
  2  """Analyzes lines with regular expressions""" 
  3   
  4  import re 
  5   
  6  from GeneralLineAnalyzer import GeneralLineAnalyzer 
  7   
8 -class RegExpLineAnalyzer(GeneralLineAnalyzer):
9 """Parses lines for an arbitrary regular expression 10 11 Only one data-set is stored per time-step 12 13 One pattern group of the RegExp can be used as a unique 14 identifier, so that more than one data-sets can be stored per 15 time-step 16 17 The string %f% in the regular expression is replaced with the 18 regular expression for a floating point number 19 """ 20 21 floatRegExp="[-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?" 22
23 - def __init__(self, 24 name, 25 exp, 26 idNr=None, 27 titles=[], 28 doTimelines=False, 29 doFiles=True, 30 accumulation=None, 31 singleFile=False, 32 startTime=None, 33 endTime=None):
34 """ 35 @param name: name of the expression (needed for output 36 @param exp: the regular expression, %f% will be replaced with the 37 regular expression for a float 38 @param idNr: number of the pattern group that is used as an identifier 39 @param titles: titles of the columns 40 @param accumulation: How multiple values should be accumulated 41 """ 42 GeneralLineAnalyzer.__init__(self, 43 titles=titles, 44 doTimelines=doTimelines, 45 doFiles=doFiles, 46 accumulation=accumulation, 47 singleFile=singleFile, 48 startTime=startTime, 49 endTime=endTime) 50 51 self.name=name 52 self.idNr=idNr 53 54 exp=exp.replace("%f%",self.floatRegExp) 55 56 self.strExp=exp 57 self.exp=re.compile(self.strExp) 58 59 self.data={}
60
61 - def startAnalysis(self,match):
62 self.tm=self.parent.getTime() 63 if self.tm=="": 64 self.tm="-1e10"
65
66 - def addToFiles(self,match):
67 name=self.name 68 fdata=match.groups() 69 if self.idNr!=None: 70 ID=match.group(self.idNr) 71 name+="_"+ID 72 fdata=fdata[:self.idNr-1]+fdata[self.idNr:] 73 else: 74 ID="" 75 76 self.sub(ID)[float(self.tm)]=fdata 77 if ID!="": 78 self.sub("")[float(self.tm)]=match.groups() 79 80 self.files.write(name,self.tm,fdata)
81
82 - def addToTimelines(self,match):
83 name=self.name 84 fdata=match.groups() 85 86 prefix="" 87 if self.idNr!=None: 88 ID=match.group(self.idNr) 89 prefix=ID+"_" 90 fdata=fdata[:self.idNr-1]+fdata[self.idNr:] 91 92 for i in range(len(fdata)): 93 val=float(fdata[i]) 94 name=prefix+"value %d" % i 95 if i<len(self.titles): 96 if self.idNr!=None and self.titles[i].find("%s")>=0: 97 name=self.titles[i] % ID 98 else: 99 name=prefix+str(self.titles[i]) 100 101 self.lines.setValue(name,val)
102
103 - def sub(self,ID):
104 """ get the data set for the identifier ID""" 105 if not self.data.has_key(ID): 106 self.data[ID]={} 107 return self.data[ID]
108
109 - def getTimes(self,ID=None):
110 """get the available time for the identifier ID""" 111 if ID==None: 112 ID="" 113 return self.sub(ID).keys()
114
115 - def getIDs(self):
116 """get a list of the available IDs""" 117 ids=self.data.keys() 118 if "" in ids: 119 ids.remove("") 120 return ids
121
122 - def getLast(self,ID=None):
123 """get the last time for the identifier ID""" 124 times=self.getTimes(ID) 125 if len(times)>0: 126 return max(times) 127 else: 128 return None
129
130 - def getData(self,time=None,ID=None):
131 """get a data value at a specific time for a specific ID""" 132 if ID==None: 133 ID="" 134 135 if time==None: 136 time=self.getLast(ID) 137 else: 138 time=float(time) 139 140 data=self.sub(ID) 141 142 if data.has_key(time): 143 return data[time] 144 else: 145 return None
146
147 -class RegExpTimeLineLineAnalyzer(RegExpLineAnalyzer):
148 """Class that stores results as timelines, too""" 149
150 - def __init__(self, 151 name, 152 exp, 153 titles=[], 154 startTime=None, 155 endTime=None):
156 """ 157 @param name: name of the expression (needed for output 158 @param exp: the regular expression, %f% will be replaced with the 159 regular expression for a float 160 @param titles: titles of the columns 161 """ 162 RegExpLineAnalyzer.__init__(self, 163 name, 164 exp, 165 idNr=None, 166 titles=titles, 167 doTimelines=True, 168 doFiles=False, 169 startTime=startTime, 170 endTime=endTime)
171