Bonus Exercise: Load employees from CVS

In the course 'Object Oriented Programming', the employee data is hardcoded in main.py. But what if you would like to load the employee data from a database or csv file? This bonus lab shows the code to load the employee list from a csv file.

Load employees from a csv file

Step 1: Create CSV file

Create new file 'employees.csv' in the project folder and copy and paste the following code in it:

Vera,Schmidt,2000,MA,M
Chuck,Norris,1800,ST,M
Samantha,Carrington,1800,ST,A
Roberto,Jacketti,2100,CO,M
Dave,Dreissig,2200,ME,M
Tina,River,2300,ME,M
Ringo,Rama,1900,ME,A
Chuck,Rainey,1800,ME,N

Each line has a first_name, last_name, salary, job_title and shift type.

Step 2: Create Storage Class

Create a new class called 'storage.py' and copy and paste the following code:

from employee import Manager
from employee import Attendant
from employee import Cook
from employee import Mechanic

from shift import MorningShift
from shift import AfternoonShift
from shift import NightShift

class Storage:
    def load_employees_from_csv(self, file_name):
        with open(file_name, 'r') as file:
            csv = file.read()
        csv_lines = csv.strip().split("\n")  # strips whitespace and splits on newlines

        employees = []
        for line in csv_lines:
            first_name, last_name, salary, job_title_code, shift_code = line.split(",")

            if shift_code == "M":
                shift = MorningShift()
            elif shift_code == "A":
                shift = AfternoonShift()
            elif shift_code == "N":
                shift = NightShift()

            if job_title_code == "MA":
                employees.append(Manager(first_name, last_name, salary, shift))
            elif job_title_code == "ST":
                employees.append(Attendant(first_name, last_name, salary, shift))
            elif job_title_code == "CO":
                employees.append(Cook(first_name, last_name, salary, shift))
            elif job_title_code == "ME":
                employees.append(Mechanic(first_name, last_name, salary, shift))

        return employees

Step 3: Replace the hard coded employees list.

Open main.py and remove the employee imports and the shift imports. Then import the storage class and use it. The finished main.py should look like this:

from reporting import AccountingReport
from reporting import StaffingReport
from reporting import ScheduleReport
from storage import Storage

storage = Storage()
employees = storage.load_employees_from_csv("employees.csv")

reports = [
    AccountingReport(employees),
    StaffingReport(employees),
    ScheduleReport(employees),
]

for r in reports:
    r.print_report()
    print()

Step 4: Test the code

Execute 'python main.py'. Here is the result:

Accounting Report
=================
Vera Schmidt, $2000
Chuck Norris, $1800
Samantha Carrington, $1800
Roberto Jacketti, $2100
Dave Dreissig, $2200
Tina River, $2300
Ringo Rama, $1900
Chuck Rainey, $1800

Staffing Report
===============
Vera Schmidt, Manager
Chuck Norris, Station attendant
Samantha Carrington, Station attendant
Roberto Jacketti, Cook
Dave Dreissig, Mechanic
Tina River, Mechanic
Ringo Rama, Mechanic
Chuck Rainey, Mechanic

Schedule
========
Vera Schmidt, 08:00 to 14:00
Chuck Norris, 08:00 to 14:00
Samantha Carrington, 12:00 to 20:00
Roberto Jacketti, 08:00 to 14:00
Dave Dreissig, 08:00 to 14:00
Tina River, 08:00 to 14:00
Ringo Rama, 12:00 to 20:00
Chuck Rainey, 14:00 to 22:00