Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvement to Python example code for aws.ec2.get_instances #9934

Open
xiongchiamiov opened this issue Sep 27, 2023 · 0 comments
Open

Improvement to Python example code for aws.ec2.get_instances #9934

xiongchiamiov opened this issue Sep 27, 2023 · 0 comments
Labels
area/docs Improvements or additions to documentation kind/enhancement Improvements or new features

Comments

@xiongchiamiov
Copy link

Problem description

Affected product version(s)

N/A (Or: Python SDK)

Suggestions for a fix

Currently there is this example on the page:

import pulumi
import pulumi_aws as aws

test_instances = aws.ec2.get_instances(instance_tags={
        "Role": "HardWorker",
    },
    filters=[aws.ec2.GetInstancesFilterArgs(
        name="instance.group-id",
        values=["sg-12345678"],
    )],
    instance_state_names=[
        "running",
        "stopped",
    ])
test_eip = []
for range in [{"value": i} for i in range(0, len(test_instances.ids))]:
    test_eip.append(aws.ec2.Eip(f"testEip-{range['value']}", instance=test_instances.ids[range["value"]]))

The last couple lines there took me a while to interpret. The most direct Pythonic translation would be to utilize enumerate:

test_eip = []
for i, instance_id in enumerate(test_instances.ids):
    test_eip.append(aws.ec2.Eip(f"testEip-{i}", instance=instance_id))

Arguably, using a list comprehension would be even more Pythonic, although some folks feel they make things too complex:

test_eip = [aws.ec2.Eip(f"testEip-{i}", instance=instance_id) for i, instance_id in enumerate(test_instances.ids)]

Here's an overall comparison of the approaches, using a mocked simplification of the data, if you find looking at example output helpful:

Python 3.11.5 (main, Aug 24 2023, 15:09:45) [Clang 14.0.3 (clang-1403.0.22.14.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from types import SimpleNamespace
>>> test_instances = SimpleNamespace()
>>> test_instances.ids = ['a', 'b', 'see']
>>> for range in [{"value": i} for i in range(0, len(test_instances.ids))]:
...     print(range['value'], test_instances.ids[range['value']])
...
0 a
1 b
2 see
>>> for i, id in enumerate(test_instances.ids):
...     print(i, id)
...
0 a
1 b
2 see
>>> ['{} {}'.format(i, id) for i, id in enumerate(test_instances.ids)]
['0 a', '1 b', '2 see']
>>> ['{} {}'.format(i, id) for i, id in enumerate([])]
[]

I found the spot in the aws sdk where this is defined, but based on the git blame it seems to be coming from somewhere else and I'm not sure where; after asking about this on the Slack, I was directed to post an issue here. I'm glad to make the actual change if that would be easiest.

@xiongchiamiov xiongchiamiov added the needs-triage Needs attention from the triage team label Sep 27, 2023
@github-project-automation github-project-automation bot moved this to 🤔 Triage in Docs 📚 Sep 27, 2023
@cnunciato cnunciato added area/docs Improvements or additions to documentation kind/enhancement Improvements or new features and removed needs-triage Needs attention from the triage team labels Sep 29, 2023
@cnunciato cnunciato moved this from 🤔 Triage to 🧳 Backlog in Docs 📚 Sep 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/docs Improvements or additions to documentation kind/enhancement Improvements or new features
Projects
Status: 🧳 Backlog
Development

No branches or pull requests

2 participants