''' DS2000 Fall 2024 Starter code for Tuesday 11/26/24 (Sec 1) tvshow.py and sec1_recommendations.csv are both the same things from last week 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 In class, we need to add: - inspect correlations between attribute pairs - if/when we find a reasonably strong correlation, plot a line of best fit ''' import csv import statistics import seaborn as sns import matplotlib.pyplot as plt from tvshow_sec1_v2 import TVShow SHOW_FILE = "sec1_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, trope, length, eps, year = list(dct.values()) show = TVShow(title, rating, tone, trope, length, year = 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()