From a2b30a9ac71e7fa3494c65a4a38a3d4a5d2ededd Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Mon, 11 Sep 2023 11:11:07 +0530 Subject: [PATCH] Use most recent CSV list of flights. --- show | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/show b/show index 34517c5..36d804a 100755 --- a/show +++ b/show @@ -1,10 +1,11 @@ #!/usr/bin/python3 import csv +import os from datetime import datetime, timedelta +from typing import Callable -# from pprint import pprint - +backup_loc = os.path.expanduser("~/backup/openflights") now = datetime.now() since_date = now - timedelta(days=365 * 5) @@ -21,6 +22,12 @@ Flight = dict[str, str] Flights = list[tuple[datetime, Flight]] +def get_most_recent(loc: str, pred: Callable[[str], bool] | None = None) -> str | None: + """Get the most recent file in a directory.""" + files = [f for f in os.listdir(loc) if pred is None or pred(f)] + return os.path.join(loc, max(files)) if files else None + + def format_row(dt: datetime, f: Flight) -> str: """Format flight.""" d = dt.strftime("%a, %d %b %Y") @@ -82,6 +89,7 @@ def show_flights(flights: Flights) -> None: if __name__ == "__main__": - filename = "/home/edward/backup/openflights/openflights-backup-2023-09-09.csv" + filename = get_most_recent(backup_loc, lambda f: f.endswith(".csv")) + assert filename flights = read_flights(filename) show_flights(flights)