Sanitize prices: drop out-of-band rows (50-500p) that poisoned the cheapest reference and deltas
This commit is contained in:
+19
-8
@@ -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
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user