Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: multiple avoid distance matrix #382 #420

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions googlemaps/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,20 @@ def join_list(sep, arg):
"""
return sep.join(as_list(arg))

def to_list(arg, sep="|"):
"""Coerces arg into a list depends on supplied separator.
If arg is already list-like return arg.

:type sep: string
"""
if _is_list(arg):
return arg

if isinstance(arg, str):
elements = arg.split(sep)
return [element for element in elements if element]

return as_list(arg)

def as_list(arg):
"""Coerces arg into a list. If arg is already list-like, returns arg.
Expand Down
6 changes: 5 additions & 1 deletion googlemaps/distance_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ def distance_matrix(client, origins, destinations,

:param avoid: Indicates that the calculated route(s) should avoid the
indicated features. Valid values are "tolls", "highways" or "ferries".
You can pass muliple values by separating them using "|".
Example: "tolls|highways"
:type avoid: string

:param units: Specifies the unit system to use when displaying results.
Expand Down Expand Up @@ -107,7 +109,9 @@ def distance_matrix(client, origins, destinations,
params["language"] = language

if avoid:
if avoid not in ["tolls", "highways", "ferries"]:
valid_avoid_values = {"tolls", "highways", "ferries"}
supplied_avoid_values = set(convert.to_list(avoid))
if supplied_avoid_values - valid_avoid_values:
raise ValueError("Invalid route restriction.")
params["avoid"] = avoid

Expand Down
11 changes: 11 additions & 0 deletions tests/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,17 @@ def test_polyline_round_trip(self):
actual_polyline = convert.encode_polyline(points)
self.assertEqual(test_polyline, actual_polyline)

def test_to_list(self):
test_to_list = "tolls|highways"

elements = convert.to_list(test_to_list)

self.assertEqual(["tolls", "highways"], elements)
test_to_list = "tolls|highways|"

elements = convert.to_list(test_to_list)
self.assertEqual(["tolls", "highways"], elements)


@pytest.mark.parametrize(
"value, expected",
Expand Down