feat: list next departures across stops with day rollover
Co-Authored-By: Claude Haiku 4.5 <[email protected]>
This commit is contained in:
1 parent
2eecbab365
commit
ce52e38f66
2 files changed
+142
-5
No files matched your search
+37
-2
@@ -1,8 +1,11 @@
|
||||
"""Pick the right timetable column for a day and list upcoming departures."""
|
||||
from datetime import date
|
||||
from dataclasses import dataclass
|
||||
from datetime import date, datetime, timedelta
|
||||
from typing import Container, Sequence
|
||||
|
||||
from timetable import KIND_REST_DAY, KIND_SPECIAL, KIND_WORKDAY, Column
|
||||
from timetable import KIND_REST_DAY, KIND_SPECIAL, KIND_WORKDAY, Column, Departure, Timetable
|
||||
|
||||
LOOKAHEAD_DAYS = 3 # days after today we search before giving up
|
||||
|
||||
|
||||
def is_rest_day(day: date, state_holidays: Container[date]) -> bool:
|
||||
@@ -30,3 +33,35 @@ def select_column(columns: Sequence[Column], day: date, rest_day: bool) -> Colum
|
||||
return None
|
||||
started = [c for c in candidates if c.valid_from <= day]
|
||||
return started[-1] if started else candidates[0]
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Upcoming:
|
||||
departure: Departure
|
||||
when: datetime
|
||||
minutes: int # whole minutes from now; 0 means "leaving now"
|
||||
next_day: bool # departs on a later calendar day than now
|
||||
|
||||
|
||||
def next_departures(
|
||||
timetables: Sequence[Timetable], now: datetime, count: int, state_holidays: Container[date]
|
||||
) -> list[Upcoming]:
|
||||
"""The next `count` departures at or after `now` (minute precision), earliest first."""
|
||||
start = now.replace(second=0, microsecond=0)
|
||||
found: list[Upcoming] = []
|
||||
for offset in range(LOOKAHEAD_DAYS + 1):
|
||||
day = start.date() + timedelta(days=offset)
|
||||
rest = is_rest_day(day, state_holidays)
|
||||
for timetable in timetables:
|
||||
column = select_column(timetable.columns, day, rest)
|
||||
if column is None:
|
||||
continue
|
||||
for dep in column.departures:
|
||||
when = datetime.combine(day, dep.time)
|
||||
if when >= start:
|
||||
minutes = int((when - start).total_seconds() // 60)
|
||||
found.append(Upcoming(dep, when, minutes, day != now.date()))
|
||||
if len(found) >= count:
|
||||
break
|
||||
found.sort(key=lambda u: (u.when, u.departure.line))
|
||||
return found[:count]
|
||||
+105
-3
@@ -1,7 +1,8 @@
|
||||
from datetime import date
|
||||
from datetime import date, datetime, time
|
||||
|
||||
from schedule import is_rest_day, select_column
|
||||
from timetable import KIND_REST_DAY, KIND_SPECIAL, KIND_WORKDAY, Column
|
||||
from schedule import is_rest_day, next_departures, select_column
|
||||
from tests.helpers import load_timetables
|
||||
from timetable import KIND_REST_DAY, KIND_SPECIAL, KIND_WORKDAY, Column, Departure, Timetable
|
||||
|
||||
|
||||
def col(kind, valid_from, dates=()):
|
||||
@@ -62,3 +63,104 @@ def test_special_date_beats_rest_day():
|
||||
def test_no_column_of_the_needed_kind_returns_none():
|
||||
assert select_column([WORK_NEW], date(2026, 10, 10), rest_day=True) is None
|
||||
assert select_column([], date(2026, 10, 6), rest_day=False) is None
|
||||
|
||||
|
||||
def dep(line, stop, h, m):
|
||||
return Departure(line, stop, time(h, m))
|
||||
|
||||
|
||||
def column(kind, valid_from, deps, dates=()):
|
||||
return Column(kind, kind, valid_from, tuple(dates), tuple(deps))
|
||||
|
||||
|
||||
V = date(2026, 10, 1)
|
||||
STOP_A, STOP_B = "U potoka", "Družstvo"
|
||||
A = Timetable(
|
||||
"4",
|
||||
STOP_A,
|
||||
(
|
||||
column(KIND_WORKDAY, V, [dep("4", STOP_A, 10, 0), dep("4", STOP_A, 10, 30), dep("4", STOP_A, 23, 6)]),
|
||||
column(KIND_REST_DAY, V, [dep("4", STOP_A, 9, 15)]),
|
||||
column(KIND_SPECIAL, V, [dep("4", STOP_A, 6, 45)], dates=[date(2026, 10, 29)]),
|
||||
),
|
||||
)
|
||||
B = Timetable("35", STOP_B, (column(KIND_WORKDAY, V, [dep("35", STOP_B, 10, 10)]),))
|
||||
TUE_0950 = datetime(2026, 10, 6, 9, 50, 0)
|
||||
|
||||
|
||||
def lines_and_times(upcoming):
|
||||
return [(u.departure.line, u.when.strftime("%H:%M")) for u in upcoming]
|
||||
|
||||
|
||||
def test_merges_timetables_earliest_first(cz):
|
||||
up = next_departures([A, B], TUE_0950, 3, cz)
|
||||
assert lines_and_times(up) == [("4", "10:00"), ("35", "10:10"), ("4", "10:30")]
|
||||
|
||||
|
||||
def test_minutes_until_departure(cz):
|
||||
first = next_departures([A, B], TUE_0950, 1, cz)[0]
|
||||
assert first.minutes == 10 and first.next_day is False
|
||||
|
||||
|
||||
def test_departure_in_the_current_minute_is_now(cz):
|
||||
first = next_departures([A], datetime(2026, 10, 6, 10, 0, 40), 1, cz)[0]
|
||||
assert first.when == datetime(2026, 10, 6, 10, 0) and first.minutes == 0
|
||||
|
||||
|
||||
def test_departed_bus_is_dropped(cz):
|
||||
first = next_departures([A], datetime(2026, 10, 6, 10, 1, 0), 1, cz)[0]
|
||||
assert first.when == datetime(2026, 10, 6, 10, 30)
|
||||
|
||||
|
||||
def test_rolls_into_next_day_after_last_departure(cz):
|
||||
up = next_departures([A], datetime(2026, 10, 6, 23, 30), 2, cz) # last bus today was 23:06
|
||||
assert [(u.when, u.next_day) for u in up] == [
|
||||
(datetime(2026, 10, 7, 10, 0), True),
|
||||
(datetime(2026, 10, 7, 10, 30), True),
|
||||
]
|
||||
assert up[0].minutes == 630
|
||||
|
||||
|
||||
def test_state_holiday_on_weekday_uses_rest_day_column(cz):
|
||||
first = next_departures([A], datetime(2026, 10, 28, 8, 0), 1, cz)[0] # Wednesday, Statehood Day
|
||||
assert first.when == datetime(2026, 10, 28, 9, 15)
|
||||
|
||||
|
||||
def test_saturday_uses_rest_day_column(cz):
|
||||
first = next_departures([A], datetime(2026, 10, 10, 8, 0), 1, cz)[0]
|
||||
assert first.when == datetime(2026, 10, 10, 9, 15)
|
||||
|
||||
|
||||
def test_special_date_uses_special_column(cz):
|
||||
first = next_departures([A], datetime(2026, 10, 29, 6, 0), 1, cz)[0]
|
||||
assert first.when == datetime(2026, 10, 29, 6, 45)
|
||||
|
||||
|
||||
def test_nothing_usable_returns_empty_list(cz):
|
||||
empty = Timetable("9", "X", ())
|
||||
assert next_departures([empty], TUE_0950, 3, cz) == []
|
||||
assert next_departures([], TUE_0950, 3, cz) == []
|
||||
|
||||
|
||||
def test_real_timetables_in_the_afternoon(cz):
|
||||
up = next_departures(load_timetables(), datetime(2026, 10, 6, 13, 28, 20), 5, cz)
|
||||
assert lines_and_times(up) == [
|
||||
("4", "13:33"),
|
||||
("4", "13:53"),
|
||||
("4", "14:08"),
|
||||
("5", "14:25"),
|
||||
("36", "14:27"),
|
||||
]
|
||||
assert up[0].minutes == 5
|
||||
|
||||
|
||||
def test_real_special_date_includes_line35(cz):
|
||||
first = next_departures(load_timetables(), datetime(2026, 10, 29, 5, 0), 1, cz)[0]
|
||||
assert (first.departure.line, first.departure.mark) == ("35", "v")
|
||||
assert first.when == datetime(2026, 10, 29, 5, 16)
|
||||
|
||||
|
||||
def test_real_timetables_late_evening_show_tomorrow(cz):
|
||||
up = next_departures(load_timetables(), datetime(2026, 10, 6, 23, 52, 30), 3, cz)
|
||||
assert all(u.next_day for u in up)
|
||||
assert up[0].when == datetime(2026, 10, 7, 4, 16)
|
||||
Reference in new issue
Block a user