def create_golfer_scores(filename, golfer_list, tournament_list, rounds_list, tourn_golfers_list): """ Create GolferRoundScores objects from data in the input file, and using previously created object lists to convert names to ids. Each line of input contains: golfer_name, tourn_name, day, score_h1, score_h2, ..., score_h18 where golfer_name is the name of the golfer, tourn_name is the name of the tournament, day is the round day, and each score_h# is the golfer's score for that hole Note: string input needs to be stripped of any whitespace int strings need to be changed to ints Use the golfers_list parameter, that was returned from the create_golfers function, with the following instance variables: golfer_id, golfer_name, golfer_birthdate Use the tourns_list parameter, that was returned from the create_tournaments function, with the following instance variables: tourn_id, tournament_name, golf_course_id, start_date, num_rounds, and num_golfers Use the rounds_list object list parameter, that was returned from the create_rounds function, with the following instance variables: round_id, tourn_id, day Use the tourn_golfers_list parameter, that was returned from the create_tourn_golfers function, with the following instance variables: tourn_golfers_id, tourn_id, golfer_id Create a GolferRoundScores object from every entry in the golfer_scores_list: Add in the following as instance variables values: golfer_scores_id, tourn_golfer_id, round_id, total_round_score, and a list of scores (score_h1, score_h2, ..., score_h18) A list is returned, where each element is a GolferRoundScore object """ print("\nThe GolferRoundScores object list\n") round_scores_list = [] # 1. Create lookup dictionaries ... # a. Create a lookup dictionary (golfer_name_to_id) # for associating a golfer_name to golfer_id golfer_name_to_id = {} #loop to get golfer_name and golfer_id from golfer's list for item in golfer_list: #golfer_name will be the golfer_list[1] golfer_name = Golfer.get_golfer_name(item) #golfer_id will be the golfer_list[0] golfer_id = Golfer.get_golfer_id(item) golfer_name_to_id.update({golfer_name: golfer_id}) # b. Create a lookup dictionary (tourn_name_to_id) # for associating tourn_name to tourn_id tourn_name_to_id = {} #loop to get tourn_name and tourn_id from tournament_list for item in tournament_list: #tournament_name will be from the tourament list tourn_name = Tournament.get_tourn_name(item) #tourn_id will be from the tournament list tourn_id = Tournament.get_tourn_id(item) tourn_name_to_id.update({tourn_name: tourn_id}) # 2. Create an empty list called 'round_scores_list' that will be # filled in with GolferRoundScore objects whose data comes # from the input file and each of the object list parameters: # golfers_list, tourns_list, rounds_list, tourn_golfers_list round_scores_list = [] # 3. Initialize the golfer_scores_id golfer_scores_id = 1 # 4. Use a try/except block to capture a File Not Found Error try: # a. Open the input file object for reading the input file input_file = open(filename, 'r') # b. Call the csv.reader function, passing in the input file # and capturing the CSV file contents. file_lines = csv.reader(input_file) # c. Create a list from the file contents: 'golfer_scores_list' golfer_scores_list = list(file_lines) # d. Close input_file object input_file.close() # 5. Create an outer loop to read each set of scores in # 'golfer_scores_list' # Loop: for line in golfer_scores_list: # a. Get the golfer_name, tourn_name, and day from the # the first three elements, stripping whitespace. golfer_name = line[0].strip() tourn_name = line[1].strip() tourn_day = line[2].strip() # b. The rest of the elements (using slice scores[3:]) # are converted to a list of ints - scores_list. # Use Python's 'map' function to convert the strings to # ints and then use the 'list' function to convert the # object returned from the map to a list. scores_list = line[3:] scores_list = map(int, scores_list) scores_list = list(scores_list) # c. Get the golfer_id using the golfer_name_to_id # (from step 1a) golfer_id = golfer_name_to_id[golfer_name] # d. Get the tourn_id using the tourn_name_to_id # (from step 1b) tourn_id = tourn_name_to_id[tourn_name] # e. Call helper functions to get round_id, and the # tourn_golfer_id round_id = get_round_id(rounds_list, tourn_id, tourn_day) tourn_golfer_id = get_tourn_golfer_id(tourn_golfers_list, tourn_id, golfer_id) # f. Set the total_round_score by summing the scores_list total_round_score = sum(scores_list) # g. Create a new GolferRoundScores object, call it # golfer_scores, passing in golfer_scores_id, # tourn_golfer_id, round_id, total_round_score, and the # scores list (from step 5b.) golfer_scores = GolferRoundScores(golfer_scores_id, tourn_golfer_id, round_id, total_round_score, scores_list) # h. Append the GolferRoundScores object to the # round_scores_list round_scores_list.append(golfer_scores) # i. Increment the golfer_scores_id golfer_scores_id = golfer_scores_id + 1 except IOError: print("File Not Found Error.") # 6. Print the round_scores_list objects to the console for rs in round_scores_list: print(rs) # 7. Return the round_scores_list return round_scores_list