This project focused on processing login and logout events using Python to identify currently active users on different machines. The project involved sorting event data, tracking users through dictionaries and sets, resolving a logout-handling error, and generating a machine-wise user activity report.
The project demonstrates practical Python programming skills including function design, class construction, list processing, dictionary management, set operations, debugging, and formatted report generation.
The objective was to create a Python-based event tracking workflow that processes login and logout events, updates machine-user relationships, prevents runtime errors from invalid logout events, and generates a clean report of active users by machine.
This function returns the event date and is used as the sorting key before processing login and logout events.
def get_event_date(event):
return event.date
This function sorts events by date, creates a dictionary of machines, and updates active users based on login and logout activity.
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)
elif event.type == "logout":
machines[event.machine].discard(event.user)
return machines
The original version used remove(), which caused an error when a logout event appeared for a user who was not currently logged in. The updated version uses discard(), which safely avoids an error if the user is not present in the set.
This function prints only machines that currently have active users.
def generate_report(machines):
for machine, users in machines.items():
if len(users) > 0:
user_list = ", ".join(users)
print("{}: {}".format(machine, user_list))
The Event class stores event date, event type, machine name, and username for each activity record.
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
A list of login and logout events was created to simulate user activity across different machines.
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'),
]
The event list was processed to identify which users remained active on each machine.
users = current_users(events)
print(users)
{'webserver.local': {'lane'}, 'myworkstation.local': set(), 'mailserver.local': set()}
The final report displays only machines with active users.
generate_report(users)
webserver.local: lane
This project demonstrates how Python can be used to process system events, track user activity, identify active sessions, and generate useful reports for administrative or security monitoring purposes.
The debugging improvement from remove() to discard() shows practical problem-solving and safer event handling when processing inconsistent or unexpected activity logs.
The project successfully processed machine login and logout events, resolved a runtime error caused by unmatched logout activity, and generated a clean report showing active users by machine.
This project supports core Python automation skills relevant to system administration, security monitoring, event analysis, and cybersecurity workflow development.