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

83 comments:

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

    ReplyDelete
    Replies
    1. Wonderful article! We will be linking to this great post on our site.
      Keep up the good writing. Simcity Buildit Hack

      Delete
  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
  15. Nice information my sincere thanks for sharing this post Please Continue to share this kind of post
    Python Training in BTM Layout

    ReplyDelete
  16. Nice Details on Python coroutine based state machine:
    Devops Training
    itEanz

    ReplyDelete
  17. Good post and I like it very much. By the way, anybody try this app development company for iOS and Android? I find it is so professional to help me boost app ranking and increase app downloads.

    ReplyDelete
  18. Those guidelines additionally worked to become a good way to
    recognize that other people online have the identical fervor like mine
    to grasp great deal more around this condition.



    aws training in chennai



    aws training in bangalore

    ReplyDelete
  19. Those guidelines additionally worked to become a good way to recognize that other people online have the identical fervor like mine to grasp great deal more around this condition.
    python training in chennai

    ReplyDelete
  20. This is really a pleasant and useful, containing all data furthermore greatly affect the new innovation. Much obliged for sharing it,
    Avriq India
    Avriq
    AVRiQ - Data Recovery
    AVRiQ Crunchbase
    Avriq Services
    Avriq Us

    ReplyDelete
  21. I really enjoyed while reading your article, the information you have delivered in this post was damn good. Keep sharing your post with efficient news.
    Final Year Project Center in Chennai | Final Year Project Center in Velachery

    ReplyDelete
  22. very interesting post.this is my first time visit here.i found so many interesting stuff in your blog especially its discussion..thanks for the post! psd to responsive wordpress

    ReplyDelete
  23. Really informative data on MSBI, looking for best msbi online training institute ?

    ReplyDelete
  24. Thanks for sharing this niche useful informative post to our knowledge..Embedded Project Center in Chennai | Embedded Project Center in Velachery

    ReplyDelete
  25. thanks for providing your awesome article with valuable information.keep updating such an wonderful post..
    Best Cloud Computing Project Center in Chennai | Cloud Computing Project Center in Velachery

    ReplyDelete
  26. Well Said, you have furnished the right information that will be useful to anyone at all time. Thanks for sharing your Ideas.
    BE/B.Tech Project Center in Chennai | BE/B.Tech Project Center in Velachery

    ReplyDelete
  27. Nice post. it was really very useful and informative .thank you for sharing and keep updating.
    Microsoft Azure Training Institute in Chennai|Microsoft Azure Training Institute in Velachery

    ReplyDelete
  28. I simply wanted to write down a quick word to say thanks to you for those wonderful tips and hints you are showing on this site.
    dotnet training in chennai

    ReplyDelete
  29. I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly.
    Final Year Project Center in Chennai | Final Year Project Center in Velachery

    ReplyDelete
  30. Nice blog.Thank you for the introduction of information that is very interesting to see in this article.keep sharing..
    Best VMware Training Institute in chennai| Best VMware Training Institute in Velachery

    ReplyDelete
  31. Excellent post..Thanks for sharing your valuable and interesting article..Embedded Project Center in Chennai | Embedded Project Center in Velachery

    ReplyDelete
  32. You've made some good points there. I looked on the internet for more information about this
    Mainframe Training In Chennai | Hadoop Training In Chennai | ETL Testing Training In Chennai

    ReplyDelete
  33. Thanks for sharing this nice and useful informative post to our knowledge.Summer Course in Thiruvanmiyur | Summer Course in Chennai

    ReplyDelete
  34. Wonderful post..Thank you for updating such a informative post..Summer Courses in Chennai | Summer Courses in Velachery

    ReplyDelete
  35. I believe that there would be great opportunities for those who are coming into this site. Thanks for sharing and please do keep sharing on...
    Best Online Software Training Institute | Python Training

    ReplyDelete
  36. Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing..
    java training in chennai
    java course in chennai

    ReplyDelete
  37. Very impressive and informative article.. thanks for sharing your valuable information.. it is very useful and easy to learn For Beginners.keep rocking and updating... looking for further blogs..
    python training in hyderabad

    ReplyDelete
  38. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us. Do check Six Sigma Training in Bangalore | Six Sigma Training in Dubai & Get trained by an expert who will enrich you with the latest trends.

    ReplyDelete
  39. I strongly believe that your site is very informative and very useful for us. It was a great time to find your blog. Keep going on and always share your updates with us. see more: Python Online Training

    ReplyDelete
  40. This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me.

    rpa training in chennai
    rpa training in bangalore
    rpa course in bangalore
    best rpa training in bangalore
    rpa online training

    ReplyDelete
  41. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
    Best Devops Training in pune
    excel advanced excel training in bangalore
    Devops Training in Chennai

    ReplyDelete
  42. You have provided a nice article, Thank you very much for this. I hope this will be useful for many people. Please keep on updating these type of blogs with good content.Thank You...
    aws online training
    aws training in hyderabad
    amazon web services(AWS) online training

    ReplyDelete