def text_file_chores_validation(text_file_line, all_households, participants_each_household, chores_each_household, i, household_names): number_chores_and_frequencies = int(int((text_file_line[i])) * 2) chores_list = set() chores_each_household[text_file_line[0]] = [] # j is another counter variable which, in the below for loop, will isolate the chore frequencies will i isolates the corresponding # chore j = i + 2 for household_chore in range(i + 1, number_chores_and_frequencies + i + 1, 2): chores_each_household[text_file_line[0]].append( text_file_line[household_chore]) chore_frequency = text_file_line[j] try: Chore.is_valid_frequency(chore_frequency) ChoresList.is_unique(text_file_line[household_chore], chores_list) chores_object = Chore(text_file_line[household_chore], chore_frequency) chores_list.add(chores_object) chores_each_household[text_file_line[0]].append(text_file_line[j]) valid = True except (TypeError, ValueError) as err: print(err) print(( "\n\tPlease correct the '{}' on the '{}' Household on the text file, first Quit the application" ).format(chore_frequency, text_file_line[0])) j += 2 try: ChoresList.is_valid_length(chores_list) except ValueError as err: print(err) household_object = Household(text_file_line[0], household_names, chores_list)
def get_chore_frequency(): valid = False chore_frequency = 0 while not valid: chore_frequency = input("\n\t\tTimes per week: ") try: Chore.is_valid_frequency(chore_frequency) valid = True except (TypeError, ValueError) as err: print(err) return int(chore_frequency)
def get_chores(): chores_list = set() new_chore = "AAA" # dummy value so that we can start the while loop number_of_chores = 0 while new_chore != "": # Get names of chore new_chore = get_chore(number_of_chores + 1) if new_chore == "": try: # Check if the number of chores is valid ChoresList.is_valid_length(chores_list) except ValueError as err: print(err) new_chore = "AAA" else: try: # Check if new chore has already be set ChoresList.is_unique(new_chore, chores_list) # Get the frequency the chore needs to be completed chore_frequency = get_chore_frequency() # Create a chore object using the name a frequency chore_obj = Chore(new_chore, chore_frequency) # Add to list of chores chores_list.add(chore_obj) number_of_chores = number_of_chores + 1 except ValueError as err: print(err) return chores_list
def get_chores(new_household_name, chores_each_household): chores_list = set() chores_each_household[new_household_name] = [] new_chore = "AAA" # dummy value so that we can start the while loop number_of_chores = 0 while new_chore != "": new_chore = get_chore(number_of_chores + 1) if new_chore == "": try: ChoresList.is_valid_length(chores_list) except ValueError as err: print(err) new_chore = "AAA" else: try: ChoresList.is_unique(new_chore, chores_list) chore_frequency = get_chore_frequency() chore_obj = Chore(new_chore, chore_frequency) chores_list.add(chore_obj) chores_each_household[new_household_name].append(new_chore) chores_each_household[new_household_name].append( chore_frequency) number_of_chores = number_of_chores + 1 except ValueError as err: print(err) return chores_list
def get_chore(chore_number): # A valid answer is either a blank or a valid name valid_answer = False while not valid_answer: chore_name = input("\n\tEnter" " the name of chore {}: ".format(chore_number)) if chore_name == "": valid_answer = True else: try: Chore.is_valid_chore_name(chore_name) valid_answer = True except ValueError as err: print(err) return chore_name
def add_households(all_households, household_names, participants, chores): # For each household from the file, create the relevant objects for the household for household in range(len(household_names)): new_household_name = household_names[household] members_set = set(participants[household]) chores_set = set() for i in range(len(chores[household])): if i % 2 == 0: chore_obj = Chore(chores[household][i], chores[household][i + 1]) chores_set.add(chore_obj) household_obj = Household(new_household_name, members_set, chores_set) all_households.append(household_obj) return
def main(): print("\nTest 1: Create a valid household") try: h = Household("House1", {"personA", "personB", "personC"}, {Chore("wash up", 4), Chore("vacuum stairs", 2), Chore("dusting", 1), Chore("empty bin", 2)}) print("\n\tVALID: ", h) except Exception as err: print("\tERROR: ", err) print("\nTest 2: Create a household with an invalid name 'a'") try: h = Household("a", {"personA", "personB", "personC"}, {Chore("wash up", 4), Chore("vacuum stairs", 2), Chore("dusting", 1), Chore("empty bin", 2)}) print("\n\tVALID: ", h) except Exception as err: print("\tERROR: ", err) print("\nTest 3: Create a household with an invalid name '*'") try: h = Household("*", {"personA", "personB", "personC"}, {Chore("wash up", 4), Chore("vacuum stairs", 2), Chore("dusting", 1), Chore("empty bin", 2)}) print("\n\tVALID: ", h) except Exception as err: print("\tERROR: ", err) print("\nTest 4: Create a household with an invalid name 12*'a'") try: h = Household(12 * "a", {"personA", "personB", "personC"}, {Chore("wash up", 4), Chore("vacuum stairs", 2), Chore("dusting", 1), Chore("empty bin", 2)}) print("\n\tVALID: ", h) except Exception as err: print("\tERROR: ", err) print("\nTest 5: Create a household with an invalid name ' '") try: h = Household(" ", {"personA", "personB", "personC"}, {Chore("wash up", 4), Chore("vacuum stairs", 2), Chore("dusting", 1), Chore("empty bin", 2)}) print("\n\tVALID: ", h) except Exception as err: print("\tERROR: ", err) print("\nTest 6: Update the log for a participant (valid)") try: h = Household("House1", {"personA", "personB", "personC"}, {Chore("wash up", 4), Chore("vacuum stairs", 2), Chore("dusting", 1), Chore("empty bin", 2)}) h.update_log("personA", "wash up", 49) print("\n\tVALID: ", h) except Exception as err: print("\tERROR: ", err)
def read_households(all_households): # Gets the data from the households file lines = read_file("households.txt") # Creates empty lists of the households' properties all_household_names = [] all_participants = [] all_chores = [] # For each line of the file data, it validates the elements based on the given format for line in lines: # Creates an error message referencing the line to be displayed when there is an error error_msg = "\nError on line {} of the 'households' file (This line will be omitted):\n\t".format( lines.index(line) + 1) # Tries to get the household name and if it can not be found, gives an error and skips the whole line try: household_name = line[0] except (IndexError, ValueError): print(error_msg) print("\tThe first element should be the households name.".format( lines.index(line) + 1)) continue # Tries to get the participants and if it can not be found, gives an error and skips the whole line try: number_of_participants = int(line[1]) participants_offset = number_of_participants + 2 participants = line[2:participants_offset] except (IndexError, ValueError): print(error_msg) print( "\tThe second element should be the number of participants followed by that number of participants." ) continue # Tries to get the chores and if it can not be found, gives an error and skips the whole line try: number_of_chores = int(line[participants_offset]) chores_offest = participants_offset + 1 chores = line[chores_offest:chores_offest + (2 * number_of_chores)] except (IndexError, ValueError): print(error_msg) print( "\tThe element after the defined number of participants should be the number of chores followed by that number of chores and frequencies." ) continue # Checks if the households’ name is valid and adds them to the respective list # If not valid, gives an error and skips the whole line try: Household.is_valid_name(household_name) if household_name in all_household_names: raise ValueError( "The household name '{}' had already been used and can not be used again." .format(household_name)) except (TypeError, ValueError) as err: print(error_msg, err) continue # Checks if the participants names' and number of participants are valid and adds them to the respective list # If not valid, gives an error and skips the whole line try: Participants.is_valid_length(participants) for name in participants: Participants.is_valid_name(name) particpants_set = set(participants) if len(participants) != len(particpants_set): raise ValueError( "There are multiple participants with the same.") except (TypeError, ValueError) as err: print(error_msg, err) continue # Checks if the number of chores, chore name, and chore frequency are is valid and adds them to the respective list # If not valid, gives an error and skips the whole line try: chore_names = [] for chore in chores: if chore.isdigit(): Chore.is_valid_frequency(chore) else: Chore.is_valid_chore_name(chore) chore_names.append(chore) ChoresList.is_valid_length(chore_names) chore_names_set = set(chore_names) if len(chore_names) != len(chore_names_set): raise ValueError("There are multiple chores with the same.") except (TypeError, ValueError) as err: print(error_msg, err) continue all_household_names.append(household_name) all_participants.append(participants) all_chores.append(chores) # Creates objects for all the valid households add_households(all_households, all_household_names, all_participants, all_chores) return