pythonlanguage-concepts

incorrect output in PyCharm


This is my homework. When I run this code, I should get an output like " result [(arrivaltime:Mon Mar 9 14:10:00 2020, user:mary, session:42, event:click, usrfield:13)] " but I got an incorrect output which is " [<main.Log object at 0x0000000000522100>] " I do't understand where the problem is. Can anyone help, please?

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)

Solution

  • _str_ should be __str__ and _repr_ should be __repr__ for the program to override the functions.