def __init__(self, schedule): """ Initialize the ObjectInitializer class :param schedule: A Schedule object """ Grid.__init__(self) self.schedule = schedule self.schedule_dict = {} self.appts_dict = {} self.appts_to_assign = copy.deepcopy( [appt for appt in schedule.appts if len(appt.interpreter) == 0]) self.language_dict = collections.defaultdict(list) self.time_dict = collections.defaultdict(list) self.valid_choices = collections.defaultdict(list) self.patients = { appt.patient for appt in self.schedule.appts if len(appt.patient) > 0 } self.languages = set() self.jobs = {} self.schedule_paths = [] self.best_paths = {} self.default_appt = Appointment(0, "00:00", 0, Patient(0, "None", [], "None"), Point(0, 0), 0, "", "").copy() self.interpreters = [] self.init_all_objects()
def print_valid_choices(self, interpreters): """ Print the valid appointment choices for self.interpreters """ for interpreter in interpreters: print('\n' + str(interpreter)) try: int_appt = self.get_last_job(interpreter) int_location = int_appt.location print("Current: " + str(int_appt.start) + " - " + str(int_appt.finish) + ", at location " + str(int_appt.location.coords) + '\n') except: int_location = Point(0, 0) print( "Not currently assigned to an appointment." + '\n' ) for appt in self.valid_choices[interpreter]: print('\t' + str(appt.IDNum) + " " + str(appt.start) + " - " + str(appt.finish) + " " + str((appt.location.x, appt.location.y)) + " " + str(round( appt.location.distanceFrom(int_location))) )
def _reset_objects(self): """ Reinitialize class objects :return: None """ for appt in self.schedule.appts: appt.interpreter = "" for interpreter in self.interpreters: self.add_loc(interpreter, Point(0, 0)) self.init_job(interpreter, self.default_appt)
def test(self): # _reset_attributes self.reinitializer._reset_attributes() self.assertEqual(0, self.reinitializer.impact) self.assertEqual(0, self.reinitializer.schedule.impact) # _reset_data_structures self.reinitializer._reset_data_structures() self.assertEqual(self.reinitializer.time_dict, collections.defaultdict(list)) self.assertEqual(self.reinitializer.valid_choices, collections.defaultdict(list)) self.assertEqual(self.reinitializer.jobs, {}) self.assertEqual(self.reinitializer.locs, {}) self.assertEqual(self.reinitializer.schedule_dict, {}) self.assertEqual(self.reinitializer.schedule_paths, []) self.assertEqual(self.reinitializer.best_paths, {}) # _reset_objects self.reinitializer._reset_objects() all_empty_str = all( [True for x in self.reinitializer.interpreters if x == ""]) all_in_locs = all([ True for x in self.reinitializer.interpreters if x in self.reinitializer.locs.keys() ]) all_in_jobs = all([ True for x in self.reinitializer.interpreters if x in self.reinitializer.jobs.keys() ]) all_locs_zeroed = all([ True for (x, y) in self.reinitializer.locs.items() if y == Point(0, 0) ]) all_jobs_default = all([ True for (x, y) in self.reinitializer.jobs.items() if y == self.reinitializer.default_appt ]) self.assertTrue(all_empty_str) self.assertTrue(all_in_locs) self.assertTrue(all_in_jobs) self.assertTrue(all_locs_zeroed) self.assertTrue(all_jobs_default)
def test(self): person = self.person point = self.point # add_loc method self.class_instance.add_loc(person, point) self.assertTrue(self.class_instance.locs[person], point) self.assertIsInstance(self.class_instance.locs[person], Point) # get_loc method self.assertTrue(self.class_instance.get_loc(person), point) self.assertIsInstance(self.class_instance.get_loc(person), Point) # move_to method new_point = Point(0, 0) self.class_instance.move_to(person, new_point) self.assertIsInstance(self.class_instance.locs[person], Point) self.assertEquals(self.class_instance.get_loc(person).x, 0) self.assertEquals(self.class_instance.get_loc(person).y, 0)
def populate_objects(self): """ Processes self.schedule and builds objects used by other methods :return: None """ # Reset interpreters to initial positions for interpreter in self.schedule.interpreters: if interpreter not in self.interpreters: self.interpreters.append(interpreter) for interpreter in self.interpreters: self.add_loc(interpreter, Point(0, 0)) self.init_job(interpreter, self.default_appt) for patient in self.patients: for language in patient.languages: self.languages.add(language) self.default_appt.patient.languages = self.languages # Associate appts by idnum for optimization functions/objects for appt in self.schedule.appts: self.appts_dict[appt.idnum] = appt
def setUp(self): self.obj = Point(10, 0)
def test(self): # attributes test_point = (10, 0) self.assertEqual(10, self.obj.x) self.assertEqual(0, self.obj.y) self.assertEqual(test_point, self.obj.coordinates) # move method point2 = Point(10, 10) point2 = point2.move(1, 1) self.assertEqual(Point(11, 11), point2) # move_to method point3 = Point(1, 1) point3 = point3.move_to(point2.x, point2.y) self.assertEqual(point2, point3) # distance_from method point1 = Point(1, 1) point2 = Point(4, 5) dist = point1.distance_from(point2) self.assertEqual(5, dist) # str method string_repr = str(point2) self.assertEqual(str((4, 5)), string_repr)
def setUp(self): self.point = Point(1, 1) self.person = Person("Doe, Jane", ["English"], "Female") self.class_instance = Grid()
def test(self): # It's important to be precise with object references from here on test_appt1 = self.schedule.appts[0] test_appt2 = self.schedule.appts[1] test_appt3 = self.schedule.appts[2] test_interpreter1 = test_appt1.interpreter test_interpreter2 = test_appt2.interpreter test_interpreter3 = test_appt3.interpreter # Note: wrappers are tested when we test the methods they call default_appt = self.object_initializer.default_appt # init_job # Passing an Interpreter object to the jobs dict should return a list # of Appointment objects self.object_initializer.jobs.pop(test_interpreter1) self.object_initializer.init_job(test_interpreter1, test_appt1) self.assertEqual(self.object_initializer.jobs[test_interpreter1], [test_appt1]) self.object_initializer.jobs[test_interpreter1] = [default_appt] # populate_objects interpreter_lst = [test_interpreter1, test_interpreter2, test_interpreter3] languages = {"French", "Spanish"} self.assertEqual(self.object_initializer.interpreters, interpreter_lst) self.assertEqual(self.object_initializer.languages, languages) # test add locs for each point in each appointment # Passing Interpreter objects to the locs dict should return a Point test_point = self.object_initializer.locs[test_interpreter1] locx, locy = test_point.coordinates self.assertEqual(self.object_initializer.locs[test_interpreter1], Point(locx, locy)) test_point = self.object_initializer.locs[test_interpreter2] locx, locy = test_point.coordinates self.assertEqual(self.object_initializer.locs[test_interpreter2], Point(locx, locy)) test_point = self.object_initializer.locs[test_interpreter3] locx, locy = test_point.coordinates self.assertEqual(self.object_initializer.locs[test_interpreter3], Point(locx, locy)) # test add jobs for each interpreters on each appointment # Passing Interpreter objects to the jobs dict should return a list of # Appointment objects self.assertEqual(self.object_initializer.jobs[test_interpreter1], [default_appt]) self.assertEqual(self.object_initializer.jobs[test_interpreter2], [default_appt]) self.assertEqual(self.object_initializer.jobs[test_interpreter3], [default_appt]) # gen_schedule_dict # Note that we are passing an integer dict key not a list index self.assertEqual(self.object_initializer.appts_dict[1], test_appt1) self.assertEqual(self.object_initializer.appts_dict[2], test_appt2) self.assertEqual(self.object_initializer.appts_dict[3], test_appt3) # gen_language_dict test_dict = {'French': [test_appt3], 'Spanish': [test_appt1, test_appt2]} self.assertEqual(test_dict, self.object_initializer.language_dict)
def test(self): self.cls.appts_to_assign = list(self.schedule.appts) appts = self.cls.appts_to_assign interpreters = self.cls.interpreters # get_job_with_id # Returns an appointment from self.appt_dict at index id # Due to overlap with the id builtin, the variable name is job_id # # This test indexes a pre-made test object and assert # equality to the index as the first test condition # If this test fails then the test object has been changed self.assertEqual(appts[0].idnum, 1) self.assertEqual(self.cls.get_job_with_id(1), appts[0]) # get_jobs_with_ids # Calls get_job_with_id on each id and returns a list of appts found # Asserting object equality by index ensures no mutation interference ids1 = [1, 2, 3] ids2 = [1, 2, 7] ids3 = [5, 6, 7] self.assertEqual(appts[0].idnum, ids1[0]) self.assertEqual(appts[1].idnum, ids1[1]) self.assertEqual(appts[2].idnum, ids1[2]) test_list = self.cls.get_jobs_with_ids(ids1) self.assertEqual([appts[0], appts[1], appts[2]], test_list) self.assertEqual(appts[0].idnum, ids1[0]) self.assertEqual(appts[1].idnum, ids1[1]) test_list = self.cls.get_jobs_with_ids(ids2) self.assertEqual([appts[0], appts[1]], test_list) test_list = self.cls.get_jobs_with_ids(ids3) self.assertEqual([], test_list) # get_last_job job = self.cls.get_last_job(interpreters[0]) self.assertEqual(job, appts[0]) job = self.cls.get_last_job(interpreters[1]) self.assertEqual(job, appts[1]) job = self.cls.get_last_job(interpreters[2]) self.assertEqual(job, appts[2]) # get_jobs jobs = self.cls.get_jobs(interpreters[0]) self.assertEqual(jobs, [self.cls.default_appt, appts[0]]) jobs = self.cls.get_jobs(interpreters[1]) self.assertEqual(jobs, [self.cls.default_appt, appts[1]]) jobs = self.cls.get_jobs(interpreters[2]) self.assertEqual(jobs, [self.cls.default_appt, appts[2]]) # add_job self.cls.add_job(interpreters[2], appt4) jobs = self.cls.get_jobs(interpreters[2]) self.assertEqual(jobs, [self.cls.default_appt, appts[2], appt4]) # Is compatible tests if the appt start is >= the interpreter's start # It also tests if appt finish is <= interpreter's end time # to avoid scheduling interpreters at invalid times # This test has appt start at 8:45 and appt finish at 10:05 # This test has interp start at 8:30 and finish at 12:30 # 8:30 <= 8:45, and 12:30 >= 10:05, so this test should PASS is_compatible = self.cls.is_appt_in_shift(self.interpreter, self.appt) self.assertTrue(is_compatible) # This test has appt start at 12:45 and appt finish at 13:25 # This test has interp start at 8:30 and finish at 12:30 # 8:30 <= 12:45, but 12:30 < 13:25, so this test should FAIL is_compatible = self.cls.is_appt_in_shift(self.interpreter, self.appt2) self.assertFalse(is_compatible) # calc_impact self.assertEqual(self.cls.calc_impact(), 150) # can_assign self.assertTrue( self.cls.can_assign(interpreters[0], appts[0], appts[1])) # can_insert_job self.cls.appts_to_assign.append(appt6) self.assertTrue(self.cls.can_insert_job(interpreters[0], appt6)) self.cls.assign(interpreters[0], appt6) self.assertFalse(self.cls.can_insert_job(interpreters[0], appt7)) # assign # save existing assigned staff before reset interpreter = self.cls.interpreters[0] # reset objects self.cls.reset() # assign the interpreter to the appointment self.cls.assign(interpreter, appts[0]) self.assertEqual(appts[0].interpreter, interpreter) self.assertIn(appts[0], self.cls.jobs[interpreter]) self.assertEqual(Point(*appts[0].location.coordinates), self.cls.locs[interpreter]) # group_assign # reset appts[0] self.cls.reset() self.cls.appts_to_assign.append(appt6) # create jobs and call the method under test jobs = [appts[0], appt6] self.cls.group_assign(interpreter, jobs) # check for appts[0] assignment self.assertEqual(appts[0].interpreter, interpreter) self.assertIn(appts[0], self.cls.jobs[interpreter]) # check for appt6 assignment self.assertEqual(appt6.interpreter, interpreter) self.assertIn(appt6, self.cls.jobs[interpreter])