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.
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:
describe(code)returns the words for a weather code fromWEATHER, orUnknown weather (code 42)for a code that isn't there.find_place(reply)takes the geocoding reply and returns(label, latitude, longitude)for the first place in itsresults. The label is thename,admin1andcountry, leaving out any that are missing:Penzance, England, United Kingdom. If there are no results, it returnsNone.forecast_url(latitude, longitude)returnsFORECAST, then a query string withlatitude,longitude,dailyset toDAILY,timezoneset toautoandforecast_daysset to3.forecast_lines(forecast)returns one line per day, likeSun 25 Oct: Light drizzle, 10 to 13°C, 90% chance of rain, wind up to 37 km/h. Temperatures and wind are rounded withround(). A missing field raises aKeyError.main()puts them together and ends withWeather 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'.
- nothing typed:
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
- Asking another computer
- What came back
- The question goes in the URL
- Asking with a URL
- Request and response
- Three days, as text
- A family of errors
- One family of errors
- How Paul would do it
- Fix Caitlin's version
- The real request
- Make the weather app
- Ship it