Sanitize prices: drop out-of-band rows (50-500p) that poisoned the cheapest reference and deltas
This commit is contained in:
+14
-3
@@ -215,22 +215,33 @@ def _fuel_price(station: dict, fuel: str) -> Optional[float]:
|
|||||||
"""Extract a price for the fuel grade from a station dict.
|
"""Extract a price for the fuel grade from a station dict.
|
||||||
|
|
||||||
The Fuel Finder payload nests prices under keys like 'prices' -> 'E10'.
|
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 {}
|
prices = station.get("prices") or station.get("price") or {}
|
||||||
if isinstance(prices, dict):
|
if isinstance(prices, dict):
|
||||||
for key in (fuel.upper(), fuel.upper() + "_PRICE", "price_" + fuel.upper()):
|
for key in (fuel.upper(), fuel.upper() + "_PRICE", "price_" + fuel.upper()):
|
||||||
if key in prices:
|
if key in prices:
|
||||||
val = prices[key]
|
val = prices[key]
|
||||||
if isinstance(val, (int, float)):
|
if isinstance(val, (int, float)):
|
||||||
return float(val)
|
raw = float(val)
|
||||||
|
break
|
||||||
# flat fallback
|
# flat fallback
|
||||||
|
if raw is None:
|
||||||
for key in (fuel.upper(), "PRICE_" + fuel.upper()):
|
for key in (fuel.upper(), "PRICE_" + fuel.upper()):
|
||||||
if key in station:
|
if key in station:
|
||||||
val = station[key]
|
val = station[key]
|
||||||
if isinstance(val, (int, float)):
|
if isinstance(val, (int, float)):
|
||||||
return float(val)
|
raw = float(val)
|
||||||
|
break
|
||||||
|
if raw is None:
|
||||||
return 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