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')

23 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
  9. Love your blog!! Why don't you blog anymore :( I miss your blog. Wahhh
    fireboy and watergirl
    Geometry Dash

    ReplyDelete
  10. The blog or and best that is extremely useful to keep I can share the ideas. Age Of War 2
    Big Farm | Slitherio | Tank Trouble
    Of the future as this is really what I was looking for, I am very comfortable and pleased to come here. Thank you very much.
    Happy Wheels | Goodgeme Empire | Slither.io

    ReplyDelete
  11. One of only a few days long holiday. It is time to help you rest, relax, go out and gather friends and relatives.
    baixar facebook l baixar whatsapp l slitherio l run 2 l happy wheels l piano tiles 2

    ReplyDelete
  12. Such a very useful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article.

    Discover our website bounty of free online games now!
    Our website has the biggest collection of free online games. Totally new games are added every day!

    age of war 2| gold Miner 2| unfair Mario 2 | cubefield 2 |tanki Online 2

    ReplyDelete
  13. The blog or and best that is extremely useful to keep I can share the ideas. Age Of War 2
    Big Farm | Slitherio | Tank Trouble
    Of the future as this is really what I was looking for, I am very comfortable and pleased to come here. Thank you very much.
    Happy Wheels | Goodgeme Empire | Slither.io


    ReplyDelete
  14. Very interesting blog. Alot of blogs I see these days don't really provide anything that I'm interested in, but I'm most interested in this one. Just thought that I would post and let you know.baixar mobogenie, Happy Wheels 4

    ReplyDelete

  15. There is more information for us.I really impress to visit your website.It is so good.Thanks for sharing with us.
    al3ab-banat01 العاب بنات
    العب الان احلى العاب طبخ

    ReplyDelete
  16. for beginners like me need a lot of reading and searching for information on various blogs. and articles that you share a very nice and inspires me

    ReplyDelete
  17. Facebook has become a part of my life, not only helped me to connect with you but also where I can share every moment in life. I really like the convenience that it brings
    facebook baixar , baixar facebook , baixar facebook gratis

    ReplyDelete
  18. The article gives me a lot of interesting information. I still often share life experiences with friends. And I enjoyed the convenience and attractiveness of free messaging app
    whatsapp baixar , baixar whatsapp , baixar whatsapp gratis

    ReplyDelete
  19. I think what you say on here is the best thing. Looking forward for our lives become increasingly more beautiful
    subway surfers game , subway surfer , subway surfers

    ReplyDelete
  20. Just remember your stab at this business, should you decide to end it, is not a failure but a training ground. You will take with you what has worked and learned from what has not in order to make the next business idea a success!
    return man 2 game , return man 2 , return man

    ReplyDelete
  21. This is extremely helpful info!! Very good work. Everything is very interesting to learn and easy to understood. Thank you for giving information.
    Facebook Lite

    ReplyDelete
  22. I would like to thank you for the efforts you have made in writing this article.
    super smash flash 2 unblocked | strike force kitty | super smash flash | super smash flash 2

    ReplyDelete