A library for working with raw Bay Area Caltrain scheduling data in Python.
The purpose of this library is for easily making queries against the Caltrain schedule such as:
- The cost of travel from Sunnyvale to South San Francisco station.
- The next train(s) from 22nd Street to San Jose.
- The duration of travel between San Mateo and Menlo Park on a Limited train.
python-caltrain does not require an internet connection, making it easy to embed into offline applications. It relies on the Caltrain GTFS file, which is updated usually a couple of times per year at most. This library will update in accordance with announcements from Caltrain. The version number is year.month.rev to signify how recent it is.
e.g. 2016.4.0 means the library is at revision 0 and uses the April 2016 GTFS file.
Install via pip:
pip install python-caltrain
Let's find the next train from Sunnyvale to San Francisco 4th and King.
>>frompython_caltrainimportCaltrain>>c=Caltrain()>>n=c.next_trips('sunnyvale', 'sf')[0]>>n.departuredatetime.time(11, 26)>>n.arrivaldatetime.time(12, 43)>>n.durationdatetime.timedelta(0, 4620)Next train is at 12:43 PM from sunnyvale. Let's see what train number that is.
>>n.train.name'143'What kind of train is it?
>>str(n.train.kind) 'Local'Can you print a summary of the trip?
>>str(n) [Local143] Departs: 11:26:00, Arrives: 12:43:00 (1:17:00)Does that train stop at San Mateo? If so, when?
>>san_mateo=c.get_station('san mateo')>>san_mateoinn.train.stopsTrue>>n.train.stops[san_mateo].arrivaldatetime.time(12, 8)How much is this trip going to cost?
>>c.fare_between('sunnyvale', 'san francisco') (7, 75)Damn, that's expensive...
What if I want to know the next train after some point in the past or future?
>>fromdatetimeimportdatetime>>d= ... # Your date time here>>n=c.next_trips('sunnyvale', 'sf', after=d)Station names do not need to be sanitized. The Caltrain.get_station(...), Caltrain.next_trip(...), and Caltrain.fare_between(...) functions all perform sanitization themselves and can automatically resolve alternate common names for stations.
For example, sf, sanfrancisco, san fran, san francisco station are all understood as the same station. Same with 22nd, Twenty-Second, twenty second street, and 22nd str.