diff options
| author | Alex Schofield <git@ajschof.me> | 2026-04-25 08:20:55 +0100 |
|---|---|---|
| committer | Alex Schofield <git@ajschof.me> | 2026-04-25 08:20:55 +0100 |
| commit | ab9f42382fe4980358faea370a828b6b2968b675 (patch) | |
| tree | 8134dfda2369e578995e3b6ecda6b364f10941d4 /main.py | |
| parent | 647f7ae1d81d2537a4968a5d28d39f6aec22e557 (diff) | |
| download | fuelnearme-ab9f42382fe4980358faea370a828b6b2968b675.tar.gz fuelnearme-ab9f42382fe4980358faea370a828b6b2968b675.zip | |
replace match-case with dict lookup in sort function
Diffstat (limited to 'main.py')
| -rw-r--r-- | main.py | 26 |
1 files changed, 11 insertions, 15 deletions
@@ -11,6 +11,13 @@ from geopy.location import Location ENDPOINT = "https://www.fuel-finder.service.gov.uk/internal/v1.0.2/csv/get-latest-fuel-prices-csv" +SORT_KV = { + "e10": "e10_price", + "e5": "e5_price", + "b7s": "diesel_price", + "distance": "distance", +} + def parse_args() -> argparse.Namespace: parser = argparse.ArgumentParser() @@ -63,20 +70,9 @@ def filter_df(dframe, arguments, loc): return near_stations -def sort_list_of_stations(stations_list, arguments): - match arguments.sort: - case "e10": - sort_by = "e10_price" - return sorted(stations_list, key=lambda d: d[sort_by]) - case "e5": - sort_by = "e5_price" - return sorted(stations_list, key=lambda d: d[sort_by]) - case "b7s": - sort_by = "diesel_price" - return sorted(stations_list, key=lambda d: d[sort_by]) - case "distance": - sort_by = "distance" - return sorted(stations_list, key=lambda d: d[sort_by]) +def sort_stations(stations: list[dict], sort: str) -> list[dict]: + sort_key = SORT_KV.get(sort) + return sorted(stations, key=lambda d: d[sort_key]) def output_stations(stations): @@ -101,7 +97,7 @@ def main(): df_filtered = filter_df(df_processed, args, location) - sorted_stations_list = sort_list_of_stations(df_filtered, args) + sorted_stations_list = sort_stations(df_filtered, args.sort) output_stations(sorted_stations_list) |
