''' DS2000 Fall 2024 Starter code for Friday 11/22/24 (Sec 1) In the starter code, we: - Read in a CSV file with TV show recommendations - Turn the CSV file into a list of dictionaries - Create TVShow object out of each one Iin class, we need to add: - Create another TVShow object that's our starting point (something the user liked) - Loop over CSV TVShows, and determine whether to recommend each one ''' import csv from tvshow import TVShow SHOW_FILE = "sec3_recommendations.csv" def csv_to_lst_of_dct(filename): ''' parameter: filename, a string returns: list of dictionaries (all key/value pairs are strings) does: creates a list of dictionaries where each dictionary represents one row from the CSV file. Dictionary keys are from the header of the csv file, and dictionary values are from the rows. ''' data = [] with open(filename, 'r') as infile: csvfile = csv.reader(infile) keys = next(csvfile) for row in csvfile: dct = row_to_dct(row, keys) data.append(dct) return data def row_to_dct(row, keys): ''' params: 2 lists of strings (one row from csv file, one list of keys) returns: a dictionary where keys come from one list, and values from the other does: turns lists into key/value pairs ''' dct = {} for i in range(len(row)): k = keys[i] v = row[i] dct[k] = v return dct def make_objects(lst): ''' params: list of dictionaries, where each dict is a row from a CSV file returns: list of TVShow objects does: iterates over list of dictionaries, creates one TVShow object each time ''' show_objs = [] for dct in lst: title, rating, tone, seasons, year, length = list(dct.values()) show = TVShow(title, rating, tone, length, seasons, year) show_objs.append(show) return show_objs def main(): # Gather data - make a list of TVShow objects show_data = csv_to_lst_of_dct(SHOW_FILE) tv_objs = make_objects(show_data) # Gather data - make a TVShow object for the user show user_show = TVShow("Agatha All Along", 9, 3, 35, 1, 2024) print(user_show) # Computation - make a list of TVShows to recommend recs = [] for show in tv_objs: print("Should I recommend", show, "?") if show.recommend(user_show): print("Yes!") recs.append(show) # Communicate - print out the recommended shows print("Recommendations...") for show in recs: print(show) main()