国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python實現簡單狀態框架的方法

2020-02-23 00:24:30
字體:
來源:轉載
供稿:網友

本文實例講述了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.

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 疏勒县| 南城县| 静安区| 宁德市| 高台县| 资中县| 福建省| 深泽县| 红河县| 大余县| 横峰县| 灵璧县| 临颍县| 衡阳市| 比如县| 门头沟区| 阳谷县| 邳州市| 洪洞县| 盐池县| 梅州市| 耒阳市| 孟连| 夏津县| 丹巴县| 盘山县| 东阳市| 普宁市| 临武县| 阳新县| 临澧县| 福鼎市| 西藏| 平顶山市| 霞浦县| 鱼台县| 北辰区| 宣化县| 昌平区| 东乌| 南汇区|