1
2 """Analyzes lines with regular expressions"""
3
4 import re
5
6 from GeneralLineAnalyzer import GeneralLineAnalyzer
7
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 progressTemplate=None,
32 singleFile=False,
33 startTime=None,
34 endTime=None):
35 """
36 @param name: name of the expression (needed for output
37 @param exp: the regular expression, %f% will be replaced with the
38 regular expression for a float
39 @param idNr: number of the pattern group that is used as an identifier
40 @param titles: titles of the columns
41 @param accumulation: How multiple values should be accumulated
42 """
43 GeneralLineAnalyzer.__init__(self,
44 titles=titles,
45 doTimelines=doTimelines,
46 doFiles=doFiles,
47 accumulation=accumulation,
48 progressTemplate=progressTemplate,
49 singleFile=singleFile,
50 startTime=startTime,
51 endTime=endTime)
52
53 self.name=name
54 self.idNr=idNr
55
56 exp=exp.replace("%f%",self.floatRegExp)
57
58 self.strExp=exp
59 self.exp=re.compile(self.strExp)
60
61 self.data={}
62
64 self.tm=self.parent.getTime()
65 if self.tm=="":
66 self.tm="-1e10"
67
69 name=self.name
70 fdata=match.groups()
71 if self.idNr!=None:
72 ID=match.group(self.idNr)
73 name+="_"+ID
74 fdata=fdata[:self.idNr-1]+fdata[self.idNr:]
75 else:
76 ID=""
77
78 self.sub(ID)[float(self.tm)]=fdata
79 if ID!="":
80 self.sub("")[float(self.tm)]=match.groups()
81
82 self.files.write(name,self.tm,fdata)
83
85 name=self.name
86 fdata=match.groups()
87
88 prefix=""
89 if self.idNr!=None:
90 ID=match.group(self.idNr)
91 prefix=ID+"_"
92 fdata=fdata[:self.idNr-1]+fdata[self.idNr:]
93
94 for i in range(len(fdata)):
95 val=float(fdata[i])
96 name=prefix+"value %d" % i
97 if i<len(self.titles):
98 if self.idNr!=None and self.titles[i].find("%s")>=0:
99 name=self.titles[i] % ID
100 else:
101 name=prefix+str(self.titles[i])
102
103 self.lines.setValue(name,val)
104
106 """ get the data set for the identifier ID"""
107 if not self.data.has_key(ID):
108 self.data[ID]={}
109 return self.data[ID]
110
112 """get the available time for the identifier ID"""
113 if ID==None:
114 ID=""
115 return self.sub(ID).keys()
116
118 """get a list of the available IDs"""
119 ids=self.data.keys()
120 if "" in ids:
121 ids.remove("")
122 return ids
123
125 """get the last time for the identifier ID"""
126 times=self.getTimes(ID)
127 if len(times)>0:
128 return max(times)
129 else:
130 return None
131
132 - def getData(self,time=None,ID=None):
133 """get a data value at a specific time for a specific ID"""
134 if ID==None:
135 ID=""
136
137 if time==None:
138 time=self.getLast(ID)
139 else:
140 time=float(time)
141
142 data=self.sub(ID)
143
144 if data.has_key(time):
145 return data[time]
146 else:
147 return None
148
150 """Class that stores results as timelines, too"""
151
152 - def __init__(self,
153 name,
154 exp,
155 titles=[],
156 startTime=None,
157 endTime=None):
158 """
159 @param name: name of the expression (needed for output
160 @param exp: the regular expression, %f% will be replaced with the
161 regular expression for a float
162 @param titles: titles of the columns
163 """
164 RegExpLineAnalyzer.__init__(self,
165 name,
166 exp,
167 idNr=None,
168 titles=titles,
169 doTimelines=True,
170 doFiles=False,
171 startTime=startTime,
172 endTime=endTime)
173