From 868c1407b50fc072b2815a5a6006ba63167a5f07 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Wed, 2 Oct 2024 15:36:26 +0100 Subject: [PATCH] Use pattern matching: train/flight/ferry --- agenda/trip.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/agenda/trip.py b/agenda/trip.py index 7ad10d2..a3cc35c 100644 --- a/agenda/trip.py +++ b/agenda/trip.py @@ -235,20 +235,20 @@ def get_locations(trip: Trip) -> dict[str, StrDict]: station_list = [] for t in trip.travel: - if t["type"] == "train": - station_list += [t["from_station"], t["to_station"]] - for leg in t["legs"]: - station_list.append(leg["from_station"]) - station_list.append(leg["to_station"]) - elif t["type"] == "flight": - for field in "from_airport", "to_airport": - if field in t: - locations["airport"][t[field]["iata"]] = t[field] - else: - assert t["type"] == "ferry" - for field in "from_terminal", "to_terminal": - terminal = t[field] - locations["ferry_terminal"][terminal["name"]] = terminal + match t["type"]: + case "train": + station_list += [t["from_station"], t["to_station"]] + for leg in t["legs"]: + station_list.append(leg["from_station"]) + station_list.append(leg["to_station"]) + case "flight": + for field in "from_airport", "to_airport": + if field in t: + locations["airport"][t[field]["iata"]] = t[field] + case "ferry": + for field in "from_terminal", "to_terminal": + terminal = t[field] + locations["ferry_terminal"][terminal["name"]] = terminal for s in station_list: if s["uic"] in locations["station"]: