Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Question The following Python code returns an error when a user ('chris') that was logged out was never logged in. How do I update the

Question

The following Python code returns an error when a user ('chris') that was logged out was never logged in. How do I update the code to remove the error?

def get_event_date(event):

return event.date

def current_users(events):

events.sort(key=get_event_date)

machines = {}

for event in events:

if event.machine not in machines:

machines[event.machine] = set()

if event.type == "login":

machines[event.machine].add(event.user)

if event.type == "logout":

machines[event.machine].remove(event.user)

return machines

def generate_report(machines):

for machine, users in machines.items():

if len(users) > 0:

user_list = ", ".join(users)

print("{}: {}".format(machine, user_list))

class Event:

def __init__(self, event_date, event_type, machine_name, user):

self.date = event_date

self.type = event_type

self.machine = machine_name

self.user = user

events = [

Event('2020-01-21 12:45:56', 'login', 'myworkstation.local', 'jordan'),

Event('2020-01-22 15:53:42', 'logout', 'webserver.local', 'jordan'),

Event('2020-01-21 18:53:21', 'login', 'webserver.local', 'lane'),

Event('2020-01-22 10:25:34', 'logout', 'myworkstation.local', 'jordan'),

Event('2020-01-21 08:20:01', 'login', 'webserver.local', 'jordan'),

Event('2020-01-23 11:24:35', 'logout', 'mailserver.local', 'chris'),

]

users = current_users(events)

print(users)

generate_report(users)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696

Students also viewed these Programming questions