Skip to content

Instantly share code, notes, and snippets.

@yeiichi
Created November 12, 2024 02:30
Show Gist options
  • Save yeiichi/f134bea394142754108b6f97033dbefc to your computer and use it in GitHub Desktop.
Save yeiichi/f134bea394142754108b6f97033dbefc to your computer and use it in GitHub Desktop.
Mimic do-while loop with an emergency brake feature.
#!/usr/bin/env python3
"""Mimic do-while loop with an emergency brake feature.
"""
def dummy_func():
for i in range(8):
yield i
def main():
num = dummy_func()
emergency_break_max = 12
emergency_break_sts = 0
while True:
# begin: main body
try:
print(next(num))
except StopIteration:
print('\033[31mLoop Runaway!\033[0m')
# end: main body
emergency_break_sts += 1
if emergency_break_sts > emergency_break_max:
print('\033[93mEB Applied\033[0m')
break
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment