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

15 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. After reading this blog I am very strong in this topics and this blog is really helpful to all.. Explanation are very clear so it is easy to understand.. Thanks for sharing this blog…
    Python Training in Chennai | Dot Net Training in Chennai

    ReplyDelete
  10. This is a very good article material and it is very useful for us all. thank you . cara menggugurkan kandungan

    ReplyDelete
  11. There are things I can not understand but I will learn it. I think nothing is too late. Then I realized that I had improved a lot.
    http://colorswitchplay.com

    ReplyDelete
  12. I enjoyed over read your blog post. Your blog have nice information, I got good ideas from this amazing blog. I am always searching like this type blog post. I hope I will see again
    FRIV100 GAMES
    FRIV - Friv 100 - Friv 100 Game - Friv games
    FRIV10000 | FRIV1000 | FRIV100 GAMES

    ReplyDelete
  13. We are really grateful for your blog post. You will find a lot of approaches after visiting your post. Great work.
    FRIV | FRIV 1 | Friv 2017 Games
    FRIV 2 & Friv 2 Games | Friv2 - Friv games | Friv Online
    FRIV 7 & Friv - Friv Games

    ReplyDelete
  14. This blog is so nice to me. I will continue to come here again and again. Visit my link as well. Good luck
    http://www.jualobataborsiherbal.com/ obat aborsi
    http://caramenggugurkankandungan.info/ cara menggugurkan kandungan
    http://www.jualobataborsiherbal.com/cara-menggugurkan-kandungan/ cara menggugurkan kandungan
    http://jualobatpenggugurkandungan.net/ obat penggugur kandungan
    http://tandatandakehamilan.net/ tanda tanda kehamilan
    http://tandatandakehamilan.net/cara-cepat-dan-selamat-menggugurkan-kandungan/ cara menggugurkan kandungan
    http://obataborsi59.com/ obat aborsi
    http://obattelatdatangbulan.info/ obat telat datang bulan
    http://klinikobataborsi.com/ jual obat aborsi

    ReplyDelete