aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Masood Malekghassemi <soltanmm@users.noreply.github.com>2015-02-26 11:13:24 -0800
committerGravatar Masood Malekghassemi <soltanmm@users.noreply.github.com>2015-02-26 11:13:24 -0800
commite313fe06fd3ae265fbb99c7ce3aa0860896bf0c2 (patch)
treeed88944fb7c1bb5c5f00cead791d7163f468177b
parent6f062bbe0375bd26f085f3769c7941c56fd5c9dd (diff)
parent94c9ed1804ebad9b1045e125c80be775ae5e4f4e (diff)
Merge pull request #119 from nathanielmanistaatgoogle/py-route-guide-cleanups
Python Route Guide clean-ups.
-rwxr-xr-xpython/route_guide/route_guide_client.py32
-rwxr-xr-xpython/route_guide/route_guide_resources.py53
-rw-r--r--python/route_guide/route_guide_server.py33
3 files changed, 75 insertions, 43 deletions
diff --git a/python/route_guide/route_guide_client.py b/python/route_guide/route_guide_client.py
index cdb453bb0f..2ba6f05ba5 100755
--- a/python/route_guide/route_guide_client.py
+++ b/python/route_guide/route_guide_client.py
@@ -29,25 +29,13 @@
"""The Python implementation of the gRPC route guide client."""
-import route_guide_pb2
-
-import json
import random
import time
-_TIMEOUT_SECONDS = 30
-
+import route_guide_pb2
+import route_guide_resources
-def read_route_guide_db():
- """Reads the route guide"""
- feature_list = []
- with open("route_guide_db.json") as route_guide_db_file:
- for item in json.load(route_guide_db_file):
- feature = route_guide_pb2.Feature(name=item["name"])
- feature.location.longitude = item["location"]["longitude"]
- feature.location.latitude = item["location"]["latitude"]
- feature_list.append(feature)
- return feature_list
+_TIMEOUT_SECONDS = 30
def make_route_note(message, latitude, longitude):
@@ -89,7 +77,7 @@ def guide_list_features(stub):
def generate_route(feature_list):
- for index in range(0, 10):
+ for _ in range(0, 10):
random_feature = feature_list[random.randint(0, len(feature_list) - 1)]
print "Visiting point %s" % random_feature.location
yield random_feature.location
@@ -97,7 +85,7 @@ def generate_route(feature_list):
def guide_record_route(stub):
- feature_list = read_route_guide_db()
+ feature_list = route_guide_resources.read_route_guide_database()
route_iter = generate_route(feature_list)
route_summary = stub.RecordRoute(route_iter, _TIMEOUT_SECONDS)
@@ -109,11 +97,11 @@ def guide_record_route(stub):
def generate_messages():
messages = [
- make_route_note("First message", 0, 0),
- make_route_note("Second message", 0, 1),
- make_route_note("Third message", 1, 0),
- make_route_note("Fourth message", 0, 0),
- make_route_note("Fifth message", 1, 0),
+ make_route_note("First message", 0, 0),
+ make_route_note("Second message", 0, 1),
+ make_route_note("Third message", 1, 0),
+ make_route_note("Fourth message", 0, 0),
+ make_route_note("Fifth message", 1, 0),
]
for msg in messages:
print "Sending %s at %s" % (msg.message, msg.location)
diff --git a/python/route_guide/route_guide_resources.py b/python/route_guide/route_guide_resources.py
new file mode 100755
index 0000000000..30c7711019
--- /dev/null
+++ b/python/route_guide/route_guide_resources.py
@@ -0,0 +1,53 @@
+# Copyright 2015, Google Inc.
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+#
+# * Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+# * Redistributions in binary form must reproduce the above
+# copyright notice, this list of conditions and the following disclaimer
+# in the documentation and/or other materials provided with the
+# distribution.
+# * Neither the name of Google Inc. nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+"""Common resources used in the gRPC route guide example."""
+
+import json
+
+import route_guide_pb2
+
+
+def read_route_guide_database():
+ """Reads the route guide database.
+
+ Returns:
+ The full contents of the route guide database as a sequence of
+ route_guide_pb2.Features.
+ """
+ feature_list = []
+ with open("route_guide_db.json") as route_guide_db_file:
+ for item in json.load(route_guide_db_file):
+ feature = route_guide_pb2.Feature(
+ name=item["name"],
+ location=route_guide_pb2.Point(
+ latitude=item["location"]["latitude"],
+ longitude=item["location"]["longitude"]))
+ feature_list.append(feature)
+ return feature_list
diff --git a/python/route_guide/route_guide_server.py b/python/route_guide/route_guide_server.py
index 51cbd0dc84..eff167ec3d 100644
--- a/python/route_guide/route_guide_server.py
+++ b/python/route_guide/route_guide_server.py
@@ -29,29 +29,17 @@
"""The Python implementation of the gRPC route guide server."""
-import json
import time
import math
import route_guide_pb2
+import route_guide_resources
_ONE_DAY_IN_SECONDS = 60 * 60 * 24
-def read_route_guide_db():
- """Reads the route guide"""
- feature_list = []
- with open("route_guide_db.json") as route_guide_db_file:
- for item in json.load(route_guide_db_file):
- feature = route_guide_pb2.Feature(name=item["name"])
- feature.location.longitude = item["location"]["longitude"]
- feature.location.latitude = item["location"]["latitude"]
- feature_list.append(feature)
- return feature_list
-
-
def get_feature(feature_db, point):
- """Returns feature at given location or None"""
+ """Returns Feature at given location or None."""
for feature in feature_db:
if feature.location == point:
return feature
@@ -59,7 +47,7 @@ def get_feature(feature_db, point):
def get_distance(start, end):
- """Distance between two points"""
+ """Distance between two points."""
coord_factor = 10000000.0
lat_1 = start.latitude / coord_factor
lat_2 = end.latitude / coord_factor
@@ -70,8 +58,10 @@ def get_distance(start, end):
delta_lat_rad = math.radians(lat_2 - lat_1)
delta_lon_rad = math.radians(lon_2 - lon_1)
- a = pow(math.sin(delta_lat_rad / 2), 2) + math.cos(lat_rad_1) * math.cos(lat_rad_2) * pow(math.sin(delta_lon_rad / 2), 2)
- c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
+ a = (pow(math.sin(delta_lat_rad / 2), 2) +
+ (math.cos(lat_rad_1) * math.cos(lat_rad_2) *
+ pow(math.sin(delta_lon_rad / 2), 2)))
+ c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
R = 6371000; # metres
return R * c;
@@ -79,14 +69,15 @@ class RouteGuideServicer(route_guide_pb2.EarlyAdopterRouteGuideServicer):
"""Provides methods that implement functionality of route guide server."""
def __init__(self):
- self.db = read_route_guide_db()
+ self.db = route_guide_resources.read_route_guide_database()
def GetFeature(self, request, context):
feature = get_feature(self.db, request)
if not feature:
- feature = route_guide_pb2.Feature(name="")
- feature.location.longitude = request.longitude
- feature.location.latitude = request.latitude
+ feature = route_guide_pb2.Feature(
+ name="",
+ location=route_guide_pb2.Point(
+ latitude=request.latitude, longitude=request.longitude))
return feature
def ListFeatures(self, request, context):