我编写了一个简单的程序来读取日志并解析并获取最低的起始数字(头部)并打印出来。我正在编辑该程序,并将其与我编写的用于解析实际日志文件的类相结合。基本上,与基于我之前程序的日志中的简单数字进行排序相反,现在我需要将解析的信息从一个类引用到另一个类中。我想知道什么是最简单的方法来做到这一点。我是一名Python初学者程序员,不知道我是否可以明确地引用这个类。使用另一个班级的一个班级
这里是类。
分析器
class LogLine:
SEVERITIES = ['EMERG','ALERT','CRIT','ERR','WARNING','NOTICE','INFO','DEBUG']
severity = 1
def __init__(self, line):
try:
m = re.match(r"^(\d{4}-\d{2}-\d{2}\s*\d{2}:\d{2}:\d{2}),?(\d{3}),?(\s+\[(?:[^\]]+)\])+\s+[A-Z]+\s+(\s?[a-zA-Z0-9\.])+\s?(\((?:\s?\w)+\))\s?(\s?.)+", line)
timestr, msstr, sevstr, self.filename, linestr, self.message = m.groups()
self.line = int(linestr)
self.sev = self.SEVERITIES.index(sevstr)
self.time = float(calendar.timegm(time.strptime(timestr, "%Y-%m-%d %H:%M:%S,%f"))) + float(msstr)/1000.0
dt = datetime.strptime(t, "%Y-%m-%d %H:%M:%S,%f")
except Exception:
print 'error',self.filename
def get_time(self):
return self.time
def get_severity(self):
return self.sev
def get_message(self):
return self.message
def get_filename(self):
return self.filename
def get_line(self):
return self.line
分拣
class LogFile:
def __init__(self,filepath):
self.logfile = open(filepath, "r")
self.head = None
def __str__(self):
return "x=" + str(self.x) + "y="+str(self.y)
def readline(self):
if self.head != None:
h = self.head
self.head = None
return h
else:
return self.logfile.readline().rstrip(' ')
def get_line(self):
if self.head == None:
self.head = self.readline().rstrip(' ')
return self.head.get.line()
else:
return self.head.get.line()
def close (self):
self.logfile.close()
我已经开始通过增加get_line功能编辑我的第二课堂。不知道我是否在正确的轨道上。
简单来说,我需要的头变成了“的logline”
这是我需要的。感谢您的明确答案。为了跟进,我可以从课堂外调用类方法吗?例如,我写了一个函数来找到最低的t。 'DEF get_line_with_lowest_t(日志): lowest_i = -1 对于i在范围(LEN(日志)): 吨=日志[I] .get_t() 如果lowest_i == -1或叔
Raj
2012-07-27 21:30:36
是的,如果'logs'是'LogLine'实例列表,那么你可以使用'log [i] .get_line()'来调用''。 get_line()'方法函数存储在列表中的位置'i'处。 – steveha 2012-07-27 21:33:53
谢谢。你能否看看我的代码的一致性?如果有什么不对,你不需要修复它。我真的更关心学习如何构建现在。可以用pastebin链接向您发送电子邮件。如果没有,我感谢你的时间。 – Raj 2012-07-27 21:38:15