1. 程式人生 > 其它 >python程序狀態

python程序狀態


思考

為啥我VS編輯器裡一直在import中報錯,但是寫到筆記本里就可以直接用
之前俄羅斯方塊也是同樣問題

安裝

pip install transitions

程式碼

from transitions import Machine
class Progress(object):
pass
model = Progress()
states = ['New', 'Ready', 'Waiting', 'Running', 'Terminated']
transitions=[
{'trigger': 'Admitted', 'source': 'New', 'dest': 'Ready'},
{'trigger': 'Input', 'source': 'Waiting', 'dest': 'Ready'},
{'trigger': 'Output', 'source': 'Waiting', 'dest': 'Ready'},
{'trigger': 'event_completion', 'source': 'Waiting', 'dest': 'Ready'},
{'trigger': 'event_wait', 'source': 'Running', 'dest': 'Waiting'},
{'trigger': 'Input', 'source': 'Running', 'dest': 'Waiting'},
{'trigger': 'Output', 'source': 'Running', 'dest': 'Waiting'},
{'trigger': 'Interrupt', 'source': 'Running', 'dest': 'Ready'},
{'trigger': 'Dispatch', 'source': 'Ready', 'dest': 'Running'},
{'trigger': 'Exit', 'source': 'Running', 'dest': 'Terminated'}]

machine=Machine(model=model,states=states,transitions=transitions,initial='New')

print(model.state)
model.Admitted()
print(model.state)
model.Dispatch()
print(model.state)
model.event_wait()
print(model.state)
model.Output()
print(model.state)
model.Dispatch()
print(model.state)
model.Exit()
print(model.state)