Merge pull request #119 from nathanielmanistaatgoogle/py-route-guide-cleanups

Python Route Guide clean-ups.
pull/3109/head
Masood Malekghassemi 10 years ago
commit e313fe06fd
  1. 32
      python/route_guide/route_guide_client.py
  2. 53
      python/route_guide/route_guide_resources.py
  3. 33
      python/route_guide/route_guide_server.py

@ -29,25 +29,13 @@
"""The Python implementation of the gRPC route guide client.""" """The Python implementation of the gRPC route guide client."""
import route_guide_pb2
import json
import random import random
import time import time
_TIMEOUT_SECONDS = 30 import route_guide_pb2
import route_guide_resources
def read_route_guide_db(): _TIMEOUT_SECONDS = 30
"""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 make_route_note(message, latitude, longitude): def make_route_note(message, latitude, longitude):
@ -89,7 +77,7 @@ def guide_list_features(stub):
def generate_route(feature_list): 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)] random_feature = feature_list[random.randint(0, len(feature_list) - 1)]
print "Visiting point %s" % random_feature.location print "Visiting point %s" % random_feature.location
yield random_feature.location yield random_feature.location
@ -97,7 +85,7 @@ def generate_route(feature_list):
def guide_record_route(stub): 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_iter = generate_route(feature_list)
route_summary = stub.RecordRoute(route_iter, _TIMEOUT_SECONDS) route_summary = stub.RecordRoute(route_iter, _TIMEOUT_SECONDS)
@ -109,11 +97,11 @@ def guide_record_route(stub):
def generate_messages(): def generate_messages():
messages = [ messages = [
make_route_note("First message", 0, 0), make_route_note("First message", 0, 0),
make_route_note("Second message", 0, 1), make_route_note("Second message", 0, 1),
make_route_note("Third message", 1, 0), make_route_note("Third message", 1, 0),
make_route_note("Fourth message", 0, 0), make_route_note("Fourth message", 0, 0),
make_route_note("Fifth message", 1, 0), make_route_note("Fifth message", 1, 0),
] ]
for msg in messages: for msg in messages:
print "Sending %s at %s" % (msg.message, msg.location) print "Sending %s at %s" % (msg.message, msg.location)

@ -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

@ -29,29 +29,17 @@
"""The Python implementation of the gRPC route guide server.""" """The Python implementation of the gRPC route guide server."""
import json
import time import time
import math import math
import route_guide_pb2 import route_guide_pb2
import route_guide_resources
_ONE_DAY_IN_SECONDS = 60 * 60 * 24 _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): 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: for feature in feature_db:
if feature.location == point: if feature.location == point:
return feature return feature
@ -59,7 +47,7 @@ def get_feature(feature_db, point):
def get_distance(start, end): def get_distance(start, end):
"""Distance between two points""" """Distance between two points."""
coord_factor = 10000000.0 coord_factor = 10000000.0
lat_1 = start.latitude / coord_factor lat_1 = start.latitude / coord_factor
lat_2 = end.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_lat_rad = math.radians(lat_2 - lat_1)
delta_lon_rad = math.radians(lon_2 - lon_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) a = (pow(math.sin(delta_lat_rad / 2), 2) +
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a)) (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 R = 6371000; # metres
return R * c; return R * c;
@ -79,14 +69,15 @@ class RouteGuideServicer(route_guide_pb2.EarlyAdopterRouteGuideServicer):
"""Provides methods that implement functionality of route guide server.""" """Provides methods that implement functionality of route guide server."""
def __init__(self): def __init__(self):
self.db = read_route_guide_db() self.db = route_guide_resources.read_route_guide_database()
def GetFeature(self, request, context): def GetFeature(self, request, context):
feature = get_feature(self.db, request) feature = get_feature(self.db, request)
if not feature: if not feature:
feature = route_guide_pb2.Feature(name="") feature = route_guide_pb2.Feature(
feature.location.longitude = request.longitude name="",
feature.location.latitude = request.latitude location=route_guide_pb2.Point(
latitude=request.latitude, longitude=request.longitude))
return feature return feature
def ListFeatures(self, request, context): def ListFeatures(self, request, context):

Loading…
Cancel
Save