Paul's Python CoursePPC

Project 1 of 10, for Riya

Weather App

A forecast for anywhere, for Riya's painting trip: ask a real weather service, read the JSON it sends back, cope when it goes wrong, and print three plain days.

About 2 hours, in 14 steps.

Sign up free to open itLog in

Act 3 needs a free account, so your work is saved as you go. Act 1 is open to everyone.

The brief

  • RiyaI'm painting outdoors in Penzance next week, and painters have been going there for the light for over a hundred years.
  • RiyaRain ruins a watercolour and wind takes the easel, so I want three days of forecast in plain words.
  • RiyaIt has to come from a real service and credit its source, because Aaesha says a forecast without one's a rumour.
  • PaulIt's talking to another computer now, and that means new ways to fail, so you'll want a plan for each one.

Write weather.py. It asks Where? , looks the place up with Open-Meteo's geocoding service, asks Open-Meteo's forecast service about the next three days, and prints a plain forecast.

fetch_json(url) is written for you. It makes the real request and hands back the JSON as dictionaries and lists, or raises an OSError if it can't. Everything else works on replies that have already arrived:

  1. describe(code) returns the words for a weather code from WEATHER, or Unknown weather (code 42) for a code that isn't there.
  2. find_place(reply) takes the geocoding reply and returns (label, latitude, longitude) for the first place in its results. The label is the name, admin1 and country, leaving out any that are missing: Penzance, England, United Kingdom. If there are no results, it returns None.
  3. forecast_url(latitude, longitude) returns FORECAST, then a query string with latitude, longitude, daily set to DAILY, timezone set to auto and forecast_days set to 3.
  4. forecast_lines(forecast) returns one line per day, like Sun 25 Oct: Light drizzle, 10 to 13°C, 90% chance of rain, wind up to 37 km/h. Temperatures and wind are rounded with round(). A missing field raises a KeyError.
  5. main() puts them together and ends with Weather data by Open-Meteo.com. When something goes wrong it prints one line, and only that:
    • nothing typed: Type the name of a place., without asking the internet anything
    • no such place: No place called Lyonesse.
    • no connection, or the service says no: Can't reach the weather service. Try again later.
    • a field missing: The forecast came back without 'wind_speed_10m_max'.
What it prints
Where? Penzance
Penzance, England, United Kingdom
Sun 25 Oct: Light drizzle, 10 to 13°C, 90% chance of rain, wind up to 37 km/h
Mon 26 Oct: Rain, 12 to 16°C, 100% chance of rain, wind up to 46 km/h
Tue 27 Oct: Rain, 10 to 16°C, 96% chance of rain, wind up to 45 km/h
Weather data by Open-Meteo.com

What you learn

What's in it

  1. Asking another computer
  2. What came back
  3. The question goes in the URL
  4. Asking with a URL
  5. Request and response
  6. Three days, as text
  7. A family of errors
  8. One family of errors
  9. How Paul would do it
  10. Fix Caitlin's version
  11. The real request
  12. Make the weather app
  13. Ship it