Skip to content
Merged
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
Generating documentation:

$ sphinx-build -b html docs docs/html

Running tests:

$ python -m unittest discover
25 changes: 7 additions & 18 deletions googlemaps/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ def latlng(arg):
"""Converts a lat/lon pair to a comma-separated string.

Accepts various representations:
1) comma-separated string
2) dict with two entries - "lat" and "lng"
3) list or tuple - e.g. (-33, 151) or [-33, 151]
1) dict with two entries - "lat" and "lng"
2) list or tuple - e.g. (-33, 151) or [-33, 151]

For example:

Expand All @@ -33,11 +32,8 @@ def latlng(arg):
# '-33.8674869,151.2069902'

:param arg: The lat/lon pair.
:type arg: basestring or dict or list
:type arg: dict or list or tuple
"""
if isinstance(arg, basestring):
return arg

if isinstance(arg, dict):
if "lat" in arg and "lng" in arg:
return "%f,%f" % (arg["lat"], arg["lng"])
Expand All @@ -47,7 +43,7 @@ def latlng(arg):
return "%f,%f" % (arg[0], arg[1])

raise TypeError(
"Expected a string or lat/lng dict, "
"Expected a lat/lng dict or tuple, "
"but got %s" % type(arg).__name__)


Expand All @@ -56,7 +52,7 @@ def join_list(sep, arg):
:param sep: Separator string.
:type sep: basestring
:param arg: Value to coerce into a list.
:type arg: basestring or list
:type arg: basestring or list of basestring
:rtype: basestring
"""
return sep.join(as_list(arg))
Expand Down Expand Up @@ -120,12 +116,9 @@ def components(arg):
# 'country:US|postal_code:94043'

:param arg: The component filter.
:type arg: dict or basestring
:type arg: dict
:rtype basestring:
"""
if isinstance(arg, basestring):
return arg

if isinstance(arg, dict):
arg = ["%s:%s" % (k, arg[k]) for k in arg]
return "|".join(arg)
Expand Down Expand Up @@ -160,12 +153,9 @@ def bounds(arg):
# '-34.169249,150.502229|-33.424598,151.342636'

:param arg: The bounds.
:type arg: basestring or dict
:type arg: dict
"""

if isinstance(arg, basestring):
return arg

if isinstance(arg, dict):
if "southwest" in arg and "northeast" in arg:
return "%s|%s" % (latlng(arg["southwest"]),
Expand All @@ -174,4 +164,3 @@ def bounds(arg):
raise TypeError(
"Expected a string or bounds (southwest/northeast) dict, "
"but got %s" % type(arg).__name__)

10 changes: 8 additions & 2 deletions googlemaps/directions.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,15 @@ def directions(ctx, origin, destination,
"""
# TODO(mdr-eng): Add optimize_waypoints=True.

if not isinstance(origin, basestring):
origin = convert.latlng(origin)

if not isinstance(destination, basestring):
origin = convert.latlng(destination)

params = {
"origin": convert.latlng(origin),
"destination": convert.latlng(destination)
"origin": origin,
"destination": destination
}

if mode:
Expand Down
2 changes: 1 addition & 1 deletion samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def main():
print googlemaps.reverse_geocode(c, geocoded[0]["geometry"]["location"])

print googlemaps.reverse_geocode(c,
latlng="-33.86536501970851,151.1969187802915",
latlng=(-33.86536501970851,151.1969187802915),
result_type=["country", "political"])

print googlemaps.geocode(c, components={"country": "US"})
Expand Down
Empty file added test/__init__.py
Empty file.
76 changes: 76 additions & 0 deletions test/test_convert.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
"""Tests for the convert module."""

import unittest
import datetime

from googlemaps import convert

class ConvertLatLngTest(unittest.TestCase):

def test_latlng(self):
ll = {"lat": 1, "lng": 2}
self.assertEqual("1.000000,2.000000", convert.latlng(ll))

ll = [1, 2]
self.assertEqual("1.000000,2.000000", convert.latlng(ll))

ll = (1, 2)
self.assertEqual("1.000000,2.000000", convert.latlng(ll))

with self.assertRaises(TypeError):
convert.latlng(1)

with self.assertRaises(TypeError):
convert.latlng("test")


def test_join_list(self):
self.assertEquals("asdf", convert.join_list("|", "asdf"))

self.assertEquals("1,2,A", convert.join_list(",", ["1", "2", "A"]))

self.assertEquals("", convert.join_list(",", []))

self.assertEquals("a,B", convert.join_list(",", ("a", "B")))

def test_as_list(self):
self.assertEquals([1], convert.as_list(1))

self.assertEquals([1, 2, 3], convert.as_list([1, 2, 3]))

self.assertEquals(["string"], convert.as_list("string"))

self.assertEquals((1, 2), convert.as_list((1, 2)))

def test_time(self):
self.assertEquals("1409810596", convert.time(1409810596))

dt = datetime.datetime.fromtimestamp(1409810596)
self.assertEquals("1409810596", convert.time(dt))

def test_components(self):
c = {"country": "US"}
self.assertEquals("country:US", convert.components(c))

c = {"country": "US", "foo": 1}
self.assertEquals("country:US|foo:1", convert.components(c))

with self.assertRaises(TypeError):
convert.components("test")

with self.assertRaises(TypeError):
convert.components(1)

with self.assertRaises(TypeError):
convert.components(("c", "b"))


def test_bounds(self):
ne = {"lat": 1, "lng": 2}
sw = (3, 4)
b = {"northeast": ne, "southwest": sw}
self.assertEquals("3.000000,4.000000|1.000000,2.000000",
convert.bounds(b))

with self.assertRaises(TypeError):
convert.bounds("test")