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