caltrain-api 1.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,116 @@
1
+ Metadata-Version: 2.5
2
+ Name: caltrain-api
3
+ Version: 1.0.1
4
+ Summary: Caltrain schedules and train positions (lat/lon, milepost, and plain-English) from GTFS and 511 real-time data.
5
+ Project-URL: Homepage, https://github.com/jsinnott/caltrain-api
6
+ Project-URL: Repository, https://github.com/jsinnott/caltrain-api
7
+ Project-URL: Issues, https://github.com/jsinnott/caltrain-api/issues
8
+ Project-URL: Changelog, https://github.com/jsinnott/caltrain-api#changelog
9
+ Author-email: Joe Sinnott <joe@jsinnott.net>
10
+ License-Expression: MIT
11
+ License-File: LICENSE
12
+ Keywords: 511,bay area,caltrain,gtfs,gtfs-realtime,train,transit
13
+ Classifier: Development Status :: 5 - Production/Stable
14
+ Classifier: Intended Audience :: Developers
15
+ Classifier: Operating System :: OS Independent
16
+ Classifier: Programming Language :: Python :: 3
17
+ Classifier: Programming Language :: Python :: 3 :: Only
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Programming Language :: Python :: 3.13
21
+ Classifier: Programming Language :: Python :: 3.14
22
+ Classifier: Topic :: Scientific/Engineering :: GIS
23
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
24
+ Classifier: Typing :: Typed
25
+ Requires-Python: >=3.11
26
+ Requires-Dist: gtfs-realtime-bindings>=1.0
27
+ Requires-Dist: httpx>=0.27
28
+ Requires-Dist: platformdirs>=4
29
+ Requires-Dist: pydantic>=2.6
30
+ Provides-Extra: dev
31
+ Requires-Dist: mypy>=1.10; extra == 'dev'
32
+ Requires-Dist: pytest>=8; extra == 'dev'
33
+ Requires-Dist: types-protobuf; extra == 'dev'
34
+ Description-Content-Type: text/markdown
35
+
36
+ # caltrain-api
37
+
38
+ Caltrain schedules and train positions -- latitude/longitude, milepost from SF 4th & King, and plain-English descriptions ("approaching Redwood City") -- from Caltrain's static GTFS and 511.org real-time data.
39
+
40
+ ## Install
41
+
42
+ ```bash
43
+ pip install caltrain-api
44
+ ```
45
+
46
+ Python 3.11+. Real-time data needs a free [511.org API key](https://511.org/open-data/token); without one, everything works from the published timetable.
47
+
48
+ ## Usage
49
+
50
+ ```python
51
+ import os
52
+ from datetime import date
53
+ from caltrain_api import CaltrainAPI, Direction
54
+
55
+ api = CaltrainAPI(api_key=os.environ.get("CALTRAIN_511_API_KEY")) # None -> schedule only
56
+
57
+ for train in api.get_active_trains():
58
+ p = train.position # best estimate
59
+ print(train.number, train.service_type.value, f"mp {p.milepost:.1f}",
60
+ p.description, train.delay) # e.g. 124 local mp 30.0 at Palo Alto 0:01:58
61
+
62
+ train = api.get_train(124)
63
+ train.position # ESTIMATED: from real-time predicted times
64
+ train.reported_position # REPORTED: raw feed lat/lon (can be minutes stale)
65
+ train.scheduled_position # SCHEDULED: where the timetable says it should be
66
+ train.next_stop, train.stops, train.alerts
67
+
68
+ for d in api.get_departures("Palo Alto", direction=Direction.NORTHBOUND, limit=5):
69
+ print(d.train.number, d.stop.departure, d.stop.delay)
70
+
71
+ for j in api.get_schedule("Palo Alto", "San Francisco",
72
+ api.schedule.typical_service_date("weekday", date.today())):
73
+ print(j.train.number, j.origin_stop.scheduled_departure, j.scheduled_duration)
74
+
75
+ api.get_stations() # in milepost order, SF first
76
+ api.get_alerts()
77
+ ```
78
+
79
+ Every method that takes `at=` answers for any moment (timezone-aware datetimes); real-time data applies only within 5 minutes of now.
80
+
81
+ Positions offer three expressions: `lat`/`lon`, `milepost` (miles along the track from SF 4th & King = 0.0, ~76.8 at Gilroy; computed from GTFS geometry, so close to but not identical with Caltrain's official mileposts), and `description` plus a structured `relation` (`AT`, `PASSING`, `APPROACHING`, `DEPARTED`, `BETWEEN`, `WAITING`, `ARRIVED`). Thresholds (0.2 mi "at", 0.5 mi "approaching/departed") are configurable via `DescriptionPolicy`.
82
+
83
+ ### Data handling
84
+
85
+ - The static GTFS is downloaded on first use, cached in the user cache dir (`~/Library/Caches/caltrain-api` on macOS), and re-checked daily or when the feed expires. `api.refresh_schedule(force=True)` forces it. Offline, the cached copy is used.
86
+ - Real-time data is fetched lazily, at most every 120 s (511 allows 60 requests/hour; positions only change every few minutes). Empty feeds and failures keep the last good data for up to 10 minutes, then fall back to the schedule. Rate limiting backs off automatically.
87
+ - `CaltrainAPI` is thread-safe; share one instance.
88
+
89
+ See [docs/data-findings.md](https://github.com/jsinnott/caltrain-api/blob/master/docs/data-findings.md) for what the data sources actually provide and how that shaped these choices.
90
+
91
+ ## License
92
+
93
+ MIT
94
+
95
+ ## Development
96
+
97
+ From a clone of https://github.com/jsinnott/caltrain-api:
98
+
99
+ ```bash
100
+ uv sync --extra dev
101
+ .venv/bin/pytest tests/unit # offline, pinned fixtures
102
+ CALTRAIN_511_API_KEY=... .venv/bin/pytest -s tests/integration # live; ~2 511 requests
103
+ .venv/bin/mypy --strict src
104
+ ```
105
+
106
+ Unit tests use a pinned GTFS feed and a captured 511 response (2026-11-13 11:45 PT) in `tests/fixtures/`. `tools/one-off-*.py` are survey/measurement scripts for the live data sources.
107
+
108
+ ## Changelog
109
+
110
+ - 1.0.1 -- Packaging for PyPI: LICENSE file, project URLs, classifiers, `py.typed` marker (type checkers now use the library's annotations), README links that work on PyPI. No code changes.
111
+ - 1.0.0 -- Stable release: the public API (everything exported from `caltrain_api`) is now covered by semantic versioning. No code changes from 0.2.1.
112
+ - 0.2.1 -- Fix: a train waiting at its origin could be placed miles down the line, because 511 reports its equipment (often finishing a previous run) before departure. Reports no longer constrain estimates before departure, and WAITING/ARRIVED apply only near the origin/destination.
113
+ - 0.2.0 -- `CaltrainAPI.get_schedule(origin, destination, service_date)` returning `Journey` objects (point-to-point timetable); `Schedule.typical_service_date("weekday"|"weekend", on_or_after)` picks an ordinary non-holiday date.
114
+ - 0.1.0 -- First working release. `Schedule.load`/`refresh` (download, disk cache, conditional revalidation, offline fallback); `SiriRealtimeSource` (511 SIRI VehicleMonitoring + ServiceAlerts); `CaltrainAPI` facade with lazy polling, empty-feed/failure tolerance, rate-limit backoff, estimated/reported/scheduled positions, delays, alerts, `get_departures`.
115
+ - 0.0.1 -- Offline timetable: `Corridor`, `DescriptionPolicy`, `Schedule.from_zip`. `Train.trip_id` added; holiday "M"-prefixed trains parsed to plain numbers.
116
+ - 0.0.0 -- API stubs for review.
@@ -0,0 +1,14 @@
1
+ caltrain_api/__init__.py,sha256=N5dwk4JNIGcwHOdVHtaFg2qlEnPYlAM5pznX4Zhg2bw,1432
2
+ caltrain_api/_feedcache.py,sha256=mwaYLsNtSLZLcHUJZpcfBt-iTwnQDcUgfQzHxRxIxi4,5745
3
+ caltrain_api/api.py,sha256=O6WoLtB0aHmi1hTBQ7uy4cuxIAdLmjRJXrchAhb31gc,23209
4
+ caltrain_api/corridor.py,sha256=5MsFonOKK-ALWkFpl2Qu8VE0m1-axf-EwmY9Uhcv3e8,9277
5
+ caltrain_api/describe.py,sha256=uVGZsF7UwtyJWgfLNyj9-mt_PnGmZY1WtwBUE6rbwac,5430
6
+ caltrain_api/errors.py,sha256=6PyOuq8oYS2bwY7t3m9xU8mKBejRpi_Y-i8uJwj5KeM,1232
7
+ caltrain_api/models.py,sha256=8TfsEdzT8AQ5nSpMSCHDCROH5a_y9TUfa-OnECqi7os,12057
8
+ caltrain_api/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
9
+ caltrain_api/realtime.py,sha256=9RkoPme4nR6DILxhmmuTrV1U-Huyk-IrPi33Ea0T1AU,12389
10
+ caltrain_api/schedule.py,sha256=6yxd-z-yqmACTuaNtwg4GTAIm3ZSMM3CuIG6sMOt79E,32053
11
+ caltrain_api-1.0.1.dist-info/METADATA,sha256=XzTty3yhDcE6rr3Xa6pAQOnRtVqoeFlcfgemfflRdIo,6498
12
+ caltrain_api-1.0.1.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
13
+ caltrain_api-1.0.1.dist-info/licenses/LICENSE,sha256=d3EqOLj5sT-38eCkXN_1_JDenF5-mJCaV6eJjWzKxT4,1068
14
+ caltrain_api-1.0.1.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.33.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Joe Sinnott
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.