Skip to content

Commit cc90079

Browse files
authored
feat: upgrade sample style (TORuS) (GoogleCloudPlatform#9938)
* feat: upgrade sample style (TORuS) * linting * linting
1 parent 10cddb1 commit cc90079

File tree

31 files changed

+70
-80
lines changed

31 files changed

+70
-80
lines changed

containeranalysis/snippets/samples.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ def get_discovery_info(resource_url, project_id):
159159

160160
from google.cloud.devtools import containeranalysis_v1
161161

162-
filter_str = 'kind="DISCOVERY" AND resourceUrl="{}"'.format(resource_url)
162+
filter_str = f'kind="DISCOVERY" AND resourceUrl="{resource_url}"'
163163
client = containeranalysis_v1.ContainerAnalysisClient()
164164
grafeas_client = client.get_grafeas_client()
165165
project_name = f"projects/{project_id}"
@@ -202,7 +202,7 @@ def get_occurrences_for_image(resource_url, project_id):
202202

203203
from google.cloud.devtools import containeranalysis_v1
204204

205-
filter_str = 'resourceUrl="{}"'.format(resource_url)
205+
filter_str = f'resourceUrl="{resource_url}"'
206206
client = containeranalysis_v1.ContainerAnalysisClient()
207207
grafeas_client = client.get_grafeas_client()
208208
project_name = f"projects/{project_id}"
@@ -250,7 +250,7 @@ def __init__(self):
250250
def pubsub_callback(self, message):
251251
# every time a pubsub message comes in, print it and count it
252252
self.msg_count += 1
253-
print('Message {}: {}'.format(self.msg_count, message.data))
253+
print(f'Message {self.msg_count}: {message.data}')
254254
message.ack()
255255

256256

containeranalysis/snippets/samples_test.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def __init__(self, expected_msg_nums, done_event):
4949
def pubsub_callback(self, message):
5050
# every time a pubsub message comes in, print it and count it
5151
self.msg_count += 1
52-
print('Message {}: {}'.format(self.msg_count, message.data))
52+
print(f'Message {self.msg_count}: {message.data}')
5353
message.ack()
5454
if (self.msg_count == self.expected_msg_nums):
5555
self.done_event.set()
@@ -58,13 +58,13 @@ def pubsub_callback(self, message):
5858
class TestContainerAnalysisSamples:
5959

6060
def setup_method(self, test_method):
61-
print('SETUP {}'.format(test_method.__name__))
62-
self.note_id = 'note-{}'.format(uuid.uuid4())
63-
self.image_url = '{}.{}'.format(uuid.uuid4(), test_method.__name__)
61+
print(f'SETUP {test_method.__name__}')
62+
self.note_id = f'note-{uuid.uuid4()}'
63+
self.image_url = f'{uuid.uuid4()}.{test_method.__name__}'
6464
self.note_obj = samples.create_note(self.note_id, PROJECT_ID)
6565

6666
def teardown_method(self, test_method):
67-
print('TEAR DOWN {}'.format(test_method.__name__))
67+
print(f'TEAR DOWN {test_method.__name__}')
6868
try:
6969
samples.delete_note(self.note_id, PROJECT_ID)
7070
except NotFound:
@@ -158,7 +158,7 @@ def test_pubsub(self):
158158
except AlreadyExists:
159159
pass
160160

161-
subscription_id = 'container-analysis-test-{}'.format(uuid.uuid4())
161+
subscription_id = f'container-analysis-test-{uuid.uuid4()}'
162162
subscription_name = client.subscription_path(PROJECT_ID,
163163
subscription_id)
164164
samples.create_occurrence_subscription(subscription_id, PROJECT_ID)
@@ -181,7 +181,7 @@ def test_pubsub(self):
181181
# to 180 seconds.
182182
# See also: python-docs-samples/issues/2894
183183
job_done.wait(timeout=180)
184-
print('done. msg_count = {}'.format(receiver.msg_count))
184+
print(f'done. msg_count = {receiver.msg_count}')
185185
assert message_count <= receiver.msg_count
186186
finally:
187187
# clean up
@@ -200,7 +200,7 @@ def test_poll_discovery_occurrence_fails(self):
200200
@pytest.mark.flaky(max_runs=3, min_passes=1)
201201
def test_poll_discovery_occurrence(self):
202202
# create discovery occurrence
203-
note_id = 'discovery-note-{}'.format(uuid.uuid4())
203+
note_id = f'discovery-note-{uuid.uuid4()}'
204204
client = containeranalysis_v1.ContainerAnalysisClient()
205205
grafeas_client = client.get_grafeas_client()
206206
note = {
@@ -258,7 +258,7 @@ def test_find_high_severity_vulnerabilities(self):
258258
assert len(occ_list) == 0
259259

260260
# create new high severity vulnerability
261-
note_id = 'discovery-note-{}'.format(uuid.uuid4())
261+
note_id = f'discovery-note-{uuid.uuid4()}'
262262
client = containeranalysis_v1.ContainerAnalysisClient()
263263
grafeas_client = client.get_grafeas_client()
264264
note = {

endpoints/bookstore-grpc-transcoding/bookstore.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,16 @@
1414

1515
import threading
1616

17-
import six
1817

19-
20-
class ShelfInfo(object):
18+
class ShelfInfo:
2119
"""The contents of a single shelf."""
2220
def __init__(self, shelf):
2321
self._shelf = shelf
2422
self._last_book_id = 0
2523
self._books = dict()
2624

2725

28-
class Bookstore(object):
26+
class Bookstore:
2927
"""An in-memory backend for storing Bookstore data."""
3028

3129
def __init__(self):
@@ -35,7 +33,7 @@ def __init__(self):
3533

3634
def list_shelf(self):
3735
with self._lock:
38-
return [s._shelf for (_, s) in six.iteritems(self._shelves)]
36+
return [s._shelf for (_, s) in self._shelves.items()]
3937

4038
def create_shelf(self, shelf):
4139
with self._lock:
@@ -56,7 +54,7 @@ def delete_shelf(self, shelf_id):
5654
def list_books(self, shelf_id):
5755
with self._lock:
5856
return [book for (
59-
_, book) in six.iteritems(self._shelves[shelf_id]._books)]
57+
_, book) in self._shelves[shelf_id]._books.items()]
6058

6159
def create_book(self, shelf_id, book):
6260
with self._lock:

endpoints/bookstore-grpc-transcoding/bookstore_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
def run(host, port, api_key, auth_token, timeout):
2626
"""Makes a basic ListShelves call against a gRPC Bookstore server."""
2727

28-
channel = grpc.insecure_channel('{}:{}'.format(host, port))
28+
channel = grpc.insecure_channel(f'{host}:{port}')
2929

3030
stub = bookstore_pb2_grpc.BookstoreStub(channel)
3131
metadata = []
@@ -34,7 +34,7 @@ def run(host, port, api_key, auth_token, timeout):
3434
if auth_token:
3535
metadata.append(('authorization', 'Bearer ' + auth_token))
3636
shelves = stub.ListShelves(empty_pb2.Empty(), timeout, metadata=metadata)
37-
print('ListShelves: {}'.format(shelves))
37+
print(f'ListShelves: {shelves}')
3838

3939

4040
if __name__ == '__main__':

endpoints/bookstore-grpc-transcoding/bookstore_pb2_grpc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from google.protobuf import empty_pb2 as google_dot_protobuf_dot_empty__pb2
2020

2121

22-
class BookstoreStub(object):
22+
class BookstoreStub:
2323
"""A simple Bookstore API.
2424
2525
The API manages shelves and books resources. Shelves contain books.
@@ -73,7 +73,7 @@ def __init__(self, channel):
7373
)
7474

7575

76-
class BookstoreServicer(object):
76+
class BookstoreServicer:
7777
"""A simple Bookstore API.
7878
7979
The API manages shelves and books resources. Shelves contain books.

endpoints/bookstore-grpc-transcoding/bookstore_server.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def serve(port, shutdown_grace_duration):
106106
store = create_sample_bookstore()
107107
bookstore_pb2_grpc.add_BookstoreServicer_to_server(
108108
BookstoreServicer(store), server)
109-
server.add_insecure_port('[::]:{}'.format(port))
109+
server.add_insecure_port(f'[::]:{port}')
110110
server.start()
111111

112112
try:

endpoints/bookstore-grpc-transcoding/status.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ def context(grpc_context):
2525
except KeyError as key_error:
2626
grpc_context.code(grpc.StatusCode.NOT_FOUND)
2727
grpc_context.details(
28-
'Unable to find the item keyed by {}'.format(key_error))
28+
f'Unable to find the item keyed by {key_error}')

endpoints/bookstore-grpc/bookstore.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,16 @@
1414

1515
import threading
1616

17-
import six
1817

19-
20-
class ShelfInfo(object):
18+
class ShelfInfo:
2119
"""The contents of a single shelf."""
2220
def __init__(self, shelf):
2321
self._shelf = shelf
2422
self._last_book_id = 0
2523
self._books = dict()
2624

2725

28-
class Bookstore(object):
26+
class Bookstore:
2927
"""An in-memory backend for storing Bookstore data."""
3028

3129
def __init__(self):
@@ -35,7 +33,7 @@ def __init__(self):
3533

3634
def list_shelf(self):
3735
with self._lock:
38-
return [s._shelf for (_, s) in six.iteritems(self._shelves)]
36+
return [s._shelf for (_, s) in self._shelves.items()]
3937

4038
def create_shelf(self, shelf):
4139
with self._lock:
@@ -56,7 +54,7 @@ def delete_shelf(self, shelf_id):
5654
def list_books(self, shelf_id):
5755
with self._lock:
5856
return [book for (
59-
_, book) in six.iteritems(self._shelves[shelf_id]._books)]
57+
_, book) in self._shelves[shelf_id]._books.items()]
6058

6159
def create_book(self, shelf_id, book):
6260
with self._lock:

endpoints/bookstore-grpc/bookstore_client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ def run(host, port, api_key, auth_token, timeout, use_tls, servername_override,
3131
if servername_override:
3232
channel_opts += ((
3333
'grpc.ssl_target_name_override', servername_override,),)
34-
channel = grpc.secure_channel('{}:{}'.format(host, port), creds, channel_opts)
34+
channel = grpc.secure_channel(f'{host}:{port}', creds, channel_opts)
3535
else:
36-
channel = grpc.insecure_channel('{}:{}'.format(host, port))
36+
channel = grpc.insecure_channel(f'{host}:{port}')
3737

3838
stub = bookstore_pb2_grpc.BookstoreStub(channel)
3939
metadata = []
@@ -42,7 +42,7 @@ def run(host, port, api_key, auth_token, timeout, use_tls, servername_override,
4242
if auth_token:
4343
metadata.append(('authorization', 'Bearer ' + auth_token))
4444
shelves = stub.ListShelves(empty_pb2.Empty(), timeout, metadata=metadata)
45-
print('ListShelves: {}'.format(shelves))
45+
print(f'ListShelves: {shelves}')
4646

4747

4848
if __name__ == '__main__':

endpoints/bookstore-grpc/bookstore_pb2_grpc.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from google.protobuf import empty_pb2 as google_dot_protobuf_dot_empty__pb2
2020

2121

22-
class BookstoreStub(object):
22+
class BookstoreStub:
2323
"""A simple Bookstore API.
2424
2525
The API manages shelves and books resources. Shelves contain books.
@@ -73,7 +73,7 @@ def __init__(self, channel):
7373
)
7474

7575

76-
class BookstoreServicer(object):
76+
class BookstoreServicer:
7777
"""A simple Bookstore API.
7878
7979
The API manages shelves and books resources. Shelves contain books.

0 commit comments

Comments
 (0)