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:
your project proposals
Dictionaries are data structures which hold information in the form of
value pairs. Let's take this example:
'courses':['DS2001 SEC 10', 'DS2001 SEC 11'],
This code creates a variable called
person which is a dictionary. The keys in this dictionary are
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:
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']['building'] will return
Excercise 1: Navigating the dictionary
Copy the dictionary above and paste it in your code.
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
piotrhas been at NEU for
2.5years? I looked at the variable
person, and accessed the value under the key called
How many courses do I teach?
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:
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:
Download this file. Have your
main()function load it, print out the keys, print out the whole dictionary to inspect it. 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). Call that function in main.
APIsVarious 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_IDwith the API key you got in your email):
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:
url = 'https://api.openweathermap.org/data/2.5/weather?q=Boston&appid=YOUR_APP_ID'
content = requests.get(url).text
data = json.loads(content)
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_formatterfunction to print a nice forecast for that city. What's the forecast for your hometown for today?