def assign_to_room(person, room=None): """Assign a person to a room. <room_name> is optional. """ # find a person by name if type(person) is str: person = Amity.find_person(person) # find a room by name if specified if type(room) is str: room = Amity.find_room(room) # is the room an office? if isinstance(room, Office): if not person.has_office(): Manager.assign_to_office(person, room) # is the room a living space? if isinstance(room, LivingSpace): if person.can_have_living_space() and \ not person.has_living_space(): Manager.assign_to_living_space(person, room) elif room is None: if not person.has_office(): Manager.assign_to_office(person) if isinstance(person, Fellow) and person.can_have_living_space(): if not person.has_living_space(): Manager.assign_to_living_space(person)
def get_members_in_room(room_name, print_stdio=False): """Given a room, this prints all members in it. """ room = Amity.find_room(room_name) occupants = ', '.join(occupant.name for occupant in room.occupants) if print_stdio is True: print occupants return occupants
def assign_to_office(person, room=None): """Assign person to an office. """ if room is None: room = Amity.get_random_office() if not room.filled(): room.add_occupant(person) else: raise OutOfOfficeException
def assign_to_living_space(person, room=None): """Assign a person to a living space. """ if room is None: room = Amity.get_random_living_space() else: if room.filled(): raise OutOfLivingSpaceException if person.is_female(): room = Amity.find_living_space_with_female_occupant()\ or Amity.get_random_living_space() if room.has_no_occupant()\ or room.has_female_occupant(): room.add_occupant(person) else: if not room.has_female_occupant() \ and not person.has_living_space(): room.add_occupant(person)
def test_can_add_room_to_amity(self): Amity.reset_room_count() Amity.add_room(self.r) Amity.add_room(self.l) self.assertEquals(Amity.room_count, 2) self.assertEquals(Amity.room_collection[1].get_capacity(), 4) self.assertEquals(Amity.room_collection[1].has_no_occupant(), True)
def test_can_generate_and_assign_to_rooms(self): rooms = alloc.generate_rooms('office') self.assertEquals(len(rooms), 10) Amity.add_rooms(rooms) self.assertIsInstance(rooms[randint(0, 9)], Office) rooms = alloc.generate_rooms('living space') self.assertEquals(len(rooms), 10) Amity.add_rooms(rooms) self.assertIsInstance(rooms[randint(0, 9)], LivingSpace) self.assertEqual(Amity.room_count, 20) # test that file can be parsed PeopleFileParser.line_to_person(file_path) person_name = 'ANDREW PHILLIPS' Manager.assign_to_room(person_name, 'Room 1') Manager.assign_to_room(person_name, 'Room 10') person = Amity.find_person(person_name) self.assertEquals(person.office.name, 'Room 1') self.assertIn(person, Amity.find_room('Room 1').occupants) # test that manager can make allocations Manager.allocate() self.assertEquals([], Manager.get_list_of_unallocated_people())
or room.has_female_occupant(): room.add_occupant(person) else: if not room.has_female_occupant() \ and not person.has_living_space(): room.add_occupant(person) @staticmethod def allocate(): for person in Amity.people_collection: Manager.assign_to_room(person) Manager.assign_to_room = assign_to_room Manager.assign_to_office = assign_to_office Manager.assign_to_living_space = assign_to_living_space Manager.allocate = allocate Manager.get_list_of_unallocated_people = get_list_of_unallocated_people Manager.get_list_of_allocations = get_list_of_allocations Manager.print_list_of_allocations = print_list_of_allocations Manager.get_members_in_room = get_members_in_room if __name__ == '__main__': offices = generate_rooms('office') Amity.add_rooms(offices) living_spaces = generate_rooms('living space') Amity.add_rooms(living_spaces) print "You have {0} rooms now in Amity".format(Amity.room_count)
def test_can_remove_room(self): Amity.remove_room(self.r.name) self.assertEquals(Amity.room_count, 1)