Simplify code

This commit is contained in:
Edward Betts 2024-04-17 14:48:18 +01:00
parent 32e07d4ce4
commit 0fcaf76104
3 changed files with 11 additions and 17 deletions

View file

@ -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]:

View file

@ -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),

View file

@ -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(