From 0fcaf761044d3ed07583209673fb7ec9f659979c Mon Sep 17 00:00:00 2001
From: Edward Betts <edward@4angle.com>
Date: Wed, 17 Apr 2024 14:48:18 +0100
Subject: [PATCH] Simplify code

---
 agenda/travel.py | 12 +++++++++++-
 agenda/trip.py   |  5 -----
 web_view.py      | 11 -----------
 3 files changed, 11 insertions(+), 17 deletions(-)

diff --git a/agenda/travel.py b/agenda/travel.py
index 9299408..27f7152 100644
--- a/agenda/travel.py
+++ b/agenda/travel.py
@@ -1,5 +1,6 @@
 """Travel."""
 
+import decimal
 import json
 import os
 import typing
@@ -43,7 +44,16 @@ def route_distances_as_json(route_distances: RouteDistances) -> str:
 def parse_yaml(travel_type: str, data_dir: str) -> TravelList:
     """Parse flights YAML and return list of travel."""
     filepath = os.path.join(data_dir, travel_type + ".yaml")
-    return typing.cast(TravelList, yaml.safe_load(open(filepath)))
+    items: TravelList = yaml.safe_load(open(filepath))
+    if not all(isinstance(item, dict) for item in items):
+        return items
+
+    for item in items:
+        price = item.get("price")
+        if price:
+            item["price"] = decimal.Decimal(price)
+
+    return items
 
 
 def get_flights(data_dir: str) -> list[Event]:
diff --git a/agenda/trip.py b/agenda/trip.py
index a5f3537..d7136f3 100644
--- a/agenda/trip.py
+++ b/agenda/trip.py
@@ -101,11 +101,6 @@ def build_trip_list(
         key=depart_datetime,
     )
 
-    for item in travel_items:
-        price = item.get("price")
-        if price:
-            item["price"] = decimal.Decimal(price)
-
     data = {
         "travel": travel_items,
         "accommodation": travel.parse_yaml("accommodation", data_dir),
diff --git a/web_view.py b/web_view.py
index 4cb2c28..0fc1e57 100755
--- a/web_view.py
+++ b/web_view.py
@@ -2,7 +2,6 @@
 
 """Web page to show upcoming events."""
 
-import decimal
 import inspect
 import operator
 import os.path
@@ -126,12 +125,6 @@ def travel_list() -> str:
         if all("distance" in leg for leg in train["legs"]):
             train["distance"] = sum(leg["distance"] for leg in train["legs"])
 
-    for travel_type in flights, trains:
-        for item in travel_type:
-            price = item.get("price")
-            if price:
-                item["price"] = decimal.Decimal(price)
-
     return flask.render_template("travel.html", flights=flights, trains=trains)
 
 
@@ -187,10 +180,6 @@ def accommodation_list() -> str:
     """Page showing a list of past, present and future accommodation."""
     data_dir = app.config["PERSONAL_DATA"]
     items = travel.parse_yaml("accommodation", data_dir)
-    for item in items:
-        price = item.get("price")
-        if price:
-            item["price"] = decimal.Decimal(price)
 
     stays_in_2024 = [item for item in items if item["from"].year == 2024]
     total_nights_2024 = sum(