python 狀態機 Python 狀態機

2021-10-13 12:00:53 字數 3221 閱讀 1380

class statemachine:

def __init__(self):

self.handlers = {} # 狀態轉移函式字典

self.startstate = none # 初始狀態

self.endstate = # 最終狀態集合

# 引數name為狀態名,handler為狀態轉移函式,end_state表明是否為最終狀態

def add_state(self, name, handler, end_state=0):

name = name.upper()

self.handlers[name] = handler

if end_state:

def set_start(self, name):

self.startstate = name.upper()

def run(self, cargo):

try:

handler = self.handlers[self.startstate]

except:

raise exception("must call .set_start() before .run()")

if not self.endstate:

raise exception("at least one state must be an end_state")

while true:

(newstate, cargo) = handler(cargo)

if newstate.upper() in self.endstate:

print("reached ", newstate)

break

else:

handler = self.handlers[newstate.upper()]

# 有限狀態集合

positive_adjectives = ["great", "super", "fun", "entertaining", "easy"]

negative_adjectives = ["boring", "difficult", "ugly", "bad"]

# 自定義狀態轉變函式

def start_transitions(txt):

# 用指定分隔符對字串進行切片,預設為空格分割,引數num指定分割次數

# 將"python is ***"語句分割為"python"和之後的"is ***"

splitted_txt = txt.split(none, 1)

word, txt = splitted_txt if len(splitted_txt) > 1 else (txt, "")

if word == "python":

newstate = "python_state" # 如果第乙個詞是python則可轉換到"python狀態"

else:

newstate = "error_state" # 如果第乙個詞不是python則進入終止狀態

return (newstate, txt) # 返回新狀態和餘下的語句txt

def python_state_transitions(txt):

splitted_txt = txt.split(none, 1)

word, txt = splitted_txt if len(splitted_txt) > 1 else (txt, "")

if word == "is":

newstate = "is_state"

else:

newstate = "error_state"

return (newstate, txt)

def is_state_transitions(txt):

splitted_txt = txt.split(none, 1)

word, txt = splitted_txt if len(splitted_txt) > 1 else (txt, "")

if word == "not":

newstate = "not_state"

elif word in positive_adjectives:

newstate = "pos_state"

elif word in negative_adjectives:

newstate = "neg_state"

else:

newstate = "error_state"

return (newstate, txt)

def not_state_transitions(txt):

splitted_txt = txt.split(none, 1)

word, txt = splitted_txt if len(splitted_txt) > 1 else (txt, "")

if word in positive_adjectives:

newstate = "neg_state"

elif word in negative_adjectives:

newstate = "pos_state"

else:

newstate = "error_state"

return (newstate, txt)

if __name__ == "__main__":

m = statemachine()

m.add_state("start", start_transitions) # 新增初始狀態

m.add_state("python_state", python_state_transitions)

m.add_state("is_state", is_state_transitions)

m.add_state("not_state", not_state_transitions)

m.add_state("neg_state", none, end_state=1) # 新增最終狀態

m.add_state("pos_state", none, end_state=1)

m.add_state("error_state", none, end_state=1)

m.set_start("start") # 設定開始狀態

m.run("python is great")

m.run("python is not fun")

m.run("perl is ugly")

m.run("pythoniseasy")

python 回憶狀態機

在模型中,將乙個或多個狀態定義為終態 end state 僅將乙個狀態定義為初始狀態 start state 呼叫類方法對此進行配置 每個處理程式都有某種必需的結構 處理程式將執行一系列操作,然後過一會兒,它帶著乙個標記返回到 statemachine.run 方法中的迴圈內,該標記指出了想得到的下...

python 狀態機教程 文字處理狀態機

狀態機是關於設計程式來控制應用程式中的流程。它是乙個有向圖,由一組節點和一組過渡函式組成。處理文字檔案通常包括順序讀取文字檔案的每個塊並執行某些操作以響應每個塊讀取。塊的含義取決於它之前存在的塊的型別以及它之後的塊。該機器是關於設計程式來控制應用程式中的流程。它是乙個有向圖,由一組節點和一組過渡函式...

狀態機 狀態機0

近半年都忙於做專案,沒有太多的時間去整理和總結在專案中用過的技術 個人還是覺得技術需要總結提煉和沉澱的,忙到沒時間去總結提公升其實不 是什麼好事,這次講下狀態機,在戰鬥型別的遊戲中角色有多種不同的狀態,而狀態的切換錯綜複雜,23種設計模式中有一種模式叫做狀態模式,不過 這種模式是把狀態切換條件放到各...