Created
November 28, 2017 13:11
-
-
Save mikepsn/7d9d98a8c39ae45e2a31faf639a0f244 to your computer and use it in GitHub Desktop.
Show's how to use Python's dict to simulate a switch satement.
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
#!/bin/env/python | |
import numpy as np | |
def action1(t, dt): | |
print t, "Action 1" | |
def action2(t, dt): | |
print t, "Action 2" | |
def action3(t, dt): | |
print t, "Action 3" | |
def action4(t, dt): | |
print t, "Action 4" | |
def tick(t, dt): | |
""" | |
Create a dictionary called script, with the keys being | |
the times we want to call an action. The values are | |
references to the funct ions we want to call. | |
script is a (key, value) store of (time, function) | |
When this function is called, a time, we check to | |
see if t is in the script, if it is we get the value | |
by using script[t] and then call the function using | |
the calling mechanism script[t](t, dt) | |
""" | |
script = { | |
10.0 : action1, | |
15.0 : action2, | |
20.0 : action3, | |
27.0 : action4 | |
} | |
if t in script: | |
script[t](t, dt) | |
def main(): | |
t1, t2, dt = (0.0, 30.0, 0.1) | |
for t in np.arange(t1, t2, dt): | |
tick(t, dt) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment