Skip to content

Commit 1bf8e84

Browse files
committed
Support for Python 3 alongside Python 2. Some tests are still broken
(dict/etc ordering different).
1 parent 8e5be1b commit 1bf8e84

5 files changed

Lines changed: 39 additions & 18 deletions

File tree

‎googlemaps/common.py‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@
2626
import hashlib
2727
import hmac
2828
import requests
29-
import urllib
29+
30+
try: # Python 3
31+
from urllib.parse import urlencode
32+
except ImportError: # Python 2
33+
from urllib import urlencode
3034

3135

3236
_VERSION = "0.1"
@@ -95,12 +99,12 @@ def _auth_url(self, path, params):
9599
"""
96100
if self.key:
97101
params["key"] = self.key
98-
return path + "?" + urllib.urlencode(params)
102+
return path + "?" + urlencode(params)
99103

100104
if self.client_id and self.client_secret:
101105
params["client"] = self.client_id
102106

103-
path = path + "?" + urllib.urlencode(params)
107+
path = path + "?" + urlencode(params)
104108
sig = _hmac_sign(self.client_secret, path)
105109
return path + "&signature=" + sig
106110

@@ -160,3 +164,15 @@ def _get(ctx, url, params, first_request_time=None):
160164

161165
# TODO(mdr-eng): use body["error_message"] if present.
162166
raise Exception("API error: %s" % body["status"])
167+
168+
def _isstr(v):
169+
"""Determines whether the passed value is a string, safe for 2/3.
170+
171+
:param v: Object to check
172+
:type ctx: basestring or object
173+
"""
174+
try:
175+
basestring
176+
except NameError:
177+
return isinstance(v, str)
178+
return isinstance(v, basestring)

‎googlemaps/directions.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def directions(ctx, origin, destination,
121121
return common._get(ctx, "/maps/api/directions/json", params)["routes"]
122122

123123
def _convert_waypoint(waypoint):
124-
if not isinstance(waypoint, basestring):
124+
if not common._isstr(waypoint):
125125
return convert.latlng(waypoint)
126126

127127
return waypoint

‎googlemaps/distance_matrix.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,5 +103,5 @@ def _convert_path(waypoints):
103103
waypoints = as_list(waypoints)
104104

105105
return convert.join_list("|",
106-
[(k if isinstance(k, basestring) else convert.latlng(k))
106+
[(k if common._isstr(k) else convert.latlng(k))
107107
for k in waypoints])

‎samples.py‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,28 @@ def main():
2626
timeout=5)
2727

2828
locations = [(40.714728, -73.998672), (-34.397, 150.644)]
29-
print googlemaps.elevation(c, locations)
30-
print googlemaps.elevation(c, (40.714728, -73.998672))
29+
print(googlemaps.elevation(c, locations))
30+
print(googlemaps.elevation(c, (40.714728, -73.998672)))
3131

32-
print googlemaps.elevation_along_path(c, locations, 3)
32+
print(googlemaps.elevation_along_path(c, locations, 3))
3333

3434
geocoded = googlemaps.geocode(c, "48 Pirrama Rd")
3535

36-
print geocoded
36+
print(geocoded)
3737

38-
print googlemaps.reverse_geocode(c, geocoded[0]["geometry"]["location"])
38+
print(googlemaps.reverse_geocode(c, geocoded[0]["geometry"]["location"]))
3939

40-
print googlemaps.reverse_geocode(c,
40+
print(googlemaps.reverse_geocode(c,
4141
latlng=(-33.86536501970851,151.1969187802915),
42-
result_type=["country", "political"])
42+
result_type=["country", "political"]))
4343

44-
print googlemaps.geocode(c, components={"country": "US"})
44+
print(googlemaps.geocode(c, components={"country": "US"}))
4545

46-
print googlemaps.directions(c,
46+
print(googlemaps.directions(c,
4747
"Sydney",
4848
"Melbourne",
4949
alternatives=True
50-
)
50+
))
5151

5252
if __name__ == '__main__':
5353
main()

‎test/test_common.py‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@
2121
import googlemaps
2222
from googlemaps import common
2323
import unittest
24-
import urlparse
24+
25+
try: # Python 3
26+
from urllib.parse import urlparse
27+
except ImportError: # Python 2
28+
from urlparse import urlparse
29+
2530

2631
# NOTE: the current version of "sesponses" doesn't have request_callback.
2732
# Use the master version until it's released.
@@ -53,7 +58,7 @@ def test_key_sent(self):
5358

5459
self.assertEquals(1, len(responses.calls))
5560

56-
url = urlparse.urlparse(responses.calls[0].request.url)
61+
url = urlparse(responses.calls[0].request.url)
5762
self.assertEquals("key=AIzaasdf&address=Sesame+St.", url.query)
5863

5964
def test_hmac(self):
@@ -83,7 +88,7 @@ def test_url_signed(self):
8388

8489
self.assertEquals(1, len(responses.calls))
8590

86-
url = urlparse.urlparse(responses.calls[0].request.url)
91+
url = urlparse(responses.calls[0].request.url)
8792
expected = "client=foo&address=Sesame+St.&signature=Ao1r8ULP1g_vPwnf7Fvf2TSCYBQ="
8893
self.assertEquals(expected, url.query)
8994

0 commit comments

Comments
 (0)