aboutsummaryrefslogtreecommitdiffhomepage
path: root/examples/python
diff options
context:
space:
mode:
authorGravatar Leifur Halldor Asgeirsson <lasgeirsson@zerofail.com>2016-03-03 10:37:31 -0500
committerGravatar Leifur Halldor Asgeirsson <lasgeirsson@zerofail.com>2016-03-18 16:24:29 -0400
commit4df39871d0d9ee7f14fe9d810d217cbdd6f621e5 (patch)
treeeb516d64858646be3c7ad71a1507aa2bca498149 /examples/python
parent79787471985c8b1403b82a3f3df225496c0fd7e1 (diff)
python3-compatible syntax for python examples
Diffstat (limited to 'examples/python')
-rw-r--r--examples/python/helloworld/greeter_client.py6
-rw-r--r--examples/python/route_guide/route_guide_client.py36
2 files changed, 23 insertions, 19 deletions
diff --git a/examples/python/helloworld/greeter_client.py b/examples/python/helloworld/greeter_client.py
index 561b25bcb2..9c18b41d25 100644
--- a/examples/python/helloworld/greeter_client.py
+++ b/examples/python/helloworld/greeter_client.py
@@ -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
@@ -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
@@ -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__':
diff --git a/examples/python/route_guide/route_guide_client.py b/examples/python/route_guide/route_guide_client.py
index b1dfad551d..9d6f865a33 100644
--- a/examples/python/route_guide/route_guide_client.py
+++ b/examples/python/route_guide/route_guide_client.py
@@ -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
@@ -29,6 +29,8 @@
"""The Python implementation of the gRPC route guide client."""
+from __future__ import print_function
+
import random
import time
@@ -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):
@@ -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))
@@ -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():
@@ -105,7 +107,7 @@ 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))
@@ -113,19 +115,19 @@ def generate_messages():
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)