Part 12 / Topic 11 of 11
Putting it together: weather report CLI
The task: a command-line tool that fetches a 5-day forecast for Pune, Delhi and Chennai, stores the readings in SQLite, and prints a summary made with SQL aggregates.
How it works (examples/07_weather_report.py):
argparse(from the Stage 1 project) reads--city,--liveand--db.- Without
--live, it starts the fake API, which answers with the committedsample_weather.json. With--live, it calls the real Open-Meteo API athttps://api.open-meteo.com, which is free and needs no API key. fetch_forecast()builds the URL withurlencode, calls it with a timeout, and turnsHTTPError/URLError/TimeoutErrorinto oneWeatherApiError.to_readings()turns the JSON lists intoReadingdataclasses (section 5).save_readings()usesexecutemanywith?andINSERT OR REPLACE(the primary key is(city, day), so running twice updates instead of duplicating).print_summary()usesCOUNT,AVG,MIN,MAX,SUMandGROUP BY.
python 07_weather_report.py # offline, all 3 cities
python 07_weather_report.py --city Pune # one city
python 07_weather_report.py --live # real data (needs internet)
python 07_weather_report.py --db demo_weather.db # keep the readings in a file
Output (offline):
Source: local sample data
Saved 5 days for Pune
Saved 5 days for Delhi
Saved 5 days for Chennai
City Days Avg max Hottest Coolest Rain mm Rainy
Delhi 5 37.4 39.2 26.9 21.7 2
Chennai 5 35.1 36.1 26.8 11.0 2
Pune 5 28.8 30.2 21.9 64.8 4
Hottest day: Delhi on 2026-07-02 (39.2°C)
With --live you see today's real numbers instead.
Done here? The practice set for Part 12 — 8 exercises with solutions — is on the part page.