这是我的家庭作业。当我运行这个代码时,我应该得到一个类似"result[(到达时间:2020年3月9日星期一14:10:00,用户:玛丽,会话:42,事件:点击,usrfield:13(]"但我得到了一个不正确的输出,它是"[lt;main.在0x0000000000522100>]处记录对象"我不知道问题出在哪里。有人能帮忙吗?
import time
import random
class Log:
def __init__(self, arrivaltime, user, session, event, usrfield):
self.arrivaltime = arrivaltime
self.user = user
self.session = session
self.event = event
self.usrfield = usrfield
def getArrivaltime(self):
return self.arrivaltime
def getUser(self):
return self.user
def getSession(self):
return self.session
def getEvent(self):
return self.event
def getUsrfield(self):
return self.usrfield
def _str_(self):
return "(arrivaltime:%s, user:%s, session:%d, event:%s, usrfield:%d)" %
(time.ctime(self.arrivaltime),
self.user,
self.session,
self.event,
self.usrfield)
def _repr_(self):
return "(arrivaltime:%s, user:%s, session:%d, event:%s, usrfield:%d)" %
(time.ctime(self.arrivaltime),
self.user,
self.session,
self.event,
self.usrfield)
def randomLogs(n):
stringmaxlen = 3
sessionmax = 1000
events = ['eventA', 'eventB', 'eventC', 'eventD', 'eventE']
arrivalmin = int(time.mktime(time.strptime('Mon Jan 1 12:00:00 2020')))
arrivalmax = int(time.mktime(time.strptime('Mon Mar 1 12:00:00 2020')))
usrfieldmax = 10000
chars = 'abcdefghijklmnprstuvyz'
logs = []
for i in range(1, n + 1):
arrival = random.randint(arrivalmin, arrivalmax)
user = ''
for x in range(0, stringmaxlen):
user += random.choice(chars)
session = random.randint(0, sessionmax)
event = random.choice(events)
usrfield = random.randint(0, usrfieldmax)
log = Log(arrival, user, session, event, usrfield)
logs.append(log)
return logs
def registerEqualQuery(logs, field):
def query(val1):
res = []
for (index, log) in enumerate(logs):
if getattr(log, field)() == val1:
res.append(log)
return res
if field not in ['getArrivaltime', 'getUser', 'getSession', 'getEvent', 'getUsrfield']:
raise Exception('field name not found')
return query
l1 = Log(int(time.mktime(time.strptime("Mon Mar 09 12:00:00 2020"))), "john", 23, "signin", 0)
l2 = Log(int(time.mktime(time.strptime("Mon Mar 09 14:10:00 2020"))), "mary", 42, "click", 13)
l3 = Log(int(time.mktime(time.strptime("Tue Mar 10 11:00:32 2020"))), "john", 23, "signout", 0)
l4 = Log(int(time.mktime(time.strptime("Wed Mar 11 22:10:00 2020"))), "viz", 350, "follow", 1)
l5 = Log(int(time.mktime(time.strptime("Fri Mar 13 03:33:02 2020"))), "admin12", 44, "signin", 0)
mylogs = [l1, l2, l3, l4, l5]
q1 = registerEqualQuery(mylogs, "getUser")
result = q1("mary")
print(result)
_str_
应为__str__
,_repr_
应为__repr__
,程序才能覆盖函数。