''' temps.py DS2000 Spring 2022 <<>>> We added to this during class: - Write a main function that calls our csv_to_dct function, calls our dct_to_weather function, and creates a scatterplot of all our weather locations, using current temperature data <<>>> There's nothing new in here, don't worry! Just reusing two functions we've done a lot recently: 1 - csv_to_dct, read a CSV file into a list of dictionaries (one dictionary per row in file, keys come from first row) 2 - dct_to_weather, turn a list of dictionaries into objects (in this case, we're making Weather objects, but the function is basically the same as making Movie objects, and Review objects) ''' import csv import matplotlib.pyplot as plt from weather import Weather LOCATIONS = "locations.csv" def csv_to_dict(filename): ''' Function: csv_to_dict Parameter: filename, a string Returns: a list of dictionaries where each dictionary reps one row of the file; keys of all dictionaries are file's first row ''' data = [] with open(filename, "r") as infile: csvfile = csv.reader(infile, delimiter = ",") keys = next(csvfile) for row in csvfile: d = {} for i in range(len(row)): d[keys[i]] = row[i] data.append(d) return data def dct_to_weather(list_of_dct): ''' Function: dct_to_objs Parameters: list of dictionaries, where each one reps one location Returns: list of Weather objects ''' weathers = [] for d in list_of_dct: lat = float(d["latitude"]) long = float(d["longitude"]) w = Weather(lat, long) weathers.append(w) return weathers def main(): # Step one -- gather data from the file data = csv_to_dict(LOCATIONS) # Step two --- turn list of dictionaries into objects weathers = dct_to_weather(data) # Step three -- commmunicate! Make the weather map lats = [w.lat for w in weathers] longs = [w.long for w in weathers] colors = [w.color for w in weathers] plt.scatter(longs, lats, color = colors) main()