Skip to content

Commit

Permalink
refactor to include global vars for easy modification of program
Browse files Browse the repository at this point in the history
  • Loading branch information
carmen904c committed Feb 7, 2024
1 parent d665c6c commit c7534f5
Show file tree
Hide file tree
Showing 10 changed files with 197 additions and 201 deletions.
2 changes: 1 addition & 1 deletion tests/scheduler_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def setUpClass(cls):
def test_import_individual_functions(self):
with patch('builtins.input', return_value='test_data/individuals.csv'):
self.scheduler.import_volunteers()
self.assertEqual(len(self.scheduler.individuals), 360)
self.assertEqual(len(self.scheduler.volunteers), 360)

with patch('builtins.input', return_value='test_data/partners.csv'):
self.scheduler.import_partners()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def setUpClass(cls):
def test_import_individual_functions(self):
with patch('builtins.input', return_value='test_data/individuals.csv'):
self.scheduler.import_volunteers()
self.assertEqual(len(self.scheduler.individuals), 360)
self.assertEqual(len(self.scheduler.volunteers), 360)

with patch('builtins.input', return_value='test_data/partners.csv'):
self.scheduler.import_partners()
Expand Down
35 changes: 34 additions & 1 deletion vsvs_scheduler/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,34 @@
COLUMN_NAMES
from enum import Enum

class TEACHER_COLUMNS(Enum):
NAME = 'Name'
PHONE = 'Cell Phone Number'
SCHOOL = 'School'
EMAIL = 'Email Address'
NUM_CLASSES = 'Number of Classes'

class VOLUNTEER_COLUMNS(Enum):
FIRST_NAME = 'First Name'
LAST_NAME = 'Last Name'
PHONE = 'Phone'
EMAIL = 'Email Address'
LEADER = 'Team Leader'
BOARD = 'Board Member'

class PARTNER_COLUMNS(Enum):
NUM_PARTNERS = 'Number of Partners'
EMAIL = 'Email Address'


CLASSROOM_RAW_DATA_FILE = "data/classrooms.csv"
VOLUNTEER_RAW_DATA_FILE = "data/individuals.csv"
PARTNER_RAW_DATA_FILE = "data/partners.csv"

ASSIGNMENTS_DIRECTORY = "results"


EARLIEST_TIME = "7:15"
LATEST_TIME = "15:30"
TIME_BLOCK_DURATION = 15
MAX_TEAM_SIZE = 5
MIN_TEAM_SIZE = 3
52 changes: 0 additions & 52 deletions vsvs_scheduler/abstract_data_uploader.py

This file was deleted.

48 changes: 0 additions & 48 deletions vsvs_scheduler/class_data_uploader.py

This file was deleted.

129 changes: 129 additions & 0 deletions vsvs_scheduler/data_uploader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import csv
import os
from applicants.teacher import Teacher
from applicants.classroom import Classroom
from applicants.volunteer import Volunteer
from applicants.schedule import Schedule
from applicants.partners import Partners
from __init__ import CLASSROOM_RAW_DATA_FILE, TEACHER_COLUMNS, VOLUNTEER_COLUMNS, VOLUNTEER_RAW_DATA_FILE, PARTNER_RAW_DATA_FILE, PARTNER_COLUMNS


class DataUploader():
def __init__(self):

self.classrooms = []
self.partners = []
self.volunteers = []
self.partners_not_found = []
self.import_data()



def import_data(self) -> str:
""" Prompt user for file path to csv/excel file."""

files = [CLASSROOM_RAW_DATA_FILE, VOLUNTEER_RAW_DATA_FILE, PARTNER_RAW_DATA_FILE]

for file_name in files:
if (not os.path.isfile(file_name)):
raise FileNotFoundError(f"File not found at {file_name}")

print (f"\nImporting {file_name} ...")

with open(file_name) as current_csv:
# DictReader creates an ordered dictionary for each row in the csv file
csv_reader = csv.DictReader(current_csv)

if file_name is CLASSROOM_RAW_DATA_FILE:
for row in csv_reader:
self.process_classroom_data(row)
elif file_name is VOLUNTEER_RAW_DATA_FILE:
for row in csv_reader:
self.process_volunteer_data(row)
elif file_name is PARTNER_RAW_DATA_FILE:
for row in csv_reader:
self.process_partner_data(row)
else:
raise ValueError(f"Invalid file name: {file_name}")


def process_classroom_data(self, row: dict):
""" Process row data from csv/excel file into Classroom objects."""
group_num = 0

number_of_classes = int(row[TEACHER_COLUMNS.NUM_CLASSES.value])

teacher = Teacher (
name = row[TEACHER_COLUMNS.NAME.value],
phone = row[TEACHER_COLUMNS.PHONE.value],
school = row[TEACHER_COLUMNS.SCHOOL.value],
email = row[TEACHER_COLUMNS.EMAIL.value],
weekday_preferences=[
row[f'Days (Class {number_of_classes} of {number_of_classes}) [1st Preference]'],
row[f'Days (Class {number_of_classes} of {number_of_classes}) [2nd Preference]'],
row[f'Days (Class {number_of_classes} of {number_of_classes}) [3rd Preference]'],
row[f'Days (Class {number_of_classes} of {number_of_classes}) [4th Preference]']
]
)

# for each class a teacher has, create a classroom object and add it to the list 'applicants'
for i in range(number_of_classes):
group_num += 1
class_num = i + 1 # class_num keeps track of which class out of the total being created
classroom = Classroom(
group_number = group_num,
teacher = teacher,
start_time = row[f'Start Time (Class {class_num} of {number_of_classes})'],
end_time = row[f'End Time (Class {class_num} of {number_of_classes})']
)
teacher.add_classrooms(classroom)

self.classrooms.append(classroom)

def process_volunteer_data(self, row: dict):
volunteer = Volunteer(
first = row[VOLUNTEER_COLUMNS.FIRST_NAME.value].strip().lower().capitalize(),
last = row[VOLUNTEER_COLUMNS.LAST_NAME.value].strip().lower().capitalize(),
phone = row[VOLUNTEER_COLUMNS.PHONE.value],
email = row[VOLUNTEER_COLUMNS.EMAIL.value].strip(),
leader_app = (lambda x: True if x == 'Yes' else False)(row[VOLUNTEER_COLUMNS.LEADER.value]),
schedule = Schedule(list(row.values())[15:55]),
board_member = (lambda current_board_member: True if current_board_member == 'Yes' else False)(row[VOLUNTEER_COLUMNS.BOARD.value])
)

self.volunteers.append(volunteer)

def process_partner_data(self, row: dict):
""" Process row data from csv/excel file into Partner objects."""

number_of_partners = int(row[PARTNER_COLUMNS.NUM_PARTNERS.value])
partner_emails = [row[PARTNER_COLUMNS.EMAIL.value].lower()]

# add all the partners' emails to the list 'partner_emails'
for i in range(1, number_of_partners):
partner_email = row[f'Group Member #{i + 1}'].lower()
partner_emails.append(partner_email)

# create a list of volunteers that are in the partner group
group = [volunteer for volunteer in self.volunteers if (volunteer.email in partner_emails)]

# Remove duplicate volunteers
for partner in group:
duplicates = [partner_group for partner_group in self.partners if partner in partner_group.members]
if len(duplicates) != 0 and len(group) > 1:
print(f'{partner.email} was in 2 groups. One deleted.')
self.partners.remove(duplicates[0])

if len(group) > 1:
self.partners.append(Partners(group))

if len(group) < number_of_partners:
self.partners_not_found.append(partner_emails)








18 changes: 11 additions & 7 deletions vsvs_scheduler/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os

from scheduler import Scheduler
from __init__ import ASSIGNMENTS_DIRECTORY, MIN_TEAM_SIZE


def main():
Expand All @@ -10,10 +11,12 @@ def main():
partner_errors = vsvs_scheduler.create_assignments()
print(partner_errors)

if not os.path.isdir("results/"):
os.mkdir("results")
if not os.path.isdir(ASSIGNMENTS_DIRECTORY):
os.mkdir(ASSIGNMENTS_DIRECTORY)

results_file_path = os.path.join(ASSIGNMENTS_DIRECTORY, 'assignments.csv')

with open('results/assignments.csv', 'a', newline='') as assignments_csv:
with open(results_file_path, 'w', newline='') as assignments_csv:
csv_writer = csv.writer(assignments_csv, delimiter=',')
csv_writer.writerow(
['Group Number', 'First Name', 'Last Name', 'Email', 'Phone Number', 'Team Leader', 'Board Member',
Expand All @@ -22,7 +25,7 @@ def main():

group_num = 1
for classroom in vsvs_scheduler.classrooms:
if len(classroom.volunteers) >= 3:
if len(classroom.volunteers) >= MIN_TEAM_SIZE:
for volunteer in classroom.volunteers:
csv_writer.writerow(
[
Expand All @@ -41,14 +44,15 @@ def main():
)
group_num += 1

with open('results/unassigned.csv', 'w', newline='') as unassigned_csv:
unassigned_file_path = os.path.join(ASSIGNMENTS_DIRECTORY, 'unassigned.csv')
with open(unassigned_file_path, 'w', newline='') as unassigned_csv:
csv_writer = csv.writer(unassigned_csv, delimiter=',')
csv_writer.writerow(
[ 'Group', 'First Name', 'Last Name', 'Email', 'Phone Number', 'Team Leader', 'Board Member', 'Teacher',
'Availability', 'Start', 'End', 'Day']
)
for classroom in vsvs_scheduler.classrooms:
if len(classroom.volunteers) < 3:
if len(classroom.volunteers) < MIN_TEAM_SIZE:
csv_writer.writerow(
[
'',
Expand All @@ -67,7 +71,7 @@ def main():
)
csv_writer.writerow(['']*6)

for volunteer in vsvs_scheduler.individuals:
for volunteer in vsvs_scheduler.volunteers:
if volunteer.group_number == -1:
csv_writer.writerow(
[
Expand Down
41 changes: 0 additions & 41 deletions vsvs_scheduler/partner_data_uploader.py

This file was deleted.

Loading

0 comments on commit c7534f5

Please sign in to comment.