python read log file

sponsored links
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 ()
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of python read log file

  • Hibernate primary key strategy-sequence

    Today, the use of hibernate in the company encountered a troublesome problem, the use of hibernate when the primary key generation strategy set sequence, but always reported in the implementation could not get next sequence value of the error, then o ...

  • hibernate call stored procedure

    hibernate call stored procedure

  • hibernate using c3p0 connection pooling

    Private http://www.lifevv.com/tenyo/doc/20070605102040991.html c3p0 for open source's JDBC connection pool, with the release hibernate. This article describes how to use the hibernate configuration in c3p0. c3p0 connection pool configuration is v ...

  • Hibernate configuration parameters hibernate.hbm2ddl.auto

    Hibernate in the configuration file: <properties> <property name="hibernate.hbm2ddl.auto" value="create" /> </ properties> Parameter Description: validate load hibernate, the authentication to create a database t ...

  • Build flex + spring + blazeds + hibernate application

    Build flex + spring + blazeds + hibernate application First, set up the project blazeds 1, will blazeds.war extract to a directory, such as: myflex /; 2, set up java works were such as: MyFlex, in the orientation of selection create project from exis ...

  • Hibernate connection pool configuration

    Hibernate connection pool configuration <! - Jdbc -> <property name="connection.driver_class"> oracle.jdbc.driver.OracleDriver </ property> <property name="connection.url"> jdbc: oracle: thin: @ 10.203.14.132:15

  • hibernate generic generic DAO

    package org.lzpeng.dao; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Criterion; import org.springside.modules.orm.hibernate.Page; /** * * @version 2009-1-10 *

  • Struts2 + hibernate + spring problem user log in

    dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q

  • Hibernate secondary cache

    Hibernate cache: 2-bit cache, also known as process-level cache or SessionFactory level cache, secondary cache can be shared by all of the session Cache configuration and the use of: Will echcache.xml (the document code in hibernate package directory ...

  • Hibernate's lazy strategy

    hibernate Lazy strategy can be used in: <class> tag, it can be true / false Tags can <PROPERTY> values true / false type of necessary tools to enhance <set> <list> can tag values true / false / extra <many-to-one> <on ...

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries