python RE and list processing -
i've file below
elapsedtime2.68s: placeorder elapsedtime2.69s: classarestcall elapsedtime0.11s: getorderlist elapsedtime0.11s: classarestcall elapsedtime2.10s: placeorder elapsedtime2.11s: classarestcall elapsedtime0.10s: getorderlist elapsedtime0.10s: classarestcall elapsedtime2.00s: placeorder elapsedtime2.01s: classarestcall elapsedtime0.28s: getorderlist elapsedtime0.28s: classarestcall elapsedtime1.64s: placeorder elapsedtime1.65s: classarestcall elapsedtime0.11s: getorderlist elapsedtime0.11s: classarestcall elapsedtime1.99s: placeorder elapsedtime2.01s: classarestcall
how can parse file result?
average min max classarestcall 1.23 0.1 2.69 getorderlist 0.15 0.1 0.28 placeorder 2.082 1.64 2.68
i've developed 1 approach solve using re , list operation. approach scan whole list once every new method name.
how can stats api names scanning list once.
import re def get_stats(n, p_api): list_of_rt = [] line in n: y= re.split("\s+", line) if y[1] == p_api: curr_rt = float(y[0][11:-2]) list_of_rt.append(curr_rt) min_rt ,max_rt = min(list_of_rt), max(list_of_rt) total_rt, total_cnt = sum(list_of_rt), len(list_of_rt) print p_api, min_rt, max_rt, "%.3f" %round(total_rt/total_cnt,3), total_cnt ifile = open('data1.txt','r').read() api_rts= re.findall(r'elapsedtime\d*.\d*s: \s*',ifile) list_of_api_names = [] api_rt in api_rts: y= re.split("\s+", api_rt) list_of_api_names.append(y[1]) #get distinct list of api names distinct_apis = set(list_of_api_names) print 'api min, max, average, total occurences' # each api name call get_stat api in distinct_apis: get_stats(api_rts ,api)
import re rgx = re.compile('elapsedtime(\d*\.\d*)s: (\s*)') collections import defaultdict d = defaultdict(list) open('data1.txt','r') f: m in rgx.finditer(f.read()): d[m.group(2)].append(float(m.group(1))) lapi = max(map(len,d.iterkeys())) print '{: ^{width}} min max average total occurences'.format('api',width=lapi) pat = '{0:%d} {1:.2f} {2:.2f} {3:.3f} {4}' % lapi print '\n'.join(pat.format(api,min(li),max(li),sum(li)/len(li),len(li)) api,li in d.iteritems())
Comments
Post a Comment