From 25d13201617acbbbb5adba1df6743b4b2c8562ee Mon Sep 17 00:00:00 2001 From: Alex Schofield Date: Mon, 4 May 2026 21:18:15 +0100 Subject: preformat price strings before tabulate to avoid floatfmt/None issues --- fnme/cli.py | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) (limited to 'fnme/cli.py') diff --git a/fnme/cli.py b/fnme/cli.py index aa49ec1..203391c 100644 --- a/fnme/cli.py +++ b/fnme/cli.py @@ -9,6 +9,14 @@ from fnme.data import get_latest_data from fnme.geo import get_location from fnme.station import process_stations, sort_stations +_PRICE_COLS = { + "e5_price": "E5 (£/L)", + "e10_price": "E10 (£/L)", + "diesel_price": "B7S (£/L)", +} + +_HEADERS = {"station_name": "Station Name", "distance": "Distance (mi)", **_PRICE_COLS} + def parse_args() -> argparse.Namespace: parser = argparse.ArgumentParser() @@ -18,23 +26,18 @@ def parse_args() -> argparse.Namespace: return parser.parse_args() +def _fmt_price(v: float | None) -> str: + return f"{v:.2f}" if v is not None else "N/A" + + def output_stations(stations: List[Dict[str, Any]]) -> None: if not stations: print("[*] No stations found.") return - print( - tabulate( - stations, - headers={ - "station_name": "Station Name", - "distance": "Distance (miles)", - "e5_price": "E5 (£/L)", - "e10_price": "E10 (£/L)", - "diesel_price": "B7S (£/L)", - }, - floatfmt=".2f", - ) - ) + + rows = [{**s, **{col: _fmt_price(s[col]) for col in _PRICE_COLS}} for s in stations] + + print(tabulate(rows, headers=_HEADERS, floatfmt="1.f")) def main(): -- cgit v1.2.3