Welcome! Today, we are going to explore an engaging task that involves managing employee records within a company. Specifically, we will work with nested dictionaries and lists to add projects and tasks for employees and retrieve those tasks as needed. This exercise will help you understand how to manipulate nested data structures efficiently.
Let's start by discussing the methods we will implement in our EmployeeRecords
class.
add_project(self, employee_id: str, project_name: str) -> bool
- this method adds a new project to an employee's list of projects. If the project already exists for that employee, the method returnsFalse
. Otherwise, it adds the project and returnsTrue
.add_task(self, employee_id: str, project_name: str, task: str) -> bool
- this method adds a new task to a specified project for an employee. If the project does not exist for that employee, the method returnsFalse
. If the task is added successfully, it returnsTrue
.get_tasks(self, employee_id: str, project_name: str) -> list | None
- this method retrieves all tasks for a specified project of an employee. If the project does not exist for that employee, the method returnsNone
. Otherwise, it returns the list of tasks.
Now, let's build our EmployeeRecords
class step by step, ensuring we understand each component clearly.
We'll start with the basic structure of the class and initialize our data storage.
Python1class EmployeeRecords: 2 def __init__(self): 3 self.records = {} 4 5# Instantiate the class to ensure it works. 6records = EmployeeRecords()
In this initial setup, we define the EmployeeRecords
class and create an instance variable records
that is an empty dictionary. This dictionary will be used to store employee records, where each key is an employee ID and each value is another dictionary holding projects.
Next, we'll implement the add_project
method to add projects to an employee's record.
Python1class EmployeeRecords: 2 def __init__(self): 3 self.records = {} 4 5 def add_project(self, employee_id: str, project_name: str) -> bool: 6 if employee_id not in self.records: 7 self.records[employee_id] = {} 8 if project_name in self.records[employee_id]: 9 return False 10 else: 11 self.records[employee_id][project_name] = [] 12 return True 13 14# Example usage and testing 15records = EmployeeRecords() 16print(records.add_project("E123", "ProjectA")) # Returns True 17print(records.add_project("E123", "ProjectA")) # Returns False
Here, the add_project
method checks if the given employee_id
exists in the records
dictionary. If not, it initializes an empty dictionary for that employee. Then, it checks if the project_name
already exists for that employee. If it does, the method returns False
. Otherwise, it initializes an empty list for the project (to hold tasks) and returns True
.
Now, we will implement the add_task
method. This method relies on the availability of the existing project.
Python1class EmployeeRecords: 2 def __init__(self): 3 self.records = {} 4 5 def add_project(self, employee_id: str, project_name: str) -> bool: 6 if employee_id not in self.records: 7 self.records[employee_id] = {} 8 if project_name in self.records[employee_id]: 9 return False 10 else: 11 self.records[employee_id][project_name] = [] 12 return True 13 14 def add_task(self, employee_id: str, project_name: str, task: str) -> bool: 15 if employee_id not in self.records or project_name not in self.records[employee_id]: 16 return False 17 self.records[employee_id][project_name].append(task) 18 return True 19 20# Example usage and testing 21records = EmployeeRecords() 22records.add_project("E123", "ProjectA") 23print(records.add_task("E123", "ProjectA", "Task1")) # Returns True 24print(records.add_task("E123", "NonExistentProject", "Task3")) # Returns False
The add_task
method first checks if the employee_id
exists in the records
dictionary and if the specified project_name
exists for that employee. If either check fails, the method returns False
. Otherwise, it appends the task to the list of tasks for the specified project and returns True
.
Lastly, let's implement the get_tasks
method to retrieve tasks from a specified employee's project.
Python1class EmployeeRecords: 2 def __init__(self): 3 self.records = {} 4 5 def add_project(self, employee_id: str, project_name: str) -> bool: 6 if employee_id not in self.records: 7 self.records[employee_id] = {} 8 if project_name in self.records[employee_id]: 9 return False 10 else: 11 self.records[employee_id][project_name] = [] 12 return True 13 14 def add_task(self, employee_id: str, project_name: str, task: str) -> bool: 15 if employee_id not in self.records or project_name not in self.records[employee_id]: 16 return False 17 self.records[employee_id][project_name].append(task) 18 return True 19 20 def get_tasks(self, employee_id: str, project_name: str) -> list | None: 21 if employee_id not in self.records or project_name not in self.records[employee_id]: 22 return None 23 return self.records[employee_id][project_name] 24 25# Example usage and testing 26records = EmployeeRecords() 27records.add_project("E123", "ProjectA") 28records.add_task("E123", "ProjectA", "Task1") 29print(records.get_tasks("E123", "ProjectA")) # Returns ["Task1"] 30print(records.get_tasks("E123", "NonExistentProject")) # Returns None
The get_tasks
method checks if the employee_id
exists in the records
dictionary and if the specified project_name
exists for that employee. If either check fails, the method returns None
. Otherwise, it returns the list of tasks for the specified project.
In this lesson, we successfully implemented the EmployeeRecords
class for managing projects and tasks for employees using nested dictionaries and lists. We covered methods for adding projects, adding tasks to those projects, and retrieving tasks from those projects.
Understanding how to work with nested data structures allows you to efficiently manage complex data hierarchies, which improves your programming skills and problem-solving abilities. I encourage you to practice similar challenges to reinforce what you learned today.
Keep coding and exploring new challenges!