Created
October 24, 2018 16:12
-
-
Save chartjes/67a752d73c5867b18bb3a9a898dbb93d to your computer and use it in GitHub Desktop.
How to make this more ideomatically pythonic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
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()
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
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: