GTFS Utils¶
This page documents utility functions in the GTFS module.
ingestor.chalicelib.gtfs.utils
¶
bucket_trips_by_hour(trips)
¶
Count trips by their starting hour of the day.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trips
|
List[Trip]
|
List of Trip objects with start_time in seconds since midnight. |
required |
Returns:
| Type | Description |
|---|---|
List[int]
|
A list of 24 integers, where each element is the number of trips |
List[int]
|
starting in that hour. |
Source code in ingestor/chalicelib/gtfs/utils.py
get_total_service_minutes(trips)
¶
Calculate the total service time in minutes across all trips.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trips
|
List[Trip]
|
List of Trip objects with start_time and end_time in seconds. |
required |
Returns:
| Type | Description |
|---|---|
int
|
The total service duration in minutes as an integer. |
Source code in ingestor/chalicelib/gtfs/utils.py
is_valid_route_id(route_id)
¶
Check whether a route ID represents a valid (non-shuttle) route.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
route_id
|
str
|
The MBTA route identifier string. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the route is valid, False if it is a shuttle route or |
bool
|
the Green Line shuttle (route 602). |
Source code in ingestor/chalicelib/gtfs/utils.py
bucket_by(items, key_getter)
¶
Group items into lists by a key derived from each item.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
List[any]
|
List of items to group. |
required |
key_getter
|
Union[str, Callable[[Any], str]]
|
A callable that extracts the grouping key from an item, or a string key for dictionary-style access. |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, List[any]]
|
A dictionary mapping each key to a list of items with that key. |
Source code in ingestor/chalicelib/gtfs/utils.py
index_by(items, key_getter)
¶
Index items into a dictionary by a key derived from each item.
If multiple items share the same key, the last item overwrites previous ones.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
items
|
List[any]
|
List of items to index. |
required |
key_getter
|
Union[str, Callable[[Any], str]]
|
A callable that extracts the index key from an item, or a string key for dictionary-style access. |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
A dictionary mapping each key to a single item. |
Source code in ingestor/chalicelib/gtfs/utils.py
date_range(start_date, end_date)
¶
Yield each date from start_date to end_date inclusive.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start_date
|
date
|
The first date in the range. |
required |
end_date
|
date
|
The last date in the range (inclusive). |
required |
Yields:
| Type | Description |
|---|---|
date
|
Each date from start_date through end_date. |
Source code in ingestor/chalicelib/gtfs/utils.py
get_service_ids_for_date_to_has_exceptions(models, today)
¶
Reports a dict of service IDs that are active on the given date mapped to a boolean indicating if there are any exceptions for that service on that date.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
models
|
SessionModels
|
The SessionModels containing calendar services and exceptions. |
required |
today
|
date
|
The date to evaluate service availability for. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, bool]
|
A dictionary mapping active service IDs to a boolean indicating whether |
dict[str, bool]
|
any calendar service exceptions apply on that date. |