다음은 이전에 작성한 로그 파서의 변형본으로 사용자의 로그 형식에 맞게 조정 된 것입니다. (일반적인 접근법은 Jim Dennis의 설명과 매우 비슷하게 추적되지만 모든 세션에 대해 모든 항목을 누적하기 위해 목록의 기본값을 사용했습니다.) 데이터를 저장하기위한
from pyparsing import Suppress,Word,nums,restOfLine
from datetime import datetime
from collections import defaultdict
def convertToDateTime(tokens):
month,day,year,hh,mm,ss = tokens
return datetime(year+2000, month, day, hh,mm,ss)
# define building blocks for parsing and processing log file entries
SLASH,COLON = map(Suppress,"/:")
integer = Word(nums).setParseAction(lambda t:int(t[0]))
date = integer + (SLASH + integer)*2
time = integer + (COLON + integer)*2
timestamp = date + time
timestamp.setParseAction(convertToDateTime)
# define format of a single line in the log file
logEntry = timestamp("timestamp") + integer("sessionid") + restOfLine("descr")
# summarize calls into single data structure
calls = defaultdict(list)
for logline in log:
entry = logEntry.parseString(logline)
calls[entry.sessionid].append(entry)
# first pass to find start/end time for each call
for sessionid in sorted(calls):
calldata = calls[sessionid]
print sessionid, calldata[-1].timestamp - calldata[0].timestamp
을,이 출력합니다 :
1234 0:00:10
당신은 하위 거래를 떨어져 애타게 유사한 방식으로 항목의 각 세션의 목록을 처리 할 수 있습니다.