Skip to content

Commit

Permalink
Merge pull request grpc#5659 from leifurhauks/py3_examples
Browse files Browse the repository at this point in the history
python3-compatible syntax for python examples
  • Loading branch information
jtattermusch committed Mar 21, 2016
2 parents c6b1a71 + 4df3987 commit bbb1d10
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
6 changes: 4 additions & 2 deletions examples/python/helloworld/greeter_client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2015, Google Inc.
# Copyright 2015-2016, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -29,6 +29,8 @@

"""The Python implementation of the GRPC helloworld.Greeter client."""

from __future__ import print_function

from grpc.beta import implementations

import helloworld_pb2
Expand All @@ -40,7 +42,7 @@ def run():
channel = implementations.insecure_channel('localhost', 50051)
stub = helloworld_pb2.beta_create_Greeter_stub(channel)
response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'), _TIMEOUT_SECONDS)
print "Greeter client received: " + response.message
print("Greeter client received: " + response.message)


if __name__ == '__main__':
Expand Down
36 changes: 19 additions & 17 deletions examples/python/route_guide/route_guide_client.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2015, Google Inc.
# Copyright 2015-2016, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -29,6 +29,8 @@

"""The Python implementation of the gRPC route guide client."""

from __future__ import print_function

import random
import time

Expand All @@ -49,13 +51,13 @@ def make_route_note(message, latitude, longitude):
def guide_get_one_feature(stub, point):
feature = stub.GetFeature(point, _TIMEOUT_SECONDS)
if not feature.location:
print "Server returned incomplete feature"
print("Server returned incomplete feature")
return

if feature.name:
print "Feature called %s at %s" % (feature.name, feature.location)
print("Feature called %s at %s" % (feature.name, feature.location))
else:
print "Found no feature at %s" % feature.location
print("Found no feature at %s" % feature.location)


def guide_get_feature(stub):
Expand All @@ -69,18 +71,18 @@ def guide_list_features(stub):
latitude=400000000, longitude = -750000000),
hi=route_guide_pb2.Point(
latitude = 420000000, longitude = -730000000))
print "Looking for features between 40, -75 and 42, -73"
print("Looking for features between 40, -75 and 42, -73")

features = stub.ListFeatures(rect, _TIMEOUT_SECONDS)

for feature in features:
print "Feature called %s at %s" % (feature.name, feature.location)
print("Feature called %s at %s" % (feature.name, feature.location))


def generate_route(feature_list):
for _ in range(0, 10):
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
time.sleep(random.uniform(0.5, 1.5))

Expand All @@ -90,10 +92,10 @@ def guide_record_route(stub):

route_iter = generate_route(feature_list)
route_summary = stub.RecordRoute(route_iter, _TIMEOUT_SECONDS)
print "Finished trip with %s points " % route_summary.point_count
print "Passed %s features " % route_summary.feature_count
print "Travelled %s meters " % route_summary.distance
print "It took %s seconds " % route_summary.elapsed_time
print("Finished trip with %s points " % route_summary.point_count)
print("Passed %s features " % route_summary.feature_count)
print("Travelled %s meters " % route_summary.distance)
print("It took %s seconds " % route_summary.elapsed_time)


def generate_messages():
Expand All @@ -105,27 +107,27 @@ def generate_messages():
make_route_note("Fifth message", 1, 0),
]
for msg in messages:
print "Sending %s at %s" % (msg.message, msg.location)
print("Sending %s at %s" % (msg.message, msg.location))
yield msg
time.sleep(random.uniform(0.5, 1.0))


def guide_route_chat(stub):
responses = stub.RouteChat(generate_messages(), _TIMEOUT_SECONDS)
for response in responses:
print "Received message %s at %s" % (response.message, response.location)
print("Received message %s at %s" % (response.message, response.location))


def run():
channel = implementations.insecure_channel('localhost', 50051)
stub = route_guide_pb2.beta_create_RouteGuide_stub(channel)
print "-------------- GetFeature --------------"
print("-------------- GetFeature --------------")
guide_get_feature(stub)
print "-------------- ListFeatures --------------"
print("-------------- ListFeatures --------------")
guide_list_features(stub)
print "-------------- RecordRoute --------------"
print("-------------- RecordRoute --------------")
guide_record_route(stub)
print "-------------- RouteChat --------------"
print("-------------- RouteChat --------------")
guide_route_chat(stub)


Expand Down

0 comments on commit bbb1d10

Please sign in to comment.