diff options
| author | Alex Schofield <git@ajschof.me> | 2026-05-04 21:10:50 +0100 |
|---|---|---|
| committer | Alex Schofield <git@ajschof.me> | 2026-05-04 21:10:50 +0100 |
| commit | 7a6c6bd36fd2651ca6cff7e2e3cb1b90df414100 (patch) | |
| tree | 13e74d8220ab8c5eab7fc80e73837fcb1df963a6 /fnme/station.py | |
| parent | 754f7dc26ddc82a11c45a114cbdd49c17786ae7b (diff) | |
| download | fuelnearme-7a6c6bd36fd2651ca6cff7e2e3cb1b90df414100.tar.gz fuelnearme-7a6c6bd36fd2651ca6cff7e2e3cb1b90df414100.zip | |
keep prices as float | None in process_stations() & sort_stations()
Diffstat (limited to 'fnme/station.py')
| -rw-r--r-- | fnme/station.py | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/fnme/station.py b/fnme/station.py index 82d17cc..109b927 100644 --- a/fnme/station.py +++ b/fnme/station.py @@ -6,6 +6,8 @@ import pandas as pd from fnme.constants import SORT_KV +_PRICE_KEYS = ("e5_price", "e10_price", "diesel_price") + def process_stations( dframe: pd.DataFrame, rad: int, loc: Tuple[float, float] @@ -32,7 +34,7 @@ def process_stations( return R * 2 * np.arcsin(np.sqrt(a)) def pence_to_pounds(col: pd.Series) -> pd.Series: - return (col / 100).round(2).where(col.notna(), other="N/A") + return (col / 100).round(2) df = bounding_box().copy() @@ -49,11 +51,21 @@ def process_stations( diesel_price=pence_to_pounds(df["forecourts.fuel_price.B7S"]), ) - return df.rename(columns={"forecourts.trading_name": "station_name"})[ - ["station_name", "distance", "e5_price", "e10_price", "diesel_price"] + records = df.rename(columns={"forecourts.trading_name": "station_name"})[ + ["station_name", *_PRICE_KEYS] ].to_dict(orient="records") + for record in records: + for key in _PRICE_KEYS: + v = record[key] + if isinstance(v, float) and math.isnan(v): + record[key] = None + + return records + def sort_stations(stations: list[dict], sort: str) -> list[dict]: sort_key = SORT_KV[sort] - return sorted(stations, key=lambda d: d[sort_key] if d[sort_key] != "N/A" else 999) + return sorted( + stations, key=lambda d: d[sort_key] if d[sort_key] is not None else float("inf") + ) |
