What is the Pythonic way to implement a simple FSM?

后端 未结 7 1636
猫巷女王i
猫巷女王i 2021-01-31 21:50

Yesterday I had to parse a very simple binary data file - the rule is, look for two bytes in a row that are both 0xAA, then the next byte will be a length byte, then skip 9 byte

7条回答
  •  死守一世寂寞
    2021-01-31 22:20

    The coolest way I've seen to implement FSMs in Python has to be via generators and coroutines. See this Charming Python post for an example. Eli Bendersky also has an excellent treatment of the subject.

    If coroutines aren't familiar territory, David Beazley's A Curious Course on Coroutines and Concurrency is a stellar introduction.

提交回复
热议问题