''' DS2000 Fall 2024 Starter code for Friday 11/22/24 (Sec 3) 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 import statistics import seaborn as sns import matplotlib.pyplot as plt from tvshow_sec3_v2 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()) length = int(length.split()[0]) show = TVShow(title, rating, tone, length, seasons, int(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) # Computation: is there a correlation between any two attributes? # Communication: plot the line of best fit between the two attributes # This is a prediction model -- given a new x, what would y be? main()