本文实例讲述了Python实现简单状态框架的方法。分享给大家供大家参考。具体分析如下:
这里使用Python实现一个简单的状态框架,代码需要在python3.2环境下运行
复制代码 代码如下:from time import sleep
from random import randint, shuffle
class StateMachine(object):
''' Usage: Create an instance of StateMachine, use set_starting_state(state) to give it an
initial state to work with, then call tick() on each second (or whatever your desired
time interval might be. '''
def set_starting_state(self, state):
''' The entry state for the state machine. '''
state.enter()
self.state = state
def tick(self):
''' Calls the current state's do_work() and checks for a transition '''
next_state = self.state.check_transitions()
if next_state is None:
# Stick with this state
self.state.do_work()
else:
# Next state found, transition to it
self.state.exit()
next_state.enter()
self.state = next_state
class BaseState(object):
''' Usage: Subclass BaseState and override the enter(), do_work(), and exit() methods.
enter() -- Setup for your state should occur here. This likely includes adding
transitions or initializing member variables.
do_work() -- Meat and potatoes of your state. There may be some logic here that will
cause a transition to trigger.
exit() -- Any cleanup or final actions should occur here. This is called just
before transition to the next state.
'''
def add_transition(self, condition, next_state):
''' Adds a new transition to the state. The "condition" param must contain a callable
object. When the "condition" evaluates to True, the "next_state" param is set as
the active state. '''
# Enforce transition validity
assert(callable(condition))
assert(hasattr(next_state, "enter"))
assert(callable(next_state.enter))
assert(hasattr(next_state, "do_work"))
assert(callable(next_state.do_work))
assert(hasattr(next_state, "exit"))
assert(callable(next_state.exit))
# Add transition
if not hasattr(self, "transitions"):
self.transitions = []
self.transitions.append((condition, next_state))
def check_transitions(self):
''' Returns the first State thats condition evaluates true (condition order is randomized) '''
if hasattr(self, "transitions"):
shuffle(self.transitions)
for transition in self.transitions:
condition, state = transition
if condition():
return state
def enter(self):
pass
def do_work(self):
pass
def exit(self):
pass
##################################################################################################
############################### EXAMPLE USAGE OF STATE MACHINE ###################################
##################################################################################################
class WalkingState(BaseState):
def enter(self):
print("WalkingState: enter()")
def condition(): return randint(1, 5) == 5
self.add_transition(condition, JoggingState())
self.add_transition(condition, RunningState())
def do_work(self):
print("Walking...")
def exit(self):
print("WalkingState: exit()")
class JoggingState(BaseState):
def enter(self):
print("JoggingState: enter()")
self.stamina = randint(5, 15)
def condition(): return self.stamina <= 0
self.add_transition(condition, WalkingState())
def do_work(self):
self.stamina -= 1
print("Jogging ({0})...".format(self.stamina))
def exit(self):
print("JoggingState: exit()")
class RunningState(BaseState):
def enter(self):
print("RunningState: enter()")
self.stamina = randint(5, 15)
def walk_condition(): return self.stamina <= 0
self.add_transition(walk_condition, WalkingState())
def trip_condition(): return randint(1, 10) == 10
self.add_transition(trip_condition, TrippingState())
def do_work(self):
self.stamina -= 2
print("Running ({0})...".format(self.stamina))
def exit(self):
print("RunningState: exit()")
class TrippingState(BaseState):
def enter(self):
print("TrippingState: enter()")
self.tripped = False
def condition(): return self.tripped
self.add_transition(condition, WalkingState())
def do_work(self):
print("Tripped!")
self.tripped = True
def exit(self):
print("TrippingState: exit()")
if __name__ == "__main__":
state = WalkingState()
state_machine = StateMachine()
state_machine.set_starting_state(state)
while True:
state_machine.tick()
sleep(1)
希望本文所述对大家的Python程序设计有所帮助。
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
更新日志
- 雨林唱片《赏》新曲+精选集SACD版[ISO][2.3G]
- 罗大佑与OK男女合唱团.1995-再会吧!素兰【音乐工厂】【WAV+CUE】
- 草蜢.1993-宝贝对不起(国)【宝丽金】【WAV+CUE】
- 杨培安.2009-抒·情(EP)【擎天娱乐】【WAV+CUE】
- 周慧敏《EndlessDream》[WAV+CUE]
- 彭芳《纯色角3》2007[WAV+CUE]
- 江志丰2008-今生为你[豪记][WAV+CUE]
- 罗大佑1994《恋曲2000》音乐工厂[WAV+CUE][1G]
- 群星《一首歌一个故事》赵英俊某些作品重唱企划[FLAC分轨][1G]
- 群星《网易云英文歌曲播放量TOP100》[MP3][1G]
- 方大同.2024-梦想家TheDreamer【赋音乐】【FLAC分轨】
- 李慧珍.2007-爱死了【华谊兄弟】【WAV+CUE】
- 王大文.2019-国际太空站【环球】【FLAC分轨】
- 群星《2022超好听的十倍音质网络歌曲(163)》U盘音乐[WAV分轨][1.1G]
- 童丽《啼笑姻缘》头版限量编号24K金碟[低速原抓WAV+CUE][1.1G]