def create_rounds(tournament_list): """ Use the tournament_list object list, that was returned from the create_tournaments function, which contains 15 Tournament objects with the following instance variables: tourn_id, tourn_name, golf_course_id, start_date, num_rounds, num_golfers, golfers... Create num_rounds Round objects from every Tournament object element in tournament_list: Add in the following as instance variables values round_id, tourn_id, day A list is returned, where each element is a Round object """ print("\nThe Round object list\n") rounds_list = [] round_id = 1 for tourn in tournament_list: number_rounds = Tournament.get_num_rounds(tourn) tourn_id = Tournament.get_tourn_id(tourn) num_rounds = number_rounds for item in range(number_rounds): if num_rounds == 4: day = "Thu" elif num_rounds == 3: day = "Fri" elif num_rounds == 2: day = "Sat" elif num_rounds == 1: day = "Sun" num_rounds = num_rounds - 1 round = Round(round_id, tourn_id, day) rounds_list.append(round) round_id = round_id + 1 for round_info in rounds_list: print(round_info) return rounds_list
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
def create_rounds(tournament_list): # Use the tournament_list object list, that was # returned from the create_tournaments function, which # contains 15 Tournament objects with the following # instance variables: # tourn_id, tourn_name, golf_course_id, start_date, # num_rounds, num_golfers, golfers... # Create num_rounds Round objects from every # Tournament object element in tournament_list: # Add in the following as instance variables values # round_id, tourn_id, day # A list is returned, where each element is a Round object # print("\nThe Round object list:\n") # 1. Create an empty list called rounds_list # that will be filled in with Round objects # whose data comes from the parameter - tournament_list rounds_list = [] # 2. Initialize the round_id round_id = 1 # 3. Create an outer loop to traverse the input # tournament_list, where the loop variable 'tourn' # will contain one of the Tournament objects in # tournament_list at each loop iteration for tourn in tournament_list: # a. Get the number_rounds and tourn_id from the # Tournament object, tourn, and initialize # num_rounds to number_rounds - this will be # decremented below to find the correct day # for the Round object being built number_rounds = Tournament.get_num_rounds(tourn) tourn_id = Tournament.get_tourn_id(tourn) # b. Create an inner loop to run number_rounds times # using the range function, where the loop # variable 'r' keeps the count for the # number of Rounds being created r = number_rounds for item in range(number_rounds): # 1. Check the value of num_rounds to determine # the day value of this Round object. # Use an if/elif/else structure to set the # day instance variable if r == 4: day = "Thu" elif r == 3: day = "Fri" elif r == 2: day = "Sat" elif r == 1: day = "Sun" # 2. Decrement the num_rounds counter r = r - 1 # 3. Create a Round object call it round passing # in round_id, tourn_id, and day round = Round(round_id, tourn_id, day) # 4. Append the Round object to the rounds_list rounds_list.append(round) # 5. Increment the round_id round_id = round_id + 1 # 4. Print the round objects to the console for rounds in rounds_list: print(rounds) #return rounds_list to console return rounds_list