Home Practicum 1 Practicum 2 Practicum 3 Practicum 4 Practicum 5 Practicum 6 Practicum 7 Practicum 8 Practicum 9 Practicum 10 Practicum 11

Welcome to DS2000 CS Practicum 9!

Introduction

Before we start on the class, go to https://home.openweathermap.org/users/sign_up and request a free account. After you have registered go to https://openweathermap.org/price and press Get API key and Start. We're doing it early because it might take about an hour to be accepted

Today we will talk about:

  1. bias in the data and the implications of building data products from biased data
  2. dictionaries
  3. APIs

Bias in the data

Before you submit your project proposal, please read this article about predictive policing and this article about pre-trial risk assesment. When you plan your project think about:

This is not to discourage you from dealing with problems that involve data about people, but to make sure you are aware us programmers have a moral responsibility for the products we create.

Dictionaries

Dictionaries are data structures which hold information in the form of key, value pairs. Let's take this example:

piotr = {
	'name':'piotr', 
	'teacher':True, 
	'courses':['DS2001a', 'DS2001b'], 
	'years_at_neu':2
	'locations': [
		{
			'building':'isec',
			'office':660
		}, {
			'building':'wvh',
			'office':212
		}]
	}

This code creates a variable called piotr which is a dictionary. The keys in this dictionary are name, teacher, courses, location, and years_at_neu and the values can be of whatever type: strigs, booleans, lists, integers, other dictionaries, etc.

We can find the list of keys in a dictionary by running the keys() function on the variable that holds it, like this: piotr.keys().

We can find out the value associated with a specific key similarly to how we found the value at a certain position in the list, with square brackets, like this: piotr['name']. Similarly, to access the contents of a dictionary that's in a list that's a value in a dictionary, we use square brackets, like this: piotr['locations'][0]['building'] will return 'isec'.

Dictionaries can be stored in the JSON format on the disk, and we can load them from file like this:

import json # on top of the file we import the json library

with open('filename.json', encoding='utf-8') as f:
	data = json.load(f)	

Excercise 1

  1. Download this file. Have your main() function load it, print out the keys, print out the whole dictionary to inspect it.
  2. Make a function weather_formatter(weather_dict) that takes the dictionary as input, prints the name of the city, human readable weather description, and the expected temperature (hint: the file has the temperature in Kelvins, you want to write a function that converts that to Fahrenheit).
  3. Call that function in main.

APIs

Various online services provide access to their data using the interfaces called APIs. The file you worked with in the previous exercise came from the openweathermap API. Your API key should work right now, try by opening this link in your browser (rememver to replace YOUR_APP_ID with the API key you got in your email):
https://api.openweathermap.org/data/2.5/weather?q=Boston&appid=YOUR_APP_ID

If it has not yet been activated, you can try with mine.

You probably noticed the call to the API contains the location for which we want to get weather. Try getting the current weather in Somerville.

Exercice 2

Modify the example from the lecture so that it:
  1. Asks the user for a name of the city.
  2. Queries the openweathermap API for the weather in that city (remember that the example puts the API key after api_key parameter, but the openweathermap uses appid as the parameter name instead!)
  3. Uses your function from Excercise 1 to print the weather in the requested city nicely.