Last active
June 13, 2020 22:09
-
-
Save irlabs/62d13aa22be2c78406d9083d7a8a1fa2 to your computer and use it in GitHub Desktop.
Running a timed sequence of events in python
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
#!/usr/bin/env python | |
import time | |
def main(): | |
print("Start program") | |
# Initialize variables | |
sequence = [ | |
{ 't': 1.0, 'msg': "Yakayak 1"}, | |
{ 't': 1.8, 'msg': "Yakayak 2"}, | |
{ 't': 2.6, 'msg': "Yaka 1", 'more': "YAKA 1"}, | |
{ 't': 3.0, 'msg': "Yaka 2", 'more': "YAKA 1"}, | |
{ 't': 3.4, 'msg': "Yaka 3", 'more': "YAKA 1"}, | |
{ 't': 3.8, 'msg': "Yaka 4", 'more': "YAKA 1"}, | |
{ 't': 5.0, 'msg': "The queen diva!"} | |
] | |
step = 0 | |
starttime = time.time() | |
# | |
# Run until sequence was completed | |
while step < len(sequence): | |
# Check the time | |
elapsed = time.time() - starttime | |
nextStepTime = sequence[step]['t'] | |
# If the time is past the time of the step, execute this step and go to the next step | |
if elapsed > nextStepTime: | |
# Execute the step | |
print(sequence[step]['msg']) | |
# Only execute the extra code if the 'more' key is present | |
if 'more' in sequence[step]: | |
print("---", sequence[step]['more'], "---") | |
# Increase the step counter | |
step += 1 | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Make sure you have at least python 2.7