forked from CodeMouse92/DeadSimplePython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrain_timetable.py
More file actions
40 lines (30 loc) · 1.26 KB
/
train_timetable.py
File metadata and controls
40 lines (30 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from datetime import time
def get_timetable(train):
# Pretend this gets data from a server.
return [
{"station": "target_field", "arrives": time(hour=16, minute=27)},
{"station": "fridley", "arrives": time(hour=16, minute=41)},
{"station": "coon_rapids_fridley", "arrives": time(hour=16, minute=50)},
{"station": "anoka", "arrives": time(hour=16, minute=54)},
{"station": "ramsey", "arrives": time(hour=16, minute=59)},
{"station": "elk_river", "arrives": time(hour=17, minute=4)},
{"station": "big_lake", "arrives": time(hour=17, minute=17)},
]
def next_station(now, timetable):
"""Return the name of the next station."""
station = None
for stop in timetable:
if stop['arrives'] > now:
station = stop
break
station['station'] = station['station'].replace('_', ' ').title()
return station
def arrives_at(station, timetable):
for stop in timetable:
if station == stop['station']:
return stop
timetable = get_timetable('nstar_northbound')
station = next_station(time(hour=16, minute=43), timetable)
print(f"Next station is {station['station']}.")
stop = arrives_at('coon_rapids_fridley', timetable)
print(f"Arrives at {stop['arrives']}.")