python read log file

分类:Python 2010-02-07 来源:CodeWeblog.com 人气:254

Using python language to read log file. And analysis of documents, calculation of the average with the average time for each firm, the longest, shortest time, statistics are not complete transaction (that is, a beginning not the end of the case) the total number and details

Part Source:
# -*- Coding: cp936 -*-
# Operating System: winxp
# Python version: 2.5

# Matching services and calculate
from datetime import datetime
from DTime import DTime
from DataConvert import DataConvert
class log:

def log (self):

# Put the data does not match the complete
temp = dict ()

# Store the data to be matched
tempTime = dict ()

# Storing data has been completed match
avg = dict ()

dt = DTime ()

i = 0

b = True

dc = DataConvert ()

items = dc.getData ()

strs = items.values ()

datas = items.keys ()

while b:
try:
# Determine whether there is already data in the previous
order = strs [i] [0]
key = strs [i] [5]
dtime = strs [i] [2]
if temp.has_key (key):
# Call time transfer function
usetime = dt.convertTime (tempTime.get (key))
othertime = dt.convertTime (dtime)
if usetime> othertime:
usetime = usetime-othertime
elif usetime <othertime:
usetime = othertime-usetime
else:
usetime = usetime-usetime
temp.pop (key)
tempTime.pop (key)
avg [order] = usetime
else:
tempTime [key] = dtime
for k in datas:
if items.get (k) == strs [i]:
temp [key] = k
i = i +1
except:
b = False
print 'dealt with', i, 'Article Data'

dc.storeTemp (temp)
return (avg, temp)

# X = log ()
# T = x.log ()

# -*- Coding: cp936 -*-
# Get the data from the log file, and process the data
from File import File
from filterData import filterData
class DataConvert:

def getData (self):

i = 0

b = True

self.f = File ()

data = dict ()

ls = self.f.loadLog ()

fd = filterData ()

strs = fd.filterStr (ls)

print 'finished loading the last did not match the data'
t = self.f.loadTemp ()

if t! = None:
if len (t)! = 0:
for value in t.values ():
strs.append (value)
print 'completed successfully added last not match the data', len (t), 'article'

# Decomposition data
while b:
try:
order = strs [i]. split ('', 1)
ip = order [1]. split ('[', 1)
dtime = ip [1]. split (']', 1)
desc = dtime [1]. split (':')
answer = desc [1]
key = strs [i]. split ('[')

if data.get (strs [i])! = strs [i]:
l = list ()
l.append (order [0])
l.append (ip [0])
l.append (dtime [0])
l.append (desc [0])
l.append (answer)
l.append (key [0])
data [strs [i]] = l
i = i +1
except:
b = False
print 'data conversion disposed of'
return data

def storeTemp (self, temp):
self.f.storeTemp (temp)

# Dc = DataConvert ()
# Dc.getData ()

# -*- Coding: cp936 -*-
# Time to change classes
from datetime import datetime
class DTime:
# Datetime (* (time.strptime (date_string, format) [0:6]))
def convertTime (self, dtime):
str = dtime.split ('')
d = str [0]. split ('-')
t = str [1]. split (':')
yy = d [0]
mm = d [1]
dd = d [2]
hh = t [0]
m = t [1]
ss = t [2]
return datetime (int (yy), int (mm), int (dd), int (hh), int (m), int (ss))

# -*- Coding: cp936 -*-
# Scan the log file
# Log to the new log file to be scanned
# Temp for the last match did not complete the scanning process of the data
import cPickle as p
class File:

def loadLog (self):
try:
f = open ('log', 'r')
strs = f.readlines ()
return strs
except:
print 'Load file failed, check the file exists'
finally:
f.close ()

def storeTemp (self, data):
try:
f = open ('temp', 'w')
p.dump (data, f)
f.close ()
except:
print 'did not match the complete data storage failure'
finally:
f.close ()

def loadTemp (self):
try:
f = open ('temp', 'r')
data = p.load (f)
return data
except:
print 'file, no data'
finally:
f.close ()

# F = File ()
# Print f.loadTempTime ()

# -*- Coding: cp936 -*-

# Filter the data is legitimate
class filterData:

def filterStr (self, strs):

i = 0

print 'total scan', len (strs), 'Article Data'

for spares in strs:
spare = spares.split ('')
if len (spare)! = 7:
i = i +1
strs.remove (spares)
if i! = 0:
print 'discarded', i, 'article failed data'
return strs

# Fd = filterData ()
# Fd.filterStr ()

# -*- Coding: cp936 -*-
# Report class
class Report:

def report (self, args):
avg = args [0]
leave = args [1]
values = avg.values ()
max = values [0]
min = values [0]
maxkeys = list ()
minkeys = list ()
# Find the max value and min value
for value in values:
if max <value:
max = value
if min> value:
min = value
keys = avg.keys ()
print'-------------------------- report --------------------- -------'
print '--- --- complete Affairs'
print 'complete affairs:', len (avg), 'a'
print 'details are as follows:'
for key in keys:
if avg.get (key) == max:
maxkeys.append (key)
if avg.get (key) == min:
minkeys.append (key)
# J = j +1
print 'affairs', key, 'the time:', avg.get (key)
print 'One of the most time with the same transaction:', len (maxkeys), 'a'
for maxkey in maxkeys:
print 'Affairs as', maxkey, 'the time:', avg.get (maxkey)
print 'in which the shortest time to the same transaction are:', len (minkeys), 'a'
for minkey in minkeys:
print 'Affairs as', minkey, 'the time:', avg.get (minkey)
print'------------------------------------------------ ----------'
print '--- incomplete transaction ----'
print 'not complete the transaction are:', len (leave), 'a'
print 'details are as follows:'
keys = leave.keys ()
for key in keys:
print '', leave.get (key)
print'-------------------------- end --------------------- --------'

# -*- Coding: cp936 -*-
# Test log class
import unittest
from report import Report
from log import log
class TestLog (unittest.TestCase):

def setUp (self):
self.r = Report ()
self.Log = log ()

def tearDown (self):
self.r = None
self.Log = None

def testLog (self):
test = self.Log.log ()
assert test! = None, 'tested'
# Print report
self.r.report (test)

def main (self):
suite = unittest.TestLoader (). loadTestsFromTestCase (TestLog)
unittest.TextTestRunner (). run (suite)

test = TestLog ("testLog")
test.main ()

分享到:
blog comments powered by Disqus

相关文章

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.302 (s). 14 q(s)