Simplify code
This commit is contained in:
parent
32e07d4ce4
commit
0fcaf76104
|
@ -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]:
|
||||
|
|
|
@ -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),
|
||||
|
|
11
web_view.py
11
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(
|
||||
|
|
Loading…
Reference in a new issue