【发布时间】:2011-01-07 07:13:29
【问题描述】:
与 this Stack Overflow question (C state-machine design) 相关,StackOverflow 的人能否与我(和社区)分享您的 Python 状态机设计技术?
目前,我正在寻找基于以下内容的引擎:
class TrackInfoHandler(object):
def __init__(self):
self._state="begin"
self._acc=""
## ================================== Event callbacks
def startElement(self, name, attrs):
self._dispatch(("startElement", name, attrs))
def characters(self, ch):
self._acc+=ch
def endElement(self, name):
self._dispatch(("endElement", self._acc))
self._acc=""
## ===================================
def _missingState(self, _event):
raise HandlerException("missing state(%s)" % self._state)
def _dispatch(self, event):
methodName="st_"+self._state
getattr(self, methodName, self._missingState)(event)
## =================================== State related callbacks
但我确信在利用 Python 的动态特性(例如动态调度)的同时,有很多方法可以实现。
我正在为接收“事件”和“调度”的“引擎”设计技术,而这些技术基于机器的“状态”。
【问题讨论】:
-
套用 Adam 的观点,我认为有关您要完成的工作的一些更具体的信息会有所帮助。
-
@Jason Orendorff:很公平。我已经相应地更新了问题。
标签: python state-machine fsm