Search This Blog

Wednesday, October 22, 2014

Python coroutine based state machine

I needed a simple state machine in a project I'm working on. Doing some research I came across coroutines. This post won't even try to explain what coroutines are. Instead, it just shows a super simple example of a finite state machine using coroutine.



This example is based on this and this. If you want to learn more about python coroutine, here is a great post.
def coroutine(func):
    """
    coroutine decorator, to automatically call next() upon creation
    """
    def start(*args,**kwargs):
        cr = func(*args,**kwargs)
        cr.next()
        return cr
    return start


@coroutine
def StateMachine():
    """
    The state machine
    """
    state = 'A'
    while True:
        print(state)
        input = (yield)
        if state == 'A':
            if input == 'a':
                state = 'B'
            elif input == 'b':
                state = 'C'
            else:
                break
        elif state == 'B':
            if input == 'a':
                state = 'C'
            elif input == 'b':
                state = 'A'
            else:
                break
        elif state == 'C':
            if input == 'a':
                state = 'A'
            elif input == 'b':
                state = 'B'
            else:
                break
        else:
            break

if __name__ == '__main__':
    sm = StateMachine()
    sm.send('a')
    sm.send('a')
    sm.send('b')
    sm.send('a')
    sm.send('b')
    sm.send('b')
    sm.send('b')
    sm.send('a')
    # sm.send('oops')

9 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. Good post however , I was wondering if you could write a litte more on this subject?
    - happy wheels
    - happy wheels
    - tetris
    - brain games
    - puzzle games
    - agario
    - abcdeyaya
    - fnaf

    ReplyDelete
  3. Thank you for the introduction of a range of information that is very interesting to see in this article.
    open facebook
    facebook login
    happy wheels
    happy wheels game
    earn to die
    earn to die game
    Good post! Thanks for sharing. Really very useful and informative post.

    ReplyDelete
  4. I must appreciate the way you have expressed your feelingsthrough your blog!. Click here to play
    shooting games | dog games | fighting games | mickey mouse games | Subway Surfers

    ReplyDelete
  5. Sometimes you can't explain what you see in a person. It's just the way they take you to a place where no one else can.
    mutant fighting cup 2 | manny pacquiao | stardew valley wiki | slither.io | powerpuff yourself | zumbi tsunami | fishdom | quotes about love | chinese food near me | snake game

    ReplyDelete
  6. Let’s keep out sites for your child! click:
    slither.io | wings.io | happy wheels | abcya | fnaf 4 | games for kids | racing games to play for free!

    ReplyDelete
  7. A good blog. Thanks for sharing the information. It is very useful for my future. keep sharing
    happy wheels | friv | girlsgogames | games2girls | happy wheels | fireboy and watergirl

    ReplyDelete
  8. I want you to thank for your time of this wonderful read!!! I definately enjoy every little bit of it and I have you bookmarked to check out new stuff of your blog a must read blog!
    slither io | wings io | science kombat | tank trouble 4

    ReplyDelete