Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Dec 31, 2023. It is now read-only.

Commit8f47a01

Browse files
authored
docs(samples): Fixing an issue with listing all instances (#349)
1 parent814be9e commit8f47a01

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

‎samples/ingredients/instances/list_all.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
# This is an ingredient file. It is not meant to be run directly. Check the samples/snippets
15+
# This is an ingredient file. It is not meant to be run directly. Check the samples/snippets
1616
# folder for complete code samples that are ready to be used.
1717
# Disabling flake8 for the ingredients file, as it would fail F821 - undefined name check.
1818
# flake8: noqa
19+
fromcollectionsimportdefaultdict
1920
fromtypingimportDict,Iterable
2021

2122
fromgoogle.cloudimportcompute_v1
@@ -42,17 +43,16 @@ def list_all_instances(
4243

4344
agg_list=instance_client.aggregated_list(request=request)
4445

45-
all_instances={}
46+
all_instances=defaultdict(list)
4647
print("Instances found:")
4748
# Despite using the `max_results` parameter, you don't need to handle the pagination
4849
# yourself. The returned `AggregatedListPager` object handles pagination
4950
# automatically, returning separated pages as you iterate over the results.
5051
forzone,responseinagg_list:
5152
ifresponse.instances:
52-
all_instances[zone]=response.instances
53+
all_instances[zone].extend(response.instances)
5354
print(f"{zone}:")
5455
forinstanceinresponse.instances:
5556
print(f" -{instance.name} ({instance.machine_type})")
5657
returnall_instances
5758
# </INGREDIENT>
58-

‎samples/snippets/instances/list_all.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121

2222
# [START compute_instances_list_all]
23+
fromcollectionsimportdefaultdict
2324
fromtypingimportDict,Iterable
2425

2526
fromgoogle.cloudimportcompute_v1
@@ -45,14 +46,14 @@ def list_all_instances(
4546

4647
agg_list=instance_client.aggregated_list(request=request)
4748

48-
all_instances={}
49+
all_instances=defaultdict(list)
4950
print("Instances found:")
5051
# Despite using the `max_results` parameter, you don't need to handle the pagination
5152
# yourself. The returned `AggregatedListPager` object handles pagination
5253
# automatically, returning separated pages as you iterate over the results.
5354
forzone,responseinagg_list:
5455
ifresponse.instances:
55-
all_instances[zone]=response.instances
56+
all_instances[zone].extend(response.instances)
5657
print(f"{zone}:")
5758
forinstanceinresponse.instances:
5859
print(f" -{instance.name} ({instance.machine_type})")

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp