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

Source Code for Module PyFoam.LogAnalysis.RegExpLineAnalyzer

  1  """Analyzes lines with regular expressions""" 
  2   
  3  import re 
  4   
  5  # from FileLineAnalyzer import FileLineAnalyzer 
  6  # from TimeLineLineAnalyzer import TimeLineLineAnalyzer 
  7   
  8  from GeneralLineAnalyzer import GeneralLineAnalyzer 
  9   
10 -class RegExpLineAnalyzer(GeneralLineAnalyzer):
11 """Parses lines for an arbitrary regular expression 12 13 Only one data-set is stored per time-step 14 15 One pattern group of the RegExp can be used as a unique 16 identifier, so that more than one data-sets can be stored per 17 time-step 18 19 The string %f% in the regular expression is replaced with the 20 regular expression for a floating point number 21 """ 22 23 floatRegExp="[-+]?[0-9]*\.?[0-9]+(?:[eE][-+]?[0-9]+)?" 24
25 - def __init__(self,name,exp,idNr=None,titles=[],doTimelines=False,doFiles=True):
26 """ 27 @param name: name of the expression (needed for output 28 @param exp: the regular expression, %f% will be replaced with the 29 regular expression for a float 30 @param idNr: number of the pattern group that is used as an identifier 31 @param titles: titles of the columns 32 """ 33 GeneralLineAnalyzer.__init__(self,titles=titles,doTimelines=doTimelines,doFiles=doFiles) 34 35 self.name=name 36 self.idNr=idNr 37 38 exp=exp.replace("%f%",self.floatRegExp) 39 40 self.strExp=exp 41 self.exp=re.compile(self.strExp) 42 43 self.data={}
44
45 - def startAnalysis(self,match):
46 self.tm=self.parent.getTime() 47 if self.tm=="": 48 self.tm="-1e10"
49
50 - def addToFiles(self,match):
51 name=self.name 52 fdata=match.groups() 53 if self.idNr!=None: 54 ID=match.group(self.idNr) 55 name+="_"+ID 56 fdata=fdata[:self.idNr-1]+fdata[self.idNr:] 57 else: 58 ID="" 59 60 self.sub(ID)[float(self.tm)]=fdata 61 if ID!="": 62 self.sub("")[float(self.tm)]=match.groups() 63 64 self.files.write(name,self.tm,fdata)
65
66 - def addToTimelines(self,match):
67 name=self.name 68 fdata=match.groups() 69 70 for i in range(len(fdata)): 71 val=float(fdata[i]) 72 name="value %d" % i 73 if i<len(self.titles): 74 name=self.titles[i] 75 76 self.lines.setValue(name,val)
77
78 - def sub(self,ID):
79 """ get the data set for the identifier ID""" 80 if not self.data.has_key(ID): 81 self.data[ID]={} 82 return self.data[ID]
83
84 - def getTimes(self,ID=None):
85 """get the available time for the identifier ID""" 86 if ID==None: 87 ID="" 88 return self.sub(ID).keys()
89
90 - def getIDs(self):
91 """get a list of the available IDs""" 92 ids=self.data.keys() 93 if "" in ids: 94 ids.remove("") 95 return ids
96
97 - def getLast(self,ID=None):
98 """get the last time for the identifier ID""" 99 times=self.getTimes(ID) 100 if len(times)>0: 101 return max(times) 102 else: 103 return None
104
105 - def getData(self,time=None,ID=None):
106 """get a data value at a specific time for a specific ID""" 107 if ID==None: 108 ID="" 109 110 if time==None: 111 time=self.getLast(ID) 112 else: 113 time=float(time) 114 115 data=self.sub(ID) 116 117 if data.has_key(time): 118 return data[time] 119 else: 120 return None
121
122 -class RegExpTimeLineLineAnalyzer(RegExpLineAnalyzer):
123 """Class that stores results as timelines, too""" 124
125 - def __init__(self,name,exp,titles=[]):
126 """ 127 @param name: name of the expression (needed for output 128 @param exp: the regular expression, %f% will be replaced with the 129 regular expression for a float 130 @param titles: titles of the columns 131 """ 132 RegExpLineAnalyzer.__init__(self,name,exp,idNr=None,titles=titles,doTimelines=True,doFiles=False)
133