Skip to content

Commit

Permalink
REFACTOR: NAV-145 - Rename start / destination to source / target to …
Browse files Browse the repository at this point in the history
…be consistent with service
  • Loading branch information
munterfi committed Aug 21, 2024
1 parent b62ded5 commit f21a8b5
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 68 deletions.
116 changes: 58 additions & 58 deletions public_transit_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def _handle_response(response: Response):
response.raise_for_status()

def search_stops(
self, query: str, limit: int = 10, search_type: SearchType = SearchType.CONTAINS
self, query: str, limit: int = 10, search_type: SearchType = SearchType.CONTAINS
) -> list[Stop]:
"""Search for stops by a query string.
Expand All @@ -74,7 +74,7 @@ def search_stops(
return [Stop(**stop) for stop in data]

def nearest_stops(
self, coordinate: Coordinate, limit: int = 10, max_distance: int = 1000
self, coordinate: Coordinate, limit: int = 10, max_distance: int = 1000
) -> list[DistanceToStop]:
"""Find the nearest stops to a given coordinate.
Expand Down Expand Up @@ -108,11 +108,11 @@ def get_stop(self, stop_id: str) -> Stop | None:
return Stop(**data) if data else None

def get_next_departures(
self,
stop: str | Stop,
departure: datetime | None = None,
limit: int = 10,
until: datetime | None = None,
self,
stop: str | Stop,
departure: datetime | None = None,
limit: int = 10,
until: datetime | None = None,
) -> list[Departure]:
"""Retrieve the next departures from a specific stop.
Expand All @@ -136,22 +136,22 @@ def get_next_departures(
return [Departure(**dep) for dep in data]

def get_connections(
self,
start: Stop | Coordinate | str | tuple[float, float],
destination: str | Stop | Coordinate | tuple[float, float],
time: datetime | None = None,
time_type: TimeType = TimeType.DEPARTURE,
max_walking_duration: int | None = None,
max_transfer_number: int | None = None,
max_travel_time: int | None = None,
min_transfer_time: int | None = None,
self,
source: Stop | Coordinate | str | tuple[float, float],
target: str | Stop | Coordinate | tuple[float, float],
time: datetime | None = None,
time_type: TimeType = TimeType.DEPARTURE,
max_walking_duration: int | None = None,
max_transfer_number: int | None = None,
max_travel_time: int | None = None,
min_transfer_time: int | None = None,
) -> list[Connection]:
"""Retrieve a list of possible connections between two stops and or locations.
Args:
start (Stop | Coordinate | str | tuple[float, float]): The starting Stop object, Coordinate object, Stop ID or
source (Stop | Coordinate | str | tuple[float, float]): The starting Stop object, Coordinate object, Stop ID or
Coordinates tuple.
destrination (Stop | Coordinate | str | tuple[float, float]): The destination Stop object, Coordinate object,
target (Stop | Coordinate | str | tuple[float, float]): The destination Stop object, Coordinate object,
Stop ID or Coordinates tuple.
time (datetime, optional): The time for the connection search. Defaults to None (=now).
time_type (TimeType, optional): Whether the time is for departure or arrival. Defaults to DEPARTURE.
Expand All @@ -164,8 +164,8 @@ def get_connections(
list[Connection]: A list of Connection objects representing the possible routes.
"""
params = self._build_params_dict(
start,
destination,
source,
target,
time=time,
time_type=time_type,
max_walking_duration=max_walking_duration,
Expand All @@ -177,20 +177,20 @@ def get_connections(
return [Connection(**conn) for conn in data]

def get_isolines(
self,
start: Stop | Coordinate | str | tuple[float, float],
time: datetime | None = None,
time_type: TimeType = TimeType.DEPARTURE,
max_walking_duration: int | None = None,
max_transfer_number: int | None = None,
max_travel_time: int | None = None,
min_transfer_time: int | None = None,
return_connections: bool = False,
self,
source: Stop | Coordinate | str | tuple[float, float],
time: datetime | None = None,
time_type: TimeType = TimeType.DEPARTURE,
max_walking_duration: int | None = None,
max_transfer_number: int | None = None,
max_travel_time: int | None = None,
min_transfer_time: int | None = None,
return_connections: bool = False,
) -> list[StopConnection]:
"""Retrieve isolines (areas reachable within a certain time) from a specific stop / location.
Args:
start (Stop | Coordinate | str | tuple[float, float]): The starting Stop object, Coordinate object, Stop ID or
source (Stop | Coordinate | str | tuple[float, float]): The starting Stop object, Coordinate object, Stop ID or
Coordinates tuple.
time (datetime, optional): The time for the isoline calculation. Defaults to None (=now).
time_type (TimeType, optional): Whether the time is for departure or arrival. Defaults to DEPARTURE.
Expand All @@ -204,7 +204,7 @@ def get_isolines(
list[StopConnection]: A list of StopConnection objects representing the reachable areas.
"""
params = self._build_params_dict(
start,
source,
time=time,
time_type=time_type,
max_walking_duration=max_walking_duration,
Expand All @@ -221,43 +221,43 @@ def get_isolines(

@staticmethod
def _build_params_dict(
start: Stop | Coordinate | str | tuple[float, float],
destination: str | Stop | Coordinate | tuple[float, float] | None = None,
time: datetime | None = None,
time_type: TimeType | None = None,
max_walking_duration: int | None = None,
max_transfer_number: int | None = None,
max_travel_time: int | None = None,
min_transfer_time: int | None = None,
source: Stop | Coordinate | str | tuple[float, float],
target: str | Stop | Coordinate | tuple[float, float] | None = None,
time: datetime | None = None,
time_type: TimeType | None = None,
max_walking_duration: int | None = None,
max_transfer_number: int | None = None,
max_travel_time: int | None = None,
min_transfer_time: int | None = None,
) -> dict[str, str]:

if isinstance(start, Stop):
start = start.id
elif isinstance(start, Coordinate):
start = start.to_tuple()
if isinstance(source, Stop):
source = source.id
elif isinstance(source, Coordinate):
source = source.to_tuple()

if isinstance(destination, Stop):
destination = destination.id
elif isinstance(destination, Coordinate):
destination = destination.to_tuple()
if isinstance(target, Stop):
target = target.id
elif isinstance(target, Coordinate):
target = target.to_tuple()

params: dict[str, str] = {
"dateTime": (datetime.now() if time is None else time).strftime(
"%Y-%m-%dT%H:%M:%S"
),
}

if start is isinstance(start, tuple):
params["sourceLatitude"] = str(start[0]) # type: ignore
params["sourceLongitude"] = str(start[1]) # type: ignore
elif isinstance(start, str):
params["sourceStopId"] = start

if destination is isinstance(destination, tuple):
params["targetLatitude"] = str(destination[0]) # type: ignore
params["targetLongitude"] = str(destination[1]) # type: ignore
elif isinstance(destination, str):
params["targetStopId"] = destination
if source is isinstance(source, tuple):
params["sourceLatitude"] = str(source[0]) # type: ignore
params["sourceLongitude"] = str(source[1]) # type: ignore
elif isinstance(source, str):
params["sourceStopId"] = source

if target is isinstance(target, tuple):
params["targetLatitude"] = str(target[0]) # type: ignore
params["targetLongitude"] = str(target[1]) # type: ignore
elif isinstance(target, str):
params["targetStopId"] = target

if time_type:
params["timeType"] = time_type.value
Expand Down
16 changes: 8 additions & 8 deletions tests/integration/test_integration_routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def test_get_connections(client):
to_stop = "BULLFROG"
departure_time = datetime(2008, 6, 1)
connections = client.get_connections(
start=from_stop,
destination=to_stop,
source=from_stop,
target=to_stop,
time=departure_time,
time_type=TimeType.DEPARTURE,
)
Expand All @@ -42,8 +42,8 @@ def test_get_connections_invalid_stop(client):

with pytest.raises(PublicTransitClientException) as exc_info:
client.get_connections(
start=from_stop,
destination=to_stop,
source=from_stop,
target=to_stop,
time=departure_time,
time_type=TimeType.DEPARTURE,
)
Expand All @@ -60,8 +60,8 @@ def test_get_connections_negative_walking_duration(client):

with pytest.raises(PublicTransitClientException) as exc_info:
client.get_connections(
start=from_stop,
destination=to_stop,
source=from_stop,
target=to_stop,
time=departure_time,
time_type=TimeType.DEPARTURE,
max_walking_duration=-10,
Expand All @@ -79,7 +79,7 @@ def test_get_isolines(client):
from_stop = "NANAA"
departure_time = datetime(2008, 6, 1)
isolines = client.get_isolines(
start=from_stop,
source=from_stop,
time=departure_time,
time_type=TimeType.DEPARTURE,
max_walking_duration=10,
Expand All @@ -101,7 +101,7 @@ def test_get_isolines_invalid_stop(client):

with pytest.raises(PublicTransitClientException) as exc_info:
client.get_isolines(
start=from_stop,
source=from_stop,
time=departure_time,
time_type=TimeType.DEPARTURE,
max_walking_duration=10,
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def test_get_connections(client):
)

connections = client.get_connections(
start="NANAA", destination="BULLFROG", time=datetime(2024, 8, 18, 17, 0)
source="NANAA", target="BULLFROG", time=datetime(2024, 8, 18, 17, 0)
)

assert isinstance(connections, list)
Expand Down Expand Up @@ -219,7 +219,7 @@ def test_get_isolines(client):
)

isolines = client.get_isolines(
start="NANAA",
source="NANAA",
time=datetime(2024, 8, 18, 17, 0),
return_connections=True,
)
Expand Down

0 comments on commit f21a8b5

Please sign in to comment.