Skip to content

Commit

Permalink
Format python snippets (#1187)
Browse files Browse the repository at this point in the history
  • Loading branch information
rinarakaki committed Sep 6, 2023
1 parent 8858fcb commit e76a103
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 50 deletions.
95 changes: 49 additions & 46 deletions content/en/docs/languages/python/basics.md
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
8 changes: 4 additions & 4 deletions content/en/docs/languages/python/quickstart.md
Expand Up @@ -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}!")
...
```

Expand Down

0 comments on commit e76a103

Please sign in to comment.