Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: DSA in Python
Stage 5: Specialization

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):

  1. argparse (from the Stage 1 project) reads --city, --live and --db.
  2. Without --live, it starts the fake API, which answers with the committed sample_weather.json. With --live, it calls the real Open-Meteo API at https://api.open-meteo.com, which is free and needs no API key.
  3. fetch_forecast() builds the URL with urlencode, calls it with a timeout, and turns HTTPError/URLError/TimeoutError into one WeatherApiError.
  4. to_readings() turns the JSON lists into Reading dataclasses (section 5).
  5. save_readings() uses executemany with ? and INSERT OR REPLACE (the primary key is (city, day), so running twice updates instead of duplicating).
  6. print_summary() uses COUNT, AVG, MIN, MAX, SUM and GROUP 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.

Search the guide