From 34fc04ef59c69931b07d223668ea2e20cf5421cc Mon Sep 17 00:00:00 2001 From: FuelBoard Contributor Date: Tue, 11 Aug 2026 19:56:15 +0100 Subject: [PATCH] Sanitize prices: drop out-of-band rows (50-500p) that poisoned the cheapest reference and deltas --- app/main.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/app/main.py b/app/main.py index d39bff1..ccd77a2 100644 --- a/app/main.py +++ b/app/main.py @@ -215,22 +215,33 @@ def _fuel_price(station: dict, fuel: str) -> Optional[float]: """Extract a price for the fuel grade from a station dict. The Fuel Finder payload nests prices under keys like 'prices' -> 'E10'. - Tolerates both flat and nested shapes. + Tolerates both flat and nested shapes. Returns None for prices outside a + sane band (50-500 pence/litre): the dataset contains junk rows (e.g. + 1.299p from a pounds-denominated column, or 1589p) that would otherwise + poison the nationwide "cheapest" reference and every delta. """ + raw = None prices = station.get("prices") or station.get("price") or {} if isinstance(prices, dict): for key in (fuel.upper(), fuel.upper() + "_PRICE", "price_" + fuel.upper()): if key in prices: val = prices[key] if isinstance(val, (int, float)): - return float(val) + raw = float(val) + break # flat fallback - for key in (fuel.upper(), "PRICE_" + fuel.upper()): - if key in station: - val = station[key] - if isinstance(val, (int, float)): - return float(val) - return None + if raw is None: + for key in (fuel.upper(), "PRICE_" + fuel.upper()): + if key in station: + val = station[key] + if isinstance(val, (int, float)): + raw = float(val) + break + if raw is None: + return None + if raw < 50 or raw > 500: + return None # junk row — treat as not selling this grade + return raw # ---------------------------------------------------------------------------