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

Source Code for Module PyFoam.LogAnalysis.RegExpLineAnalyzer

  1  #  ICE Revision: $Id: RegExpLineAnalyzer.py 10069 2009-03-02 09:39:44Z 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 """ 33 @param name: name of the expression (needed for output 34 @param exp: the regular expression, %f% will be replaced with the 35 regular expression for a float 36 @param idNr: number of the pattern group that is used as an identifier 37 @param titles: titles of the columns 38 @param accumulation: How multiple values should be accumulated 39 """ 40 GeneralLineAnalyzer.__init__(self, 41 titles=titles, 42 doTimelines=doTimelines, 43 doFiles=doFiles, 44 accumulation=accumulation, 45 singleFile=singleFile) 46 47 self.name=name 48 self.idNr=idNr 49 50 exp=exp.replace("%f%",self.floatRegExp) 51 52 self.strExp=exp 53 self.exp=re.compile(self.strExp) 54 55 self.data={}
56
57 - def startAnalysis(self,match):
58 self.tm=self.parent.getTime() 59 if self.tm=="": 60 self.tm="-1e10"
61
62 - def addToFiles(self,match):
63 name=self.name 64 fdata=match.groups() 65 if self.idNr!=None: 66 ID=match.group(self.idNr) 67 name+="_"+ID 68 fdata=fdata[:self.idNr-1]+fdata[self.idNr:] 69 else: 70 ID="" 71 72 self.sub(ID)[float(self.tm)]=fdata 73 if ID!="": 74 self.sub("")[float(self.tm)]=match.groups() 75 76 self.files.write(name,self.tm,fdata)
77
78 - def addToTimelines(self,match):
79 name=self.name 80 fdata=match.groups() 81 82 for i in range(len(fdata)): 83 val=float(fdata[i]) 84 name="value %d" % i 85 if i<len(self.titles): 86 name=self.titles[i] 87 88 self.lines.setValue(name,val)
89
90 - def sub(self,ID):
91 """ get the data set for the identifier ID""" 92 if not self.data.has_key(ID): 93 self.data[ID]={} 94 return self.data[ID]
95
96 - def getTimes(self,ID=None):
97 """get the available time for the identifier ID""" 98 if ID==None: 99 ID="" 100 return self.sub(ID).keys()
101
102 - def getIDs(self):
103 """get a list of the available IDs""" 104 ids=self.data.keys() 105 if "" in ids: 106 ids.remove("") 107 return ids
108
109 - def getLast(self,ID=None):
110 """get the last time for the identifier ID""" 111 times=self.getTimes(ID) 112 if len(times)>0: 113 return max(times) 114 else: 115 return None
116
117 - def getData(self,time=None,ID=None):
118 """get a data value at a specific time for a specific ID""" 119 if ID==None: 120 ID="" 121 122 if time==None: 123 time=self.getLast(ID) 124 else: 125 time=float(time) 126 127 data=self.sub(ID) 128 129 if data.has_key(time): 130 return data[time] 131 else: 132 return None
133
134 -class RegExpTimeLineLineAnalyzer(RegExpLineAnalyzer):
135 """Class that stores results as timelines, too""" 136
137 - def __init__(self,name,exp,titles=[]):
138 """ 139 @param name: name of the expression (needed for output 140 @param exp: the regular expression, %f% will be replaced with the 141 regular expression for a float 142 @param titles: titles of the columns 143 """ 144 RegExpLineAnalyzer.__init__(self,name,exp,idNr=None,titles=titles,doTimelines=True,doFiles=False)
145