diff --git a/googlemaps/convert.py b/googlemaps/convert.py index 2b3d056e..d9d5f4bc 100644 --- a/googlemaps/convert.py +++ b/googlemaps/convert.py @@ -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. diff --git a/googlemaps/distance_matrix.py b/googlemaps/distance_matrix.py index a30cbe09..c15efd93 100755 --- a/googlemaps/distance_matrix.py +++ b/googlemaps/distance_matrix.py @@ -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. @@ -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 diff --git a/tests/test_convert.py b/tests/test_convert.py index 39546aee..fba994ad 100644 --- a/tests/test_convert.py +++ b/tests/test_convert.py @@ -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",