Home Practicum 1 Practicum 2 Practicum 3 Practicum 4 Practicum 5 Practicum 6 Practicum 7 Practicum 8

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

The practicum proper

Today we will talk about:

  1. your project proposals
  2. dictionaries
  3. APIs

Dictionaries

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

person = {
  'name':'piotr',
  'teacher':True,
  'courses':['DS2001 SEC 10', 'DS2001 SEC 11'],
  'years_at_neu':2.5,
  'locations': [
    {
      'building':'isec',
      'office':660
    }, {
      'building':'wvh',
      'office':212
    }
  ]
}

This code creates a variable called person 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: person.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: person['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: person['locations'][0]['building'] will return 'isec'.

Excercise 1: Navigating the dictionary

  1. Copy the dictionary above and paste it in your code.
  2. Use the key names to find and print the answers to following questions. (I know you immediately see the answers, you can use this fact to help you navigate the dictionary: "How do I know that piotr has been at NEU for 2.5 years? I looked at the variable person, and accessed the value under the key called years_at_neu.")
    1. How many courses do I teach?
    2. Loop through my locations, find the one that's in isec, report the office number.

Loading dictionaries from disk

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

content = open('filename.json', encoding='utf-8').read()
data = json.loads(content)

If you just call print(data) the dictionary will be printed in one long line, making it difficult to inspect. Instead, you can use this function to print it nicely:

print(json.dumps(data, indent=2))

Excercise 2

  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 argument, 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.

We previously loaded the dictionary from file on disk, but loading it from the Internet is not more difficult:

import requests #we *ALSO* import the library for talking to the internet
import json

url = 'https://api.openweathermap.org/data/2.5/weather?q=Boston&appid=YOUR_APP_ID'
content = requests.get(url).text
data = json.loads(content)

Excercise 3

  1. Write a function that takes city name as argument, queries the Weather API for the weather in that city rather than in Boston, and then use your weather_formatter function to print a nice forecast for that city.
  2. What's the forecast for your hometown for today?