def load_people(self, args): """Creates people from a specified file and allocates each person based on person type""" path = args['<file_location>'] if not Util.is_file(path): Util.print_line('File location is invalid') return False with open(path, 'r') as file: for line in file: records = line.split() living_space = False firstname = records[0] lastname = records[1] person_type = records[2] if person_type.upper() == 'FELLOW': if (records[3].upper() == 'Y'): living_space = True person = Fellow(firstname, lastname, living_space) else: person = Staff(firstname, lastname) self.people.append(person) Util.print_line(person.name() + ' succesfully created') self.allocate(person, None, 'OFFICE') if person.is_fellow(): self.allocate(person) self.save_state_to_pickle()
def test_update_living_space(self): fellow = Fellow('Dhani', 'AF26121') self.assertRaises(TypeError, fellow.update_living_space, self.office) fellow = Fellow('Dhani', 'AF26121', have_living_space=False) self.assertRaises(LivingSpaceRejectedException, fellow.update_living_space, self.living_space)
class Amity(object): def __init__(self): print("Welcome to Amity\n") self.office = Office() self.livingspace = LivingSpace() self.staff = Staff() self.fellow = Fellow() def ready(self): command = None while command != "exit": command = self.get_input() if command.lower() == "exit": command = "exit" else: method_params = map(str.lower, command.split()) getattr(self, method_params[0])(method_params[1::]) return def get_input(self): return raw_input(">> ").strip() """ create_room OFFICE carat vvida minion create_room LIVINGSPACE carat vvida minion """ def create_room(self, rooms): if rooms[0] == "office": self.office.create(rooms) else: self.livingspace.create(rooms) """ add_person bukky FELLOW Y add_person tosin STAFF """ def add_person(self, person_details): if len(person_details) == 2: person_details.append("N") if person_details[1] == "fellow": self.fellow.add(person_details) self.fellow.allocate_room(self.office, self.livingspace) else: self.staff.add(person_details) self.staff.allocate_room(self.office)
def handle_collisions(self, coll_objects): for object in coll_objects: if isinstance(object, ForceField): self.is_alive = False fellow = Fellow(self.x, self.y, self.game) self.game.objects.append(fellow) self.game.fellows.append(fellow)
def add_person(self, person_name, person_type, wants_accomodation='N'): random_office = None random_livingspace = None if self.get_available_rooms("office"): random_office = random.choice(self.get_available_rooms("office")) if self.get_available_rooms("livingspace"): random_livingspace = random.choice( self.get_available_rooms("livingspace")) if person_type == 'fellow': if wants_accomodation and wants_accomodation == 'Y': new_fellow = Fellow(person_name, "fellow", random_office, random_livingspace) self.fellows_added.append(new_fellow) if random_office: new_fellow.office.number_of_occupants += 1 print((new_fellow.office.name, str(new_fellow.office.number_of_occupants))) if random_livingspace: new_fellow.livingspace.number_of_occupants += 1 return 'Fellow ' + person_name + ' has successfully been added.' else: new_fellow = Fellow(person_name, "fellow", random_office, None) self.fellows_added.append(new_fellow) if random_office: new_fellow.office.number_of_occupants += 1 print((new_fellow.office.name, str(new_fellow.office.number_of_occupants))) return 'Fellow ' + person_name + ' has successfully been added.' if person_type == 'staff': if wants_accomodation and wants_accomodation == 'Y': return 'Staff cannot be allocated livingspace' else: new_staff = Staff(person_name, "staff", random_office) self.staff_added.append(new_staff) if random_office: new_staff.office.number_of_occupants += 1 print((new_staff.office.name, str(new_staff.office.number_of_occupants))) return 'Staff ' + person_name + ' has successfully been added.'
class Room: s = "staff" f = "fellow" input = raw_input( "Hi, please identify yourself by either: staff or fellow ::") if input == s: Staff.member() #runs the member method in a static manner. elif input == f: Fellow.fellower() #runs the fellower method in a static manner else: print "Invalid option"
def get_person(self, row): """Creates person instance based on person type""" living_space = False if row['living_space'] == 1: living_space = True if row['person_type'] == 'FELLOW': person = Fellow(str(row['firstname']), str(row['lastname']), living_space) else: person = Staff(str(row['firstname']), str(row['lastname']), living_space) if row['assigned_room']: key_value_pairs = row['assigned_room'].split('=') for i in xrange(len(key_value_pairs) - 1): keys = key_value_pairs[i].split(',') values = key_value_pairs[i + 1].split(',') for index in xrange(len(keys)): person.assigned_room[str(keys[index])] = str(values[index]) person.date_time = row['date_time'] return person
def add_person(self, firstname, lastname, role, wants_accomodation="N"): try: fellow = {} staff = {} roles = ["fellow", "staff", "F", "S"] wants_accomodation_options = ["Yes", "N", "Y" "No", None] if type(firstname) != str or type(lastname) != str: print("Please enter valid names.") else: if role not in roles: return "Please enter valid role." else: if role == "fellow" or role == "F": fellow_name = firstname + " " + lastname self.fellow_counter = self.fellow_counter+1 fellow_id = "F"+str(self.fellow_counter) fellow["id"] = fellow_id fellow["name"] = fellow_name fellow["office"] = self.allocate_office(fellow_id) if wants_accomodation == "Yes" or wants_accomodation == "Y": fellow["living_space"] = self.allocate_living_space( fellow_id, "topaz") elif wants_accomodation == "no" or wants_accomodation == "N": pass else: print( "invalid accomodation option, fellow created but not assigned living space.") self.fellows.append(fellow) fellow = Fellow() else: self.staff_counter = self.staff_counter + 1 staff_id = "S" + str(self.staff_counter) staff["id"] = staff_id staff["name"] = firstname + " " + lastname staff["office"] = self.allocate_office(staff_id) self.staff.append(staff) staff = Staff() if wants_accomodation == "yes" or wants_accomodation == "Y": print( "Staff does not get living space. staff created with no living space.") elif wants_accomodation == "no" or wants_accomodation == "N": pass else: print("invalid accomodation") except(): return "Please try again"
def add_person(self, args): """ Resolves the argument and creates the right person type, adds the person to people list and allocates the person to a room. This also saves the current state to pickle file """ firstname = args['<firstname>'].upper() lastname = args['<lastname>'].upper() if args['<person_type>'].upper() == 'FELLOW': person = Fellow(firstname, lastname, args['-w']) else: person = Staff(firstname, lastname, args['-w']) if self.person_exists(person): Util.print_line(person.name() + ' already exists') else: self.people.append(person) print person.name() + ' successfully created' self.allocate(person, None, 'OFFICE') if person.is_fellow(): self.allocate(person) self.save_state_to_pickle()
def __init__(self): print("Welcome to Amity\n") self.office = Office() self.livingspace = LivingSpace() self.staff = Staff() self.fellow = Fellow()
def setUp(self): self.amity = Amity() self.fellow = Fellow() self.Staff = Staff() self.office = Office() self.livingspace = LivingSpace()
def add_person(self, person_type, first_name, surname, wants_accommodation="n"): """ Method adds a new person and assigns them an office, and an optional living space """ person_type = person_type.lower() wants_accommodation = wants_accommodation.lower() if person_type == "fellow" or person_type == "staff": if person_type == "fellow": new_person = Fellow(person_type, first_name, surname, wants_accommodation) self.all_persons.append(new_person) self.fellows.append(new_person) print("Fellow {0:s} {1:s} has been successfully added".format( first_name, surname)) """The following section assigns a random office and an optional random living space to the new fellow added above""" if len(self.all_offices) == 0: print( "Office not assigned, because no offices are available at this time.") else: random_office = random.choice(self.all_offices) if len(random_office.occupants) == 6: random_office = random.choice(self.all_offices) else: random_office.occupants.append(new_person) self.allocated_an_office.append(new_person) print("{0:s} {1:s} has been allocated the office {2:s}".format( new_person.first_name, new_person.surname, random_office.room_name)) if new_person.wants_accommodation == "yes" or new_person.wants_accommodation == "y": if len(self.all_living_spaces) == 0: print( "Living space not assigned, because no living spaces are available at this time.") else: random_living_space = random.choice( self.all_living_spaces) if len(random_living_space.occupants) == 4: random_living_space = random.choice(self.all_living_spaces) else: random_living_space.occupants.append(new_person) self.allocated_a_living_space.append(new_person) print("{0:s} {1:s} has been allocated the living space {2:s}".format( new_person.first_name, new_person.surname, random_living_space.room_name)) elif person_type == "staff": new_person = Staff(person_type, first_name, surname, wants_accommodation) self.all_persons.append(new_person) self.staff.append(new_person) print("Staff {0:s} {1:s} has been successfully added".format( first_name, surname)) if len(self.all_offices) == 0: print( "Office not assigned, because no offices are available at this time.") if new_person.wants_accommodation == "yes" or new_person.wants_accommodation == "y": print("Staff cannot be assigned a living space.") else: random_office = random.choice(self.all_offices) if len(random_office.occupants) >= 6: random_office = random.choice(self.all_offices) if new_person.wants_accommodation == "yes" or new_person.wants_accommodation == "y": print("Staff cannot be assigned a living space.") else: random_office.occupants.append(new_person) self.allocated_an_office.append(new_person) print("{0:s} {1:s} has been allocated the office {2:s}".format(new_person.first_name, new_person.surname, random_office.room_name)) if new_person.wants_accommodation == "yes" or new_person.wants_accommodation == "y": print("Staff cannot be assigned a living space.") else: print("Person can only either be Fellow or Staff.")
def add_fellow(self): fellow_ = Fellow() self.fellows.append(fellow_)
def add_person(self, first_name, last_name, person_type, wants_accommodation): """Function to add a person to the system, allocate them to rooms and/or living spaces and add them to the waiting list if full rooms""" people = "" if person_type.lower() == "staff": staff = Staff(first_name, last_name) # add staff to people self.people.append(staff) self.staff.append(staff) print("{} {} of id {} has been added to the system" .format(staff.first_name, staff.last_name, staff.person_id)) people += "{} {} of id {} has been added to the system"\ .format(staff.first_name, staff.last_name, staff.person_id) # Allocate staff to a random empty office if len([room_object for room_object in self.offices if len(room_object.room_members) < room_object.room_capacity]): office_allocated = random.choice([room_object for room_object in self.offices if len(room_object.room_members) < room_object.room_capacity]) office_allocated.room_members.append(staff) print("{} {} has been allocated {}" .format(staff.first_name, staff.last_name, office_allocated)) people += "{} {} has been allocated {}"\ .format(staff.first_name, staff.last_name, office_allocated) else: self.waiting_list.append(staff) self.office_waiting_list.append(staff) print("{} {} has been added to the office waiting list" .format(staff.first_name, staff.last_name)) people += "{} {} has been added to the office waiting list"\ .format(staff.first_name, staff.last_name) if wants_accommodation == "Y": print("Staff cannot be allocated a living space!") people += "Staff cannot be allocated a living space!" elif person_type.lower() == "fellow": fellow = Fellow(first_name, last_name) # add fellow to people self.people.append(fellow) self.fellows.append(fellow) people += "{} {} of id {} has been added to the system"\ .format(first_name, last_name, fellow.person_id) print("{} {} of id {} has been added to the system" .format(first_name, last_name, fellow.person_id)) # Give them an office first if len([room_object for room_object in self.offices if len(room_object.room_members) < room_object.room_capacity]): office_allocated = random.choice([room_object for room_object in self.offices if len(room_object.room_members) < room_object.room_capacity]) office_allocated.room_members.append(fellow) print("{} {} has been allocated {}" .format(fellow.first_name, fellow.last_name, office_allocated)) people += "{} {} has been allocated {}"\ .format(fellow.first_name, fellow.last_name, office_allocated) else: self.waiting_list.append(fellow) self.office_waiting_list.append(fellow) print("{} {} has been added to the office waiting list" .format(fellow.first_name, fellow.last_name)) people += "{} {} has been added to the office waiting list"\ .format(fellow.first_name, fellow.last_name) if wants_accommodation == "Y": # Get the list of available living spaces if len([room_object for room_object in self.living_spaces if len(room_object.room_members) < room_object.room_capacity]): living_space_allocated = \ random.choice([room_object for room_object in self.living_spaces if len(room_object.room_members) < room_object.room_capacity]) living_space_allocated.room_members.append(fellow) print("{} {} has been allocated {}" .format(fellow.first_name, fellow.last_name, living_space_allocated)) people += "{} {} has been allocated {}"\ .format(fellow.first_name, fellow.last_name, living_space_allocated) else: self.waiting_list.append(fellow) self.living_space_waiting_list.append(fellow) print("{} {} has been added to the living space " "waiting list".format(fellow.first_name, fellow.last_name)) people += "{} {} has been added to the living space "\ "waiting list".format(fellow.first_name, fellow.last_name) else: print("Person type can only be fellow or staff") people += "Person type can only be fellow or staff" return people
def load_state(self, database=None): database = './files/' + database """Function to load people from a database""" try: db = database if database else "amity.db" connection = sqlite3.connect(db) cc = connection.cursor() cc.execute("SELECT * FROM person") people = cc.fetchall() for person in people: if person[2] == 'staff': staff = Staff(person[1], person[2], person[3]) staff.person_id = person[0] self.people.append(staff) self.staff.append(staff) else: fellow = Fellow(person[1], person[2], person[3]) fellow.person_id = person[0] self.people.append(fellow) self.fellows.append(fellow) cc.execute("SELECT * FROM room") rooms = cc.fetchall() for room in rooms: if room[2] == 6: office = Office(room[1]) office.room_id = room[0] office.room_members = [person for person in self.people if person.person_id in [int(str(person_id)) for person_id in room[4].split(',')] ] self.rooms.append(office) self.offices.append(office) else: living_space = LivingSpace(room[1]) living_space.room_id = room[0] living_space.room_members = [person for person in self.people if person.person_id in [int(str(person_id)) for person_id in room[4].split(',')] ] self.rooms.append(living_space) self.living_spaces.append(living_space) cc.execute("SELECT * FROM office_waiting_list") office_waiting_list = cc.fetchall() for person_object in office_waiting_list: for individual in self.people: if individual.id == person_object[3]: self.waiting_list.append(individual) self.office_waiting_list.append(individual) cc.execute("SELECT * FROM living_space_waiting_list") living_space_waiting_list = cc.fetchall() for person_object in living_space_waiting_list: for individual in self.people: if individual.id == person_object[3]: self.waiting_list.append(individual) self.living_space_waiting_list.append(individual) connection.commit() connection.close() print("Data successfully loaded to amity!") except NoDataFound: print("Please add data to the database!")