diff --git a/content/en/docs/languages/python/basics.md b/content/en/docs/languages/python/basics.md index 8759456903..6f747b4655 100644 --- a/content/en/docs/languages/python/basics.md +++ b/content/en/docs/languages/python/basics.md @@ -202,11 +202,11 @@ database in a `Feature`. ```python def GetFeature(self, request, context): - feature = get_feature(self.db, request) - if feature is None: - return route_guide_pb2.Feature(name="", location=request) - else: - return feature + feature = get_feature(self.db, request) + if feature is None: + return route_guide_pb2.Feature(name="", location=request) + else: + return feature ``` The method is passed a `route_guide_pb2.Point` request for the RPC, and a @@ -220,16 +220,18 @@ that sends multiple `Feature`s to the client. ```python def ListFeatures(self, request, context): - left = min(request.lo.longitude, request.hi.longitude) - right = max(request.lo.longitude, request.hi.longitude) - top = max(request.lo.latitude, request.hi.latitude) - bottom = min(request.lo.latitude, request.hi.latitude) - for feature in self.db: - if (feature.location.longitude >= left and - feature.location.longitude <= right and - feature.location.latitude >= bottom and - feature.location.latitude <= top): - yield feature + left = min(request.lo.longitude, request.hi.longitude) + right = max(request.lo.longitude, request.hi.longitude) + top = max(request.lo.latitude, request.hi.latitude) + bottom = min(request.lo.latitude, request.hi.latitude) + for feature in self.db: + if ( + feature.location.longitude >= left + and feature.location.longitude <= right + and feature.location.latitude >= bottom + and feature.location.latitude <= top + ): + yield feature ``` Here the request message is a `route_guide_pb2.Rectangle` within which the @@ -244,25 +246,27 @@ request values and returns a single response value. ```python def RecordRoute(self, request_iterator, context): - point_count = 0 - feature_count = 0 - distance = 0.0 - prev_point = None - - start_time = time.time() - for point in request_iterator: - point_count += 1 - if get_feature(self.db, point): - feature_count += 1 - if prev_point: - distance += get_distance(prev_point, point) - prev_point = point - - elapsed_time = time.time() - start_time - return route_guide_pb2.RouteSummary(point_count=point_count, - feature_count=feature_count, - distance=int(distance), - elapsed_time=int(elapsed_time)) + point_count = 0 + feature_count = 0 + distance = 0.0 + prev_point = None + + start_time = time.time() + for point in request_iterator: + point_count += 1 + if get_feature(self.db, point): + feature_count += 1 + if prev_point: + distance += get_distance(prev_point, point) + prev_point = point + + elapsed_time = time.time() - start_time + return route_guide_pb2.RouteSummary( + point_count=point_count, + feature_count=feature_count, + distance=int(distance), + elapsed_time=int(elapsed_time), + ) ``` ##### Bidirectional streaming RPC @@ -271,12 +275,12 @@ Lastly let's look at the bidirectionally-streaming method `RouteChat`. ```python def RouteChat(self, request_iterator, context): - prev_notes = [] - for new_note in request_iterator: - for prev_note in prev_notes: - if prev_note.location == new_note.location: - yield prev_note - prev_notes.append(new_note) + prev_notes = [] + for new_note in request_iterator: + for prev_note in prev_notes: + if prev_note.location == new_note.location: + yield prev_note + prev_notes.append(new_note) ``` This method's semantics are a combination of those of the request-streaming @@ -290,12 +294,11 @@ start up a gRPC server so that clients can actually use your service: ```python def serve(): - server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) - route_guide_pb2_grpc.add_RouteGuideServicer_to_server( - RouteGuideServicer(), server) - server.add_insecure_port('[::]:50051') - server.start() - server.wait_for_termination() + server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) + route_guide_pb2_grpc.add_RouteGuideServicer_to_server(RouteGuideServicer(), server) + server.add_insecure_port("[::]:50051") + server.start() + server.wait_for_termination() ``` The server `start()` method is non-blocking. A new thread will be instantiated diff --git a/content/en/docs/languages/python/quickstart.md b/content/en/docs/languages/python/quickstart.md index 1edf94117b..90cc355c62 100644 --- a/content/en/docs/languages/python/quickstart.md +++ b/content/en/docs/languages/python/quickstart.md @@ -171,11 +171,11 @@ this: ```py class Greeter(helloworld_pb2_grpc.GreeterServicer): - def SayHello(self, request, context): - return helloworld_pb2.HelloReply(message=f'Hello, {request.name}!') + def SayHello(self, request, context): + return helloworld_pb2.HelloReply(message=f"Hello, {request.name}!") - def SayHelloAgain(self, request, context): - return helloworld_pb2.HelloReply(message=f'Hello again, {request.name}!') + def SayHelloAgain(self, request, context): + return helloworld_pb2.HelloReply(message=f"Hello again, {request.name}!") ... ```