def get_household_name(): household_name = "" valid = False while not valid: household_name = input("\n\tEnter household name: ") try: Household.is_valid_name(household_name) valid = True except ValueError as err: print(err) return household_name
def text_file_household_name_validation(text_file_line, all_households, participants_each_household, chores_each_household): try: Household.is_valid_name(text_file_line[0]) valid = True all_households.append(text_file_line[0]) text_file_participants_validation(text_file_line, all_households, participants_each_household, chores_each_household) except ValueError as err: print(err) print(( "\n\nPlease correct the '{}' Household name the text file, first Quit the application" ).format(text_file_line[0]))
def create_household(all_households): # Get new household name new_household_name = get_household_name() # Check if the household already exists household_obj = household_exists(new_household_name, all_households) # Returns None if does not already exist if household_obj is None: # Get participants names members_set = get_participants_names() # Get chores chores_set = get_chores() # Create new household using participants and chores household_obj = Household(new_household_name, members_set, chores_set) # Add created household to list of current households all_households.append(household_obj) # Print out all entered information print("\n\tHousehold entered:\n") print("\t\tHousehold Name: ", getattr(household_obj, "household_name")) print("\t\tParticipants: ", getattr(household_obj, "participants")) print("\t\tChores (Frequency): ", getattr(household_obj, "chores")) else: # If household already exists, alert user print("Household {} already exists, returning to the menu.".format( new_household_name)) return
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 create_household(all_households, participants_each_household, chores_each_household, household_text_file): new_household_name = get_household_name() found = False household_obj = household_exists(new_household_name, all_households) if household_obj == None: members_set = get_participants_names(new_household_name, participants_each_household) chores_set = get_chores(new_household_name, chores_each_household) household_obj = Household(new_household_name, members_set, chores_set) all_households.append(new_household_name) household_text_file.write(("{}, {}, ").format( household_obj, len(participants_each_household[new_household_name]))) for member_of in range( len(participants_each_household[new_household_name])): household_text_file.write(("{}, ").format( participants_each_household[new_household_name][member_of])) household_text_file.write(("{0:.1g}, ").format( len(chores_each_household[new_household_name]) / 2)) chores_in = 0 while chores_in < len(chores_each_household[new_household_name]) - 1: household_text_file.write(("{}, ").format( chores_each_household[new_household_name][chores_in])) chores_in += 1 household_text_file.write(("{} \n").format( chores_each_household[new_household_name][chores_in])) print(all_households) else: print("Household {} already exists, returning to the menu.".format( new_household_name)) return
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 create_household(all_households): new_household_name = get_household_name() found = False household_obj = household_exists(new_household_name, all_households) if household_obj == None: members_set = get_participants_names() chores_set = get_chores() household_obj = Household(new_household_name, members_set, chores_set) all_households.append(household_obj) # print(all_households) else: print("Household {} already exists, returning to the menu.".format( new_household_name)) return
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