|
| 1 | +from datetime import time |
| 2 | + |
| 3 | + |
| 4 | +def get_timetable(train): |
| 5 | + # Pretend this gets data from a server. |
| 6 | + return [ |
| 7 | + {"station": "target_field", "arrives": time(hour=16, minute=27)}, |
| 8 | + {"station": "fridley", "arrives": time(hour=16, minute=41)}, |
| 9 | + {"station": "coon_rapids_fridley", "arrives": time(hour=16, minute=50)}, |
| 10 | + {"station": "anoka", "arrives": time(hour=16, minute=54)}, |
| 11 | + {"station": "ramsey", "arrives": time(hour=16, minute=59)}, |
| 12 | + {"station": "elk_river", "arrives": time(hour=17, minute=4)}, |
| 13 | + {"station": "big_lake", "arrives": time(hour=17, minute=17)}, |
| 14 | + ] |
| 15 | + |
| 16 | + |
| 17 | +def next_station(now, timetable): |
| 18 | + """Return the name of the next station.""" |
| 19 | + station = None |
| 20 | + for stop in timetable: |
| 21 | + if stop['arrives'] > now: |
| 22 | + station = stop |
| 23 | + break |
| 24 | + station['station'] = station['station'].replace('_', ' ').title() |
| 25 | + return station |
| 26 | + |
| 27 | + |
| 28 | +def arrives_at(station, timetable): |
| 29 | + for stop in timetable: |
| 30 | + if station == stop['station']: |
| 31 | + return stop |
| 32 | + |
| 33 | + |
| 34 | +timetable = get_timetable('nstar_northbound') |
| 35 | + |
| 36 | +station = next_station(time(hour=16, minute=43), timetable) |
| 37 | +print(f"Next station is {station['station']}.") |
| 38 | + |
| 39 | +stop = arrives_at('coon_rapids_fridley', timetable) |
| 40 | +print(f"Arrives at {stop['arrives']}.") |
0 commit comments