Skip to content

Instantly share code, notes, and snippets.

@chartjes
Created October 24, 2018 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chartjes/67a752d73c5867b18bb3a9a898dbb93d to your computer and use it in GitHub Desktop.
Save chartjes/67a752d73c5867b18bb3a9a898dbb93d to your computer and use it in GitHub Desktop.
How to make this more ideomatically pythonic
while test_completed is False:
ret = v1.list_pod_for_all_namespaces(watch=False)
for i in ret.items:
container_status = i.status.container_statuses
for container in container_status:
if isinstance(container.state.terminated, client.V1ContainerStateTerminated):
if container.state.terminated.reason == 'Completed':
bar.finish()
test_completed = True
if container.state.terminated.exit_code == 0:
print('Loadtest containers exited without errors')
else:
print('Loadtest containers exited with errors, check logs')
bar.update()
@tarekziade
Copy link

tarekziade commented Oct 24, 2018

Looks good.

Something I don't get in your code, once you put test_completed to True, do you really want to continue looping?
Do we want to call bar.finish() several times?

This is how I would change it, for the following reasons:

  • avoid extra variables
  • avoid calling the same dotted.value several times (expensive in Python)
  • avoid one letter variables
  • reduce the complexity by having less imbricated loops
  • use a function to return immediatly in case we want to call bar.finsih() on a first hit?
def test_containers(items):
    for item in items:
        for container in item.status.container_statuses:
            terminated = container.state.terminated
            if (not isinstance(terminated, client.V1ContainerStateTerminated)
                or terminated.reason != 'Completed'):
                continue
            if terminated.exit_code == 0:
                print('Loadtest containers exited without errors')
            else:
                print('Loadtest containers exited with errors, check logs')
            bar.finish()
            return True
    return False

while not test_containers(v1.list_pod_for_all_namespaces(watch=False).items):
    bar.update()

@tarekziade
Copy link

tarekziade commented Oct 24, 2018

One with an iterator :)

def terminated_iter():
    for item in v1.list_pod_for_all_namespaces(watch=False).items:
        for container in item.status.container_statuses:
            terminated = container.state.terminated
            if isinstance(terminated, client.V1ContainerStateTerminated):
                yield terminated

def test_containers():
    for terminated in terminated_iter():
        if terminated.reason != 'Completed':
            continue
        if terminated.exit_code == 0:
            print('Loadtest containers exited without errors')
        else:
            print('Loadtest containers exited with errors, check logs')
        bar.finish()
        return True

    return False

while not test_containers():
    bar.update()

@chartjes
Copy link
Author

Hey Tarek,

Thanks for all the suggestions. I do want the while loop to end once the containers have completed running

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment