Skip to content

Commit f7161ab

Browse files
Pub/Sub: moved import statements inside region tags (#1753)
* Moved import stataments inside region tags * Explained topic and subscription path methods
1 parent 1154b3b commit f7161ab

File tree

2 files changed

+207
-104
lines changed

2 files changed

+207
-104
lines changed

pubsub/cloud-client/publisher.py

Lines changed: 74 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,70 +22,95 @@
2222
"""
2323

2424
import argparse
25-
import time
2625

27-
from google.cloud import pubsub_v1
2826

29-
30-
def list_topics(project):
27+
def list_topics(project_id):
3128
"""Lists all Pub/Sub topics in the given project."""
3229
# [START pubsub_list_topics]
30+
from google.cloud import pubsub_v1
31+
32+
# TODO project_id = "Your Google Cloud Project ID"
33+
3334
publisher = pubsub_v1.PublisherClient()
34-
project_path = publisher.project_path(project)
35+
project_path = publisher.project_path(project_id)
3536

3637
for topic in publisher.list_topics(project_path):
3738
print(topic)
3839
# [END pubsub_list_topics]
3940

4041

41-
def create_topic(project, topic_name):
42+
def create_topic(project_id, topic_name):
4243
"""Create a new Pub/Sub topic."""
4344
# [START pubsub_create_topic]
45+
from google.cloud import pubsub_v1
46+
47+
# TODO project_id = "Your Google Cloud Project ID"
48+
# TODO topic_name = "Your Pub/Sub topic name"
49+
4450
publisher = pubsub_v1.PublisherClient()
45-
topic_path = publisher.topic_path(project, topic_name)
51+
topic_path = publisher.topic_path(project_id, topic_name)
4652

4753
topic = publisher.create_topic(topic_path)
4854

4955
print('Topic created: {}'.format(topic))
5056
# [END pubsub_create_topic]
5157

5258

53-
def delete_topic(project, topic_name):
59+
def delete_topic(project_id, topic_name):
5460
"""Deletes an existing Pub/Sub topic."""
5561
# [START pubsub_delete_topic]
62+
from google.cloud import pubsub_v1
63+
64+
# TODO project_id = "Your Google Cloud Project ID"
65+
# TODO topic_name = "Your Pub/Sub topic name"
66+
5667
publisher = pubsub_v1.PublisherClient()
57-
topic_path = publisher.topic_path(project, topic_name)
68+
topic_path = publisher.topic_path(project_id, topic_name)
5869

5970
publisher.delete_topic(topic_path)
6071

6172
print('Topic deleted: {}'.format(topic_path))
6273
# [END pubsub_delete_topic]
6374

6475

65-
def publish_messages(project, topic_name):
76+
def publish_messages(project_id, topic_name):
6677
"""Publishes multiple messages to a Pub/Sub topic."""
6778
# [START pubsub_quickstart_publisher]
6879
# [START pubsub_publish]
80+
from google.cloud import pubsub_v1
81+
82+
# TODO project_id = "Your Google Cloud Project ID"
83+
# TODO topic_name = "Your Pub/Sub topic name"
84+
6985
publisher = pubsub_v1.PublisherClient()
70-
topic_path = publisher.topic_path(project, topic_name)
86+
# The `topic_path` method creates a fully qualified identifier
87+
# in the form `projects/{project_id}/topics/{topic_name}`
88+
topic_path = publisher.topic_path(project_id, topic_name)
7189

7290
for n in range(1, 10):
7391
data = u'Message number {}'.format(n)
7492
# Data must be a bytestring
7593
data = data.encode('utf-8')
76-
publisher.publish(topic_path, data=data)
94+
# When you publish a message, the client returns a future.
95+
future = publisher.publish(topic_path, data=data)
96+
print('Published {} of message ID {}.'.format(data, future.result()))
7797

7898
print('Published messages.')
7999
# [END pubsub_quickstart_publisher]
80100
# [END pubsub_publish]
81101

82102

83-
def publish_messages_with_custom_attributes(project, topic_name):
103+
def publish_messages_with_custom_attributes(project_id, topic_name):
84104
"""Publishes multiple messages with custom attributes
85105
to a Pub/Sub topic."""
86106
# [START pubsub_publish_custom_attributes]
107+
from google.cloud import pubsub_v1
108+
109+
# TODO project_id = "Your Google Cloud Project ID"
110+
# TODO topic_name = "Your Pub/Sub topic name"
111+
87112
publisher = pubsub_v1.PublisherClient()
88-
topic_path = publisher.topic_path(project, topic_name)
113+
topic_path = publisher.topic_path(project_id, topic_name)
89114

90115
for n in range(1, 10):
91116
data = u'Message number {}'.format(n)
@@ -99,12 +124,17 @@ def publish_messages_with_custom_attributes(project, topic_name):
99124
# [END pubsub_publish_custom_attributes]
100125

101126

102-
def publish_messages_with_futures(project, topic_name):
127+
def publish_messages_with_futures(project_id, topic_name):
103128
"""Publishes multiple messages to a Pub/Sub topic and prints their
104129
message IDs."""
105130
# [START pubsub_publisher_concurrency_control]
131+
from google.cloud import pubsub_v1
132+
133+
# TODO project_id = "Your Google Cloud Project ID"
134+
# TODO topic_name = "Your Pub/Sub topic name"
135+
106136
publisher = pubsub_v1.PublisherClient()
107-
topic_path = publisher.topic_path(project, topic_name)
137+
topic_path = publisher.topic_path(project_id, topic_name)
108138

109139
# When you publish a message, the client returns a Future. This Future
110140
# can be used to track when the message is published.
@@ -124,11 +154,18 @@ def publish_messages_with_futures(project, topic_name):
124154
# [END pubsub_publisher_concurrency_control]
125155

126156

127-
def publish_messages_with_error_handler(project, topic_name):
157+
def publish_messages_with_error_handler(project_id, topic_name):
128158
"""Publishes multiple messages to a Pub/Sub topic with an error handler."""
129159
# [START pubsub_publish_messages_error_handler]
160+
import time
161+
162+
from google.cloud import pubsub_v1
163+
164+
# TODO project_id = "Your Google Cloud Project ID"
165+
# TODO topic_name = "Your Pub/Sub topic name"
166+
130167
publisher = pubsub_v1.PublisherClient()
131-
topic_path = publisher.topic_path(project, topic_name)
168+
topic_path = publisher.topic_path(project_id, topic_name)
132169

133170
def callback(message_future):
134171
# When timeout is unspecified, the exception method waits indefinitely.
@@ -155,17 +192,22 @@ def callback(message_future):
155192
# [END pubsub_publish_messages_error_handler]
156193

157194

158-
def publish_messages_with_batch_settings(project, topic_name):
195+
def publish_messages_with_batch_settings(project_id, topic_name):
159196
"""Publishes multiple messages to a Pub/Sub topic with batch settings."""
160197
# [START pubsub_publisher_batch_settings]
161-
# Configure the batch to publish once there is one kilobyte of data or
162-
# 1 second has passed.
198+
from google.cloud import pubsub_v1
199+
200+
# TODO project_id = "Your Google Cloud Project ID"
201+
# TODO topic_name = "Your Pub/Sub topic name"
202+
203+
# Configure the batch to publish as soon as there is one kilobyte
204+
# of data or one second has passed.
163205
batch_settings = pubsub_v1.types.BatchSettings(
164206
max_bytes=1024, # One kilobyte
165207
max_latency=1, # One second
166208
)
167209
publisher = pubsub_v1.PublisherClient(batch_settings)
168-
topic_path = publisher.topic_path(project, topic_name)
210+
topic_path = publisher.topic_path(project_id, topic_name)
169211

170212
for n in range(1, 10):
171213
data = u'Message number {}'.format(n)
@@ -182,7 +224,7 @@ def publish_messages_with_batch_settings(project, topic_name):
182224
description=__doc__,
183225
formatter_class=argparse.RawDescriptionHelpFormatter
184226
)
185-
parser.add_argument('project', help='Your Google Cloud project ID')
227+
parser.add_argument('project_id', help='Your Google Cloud project ID')
186228

187229
subparsers = parser.add_subparsers(dest='command')
188230
subparsers.add_parser('list', help=list_topics.__doc__)
@@ -220,18 +262,19 @@ def publish_messages_with_batch_settings(project, topic_name):
220262
args = parser.parse_args()
221263

222264
if args.command == 'list':
223-
list_topics(args.project)
265+
list_topics(args.project_id)
224266
elif args.command == 'create':
225-
create_topic(args.project, args.topic_name)
267+
create_topic(args.project_id, args.topic_name)
226268
elif args.command == 'delete':
227-
delete_topic(args.project, args.topic_name)
269+
delete_topic(args.project_id, args.topic_name)
228270
elif args.command == 'publish':
229-
publish_messages(args.project, args.topic_name)
271+
publish_messages(args.project_id, args.topic_name)
230272
elif args.command == 'publish-with-custom-attributes':
231-
publish_messages_with_custom_attributes(args.project, args.topic_name)
273+
publish_messages_with_custom_attributes(
274+
args.project_id, args.topic_name)
232275
elif args.command == 'publish-with-futures':
233-
publish_messages_with_futures(args.project, args.topic_name)
276+
publish_messages_with_futures(args.project_id, args.topic_name)
234277
elif args.command == 'publish-with-error-handler':
235-
publish_messages_with_error_handler(args.project, args.topic_name)
278+
publish_messages_with_error_handler(args.project_id, args.topic_name)
236279
elif args.command == 'publish-with-batch-settings':
237-
publish_messages_with_batch_settings(args.project, args.topic_name)
280+
publish_messages_with_batch_settings(args.project_id, args.topic_name)

0 commit comments

Comments
 (0)