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

135 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
  43. Hello! This is my first visit to your blog! We are a team of volunteers and starting a new initiative in a community in the same niche. Your blog provided us useful information to work on. You have done an outstanding job.
    Best AWS Training in Chennai | Amazon Web Services Training in Chennai
    AWS Training in Bangalore | Amazon Web Services Training in Bangalore
    Amazon Web Services Training in Pune | Best AWS Training in Pune

    ReplyDelete
  44. Sap fico training institute in Noida

    Sap fico training institute in Noida - Webtrackker Technology is IT Company which is providing the web designing, development, mobile application, and sap installation, digital marketing service in Noida, India and out of India. Webtrackker is also providing the sap fico training in Noida with working trainers.


    WEBTRACKKER TECHNOLOGY (P) LTD.
    C - 67, sector- 63, Noida, India.
    F -1 Sector 3 (Near Sector 16 metro station) Noida, India.

    +91 - 8802820025
    0120-433-0760
    0120-4204716
    EMAIL: info@webtrackker.com
    Website: www.webtrackker.com

    ReplyDelete
  45. Great post and informative blog.it was awesome to read, thanks for sharing this great content to my vision.

    CEH Training In Hyderbad

    ReplyDelete
  46. wow i got Output of your python code .Really it is simple to understand your programming. Good work
    Python Training and placement in Bangalore

    ReplyDelete
  47. Thanks For Sharing the Information The Information Shared Is very valuable Please Keep Updating us Time Just Went On Reading The article Python Online training Hadoop Online training Data Science Online training AWS Online training

    ReplyDelete
  48. I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting ..Good work..Best vacation course Training in and vacation course for Students in Kanchipuram|

    ReplyDelete
  49. Thank you for the introduction of information that is very interesting to see in this article.Best vacation course Training in and vacation course for Students in Kanchipuram|

    ReplyDelete
  50. very true indeed – its all about the blog. and i think this goes beyond blogging...Best summer courses traning for Students in Kanchipuram|

    ReplyDelete
  51. Nice information my sincere thanks for sharing this post Please Continue to share this kind of post..Best summer courses traning for Students in Kanchipuram|

    ReplyDelete
  52. I am obliged to you for sharing this piece of information here and updating us with your resourceful guidance. Hope this might benefit many learners. Keep sharing this gainful articles and continue updating us.
    Blue Prism Training in Chennai
    Blue Prism Course in Chennai
    Angularjs Training in Chennai
    AWS Training in Chennai
    Big Data Analytics Courses in Chennai
    CCNA Course in Chennai
    SEO Training in Chennai
    Blue Prism Training in Tambaram

    ReplyDelete
  53. This is really a pleasant and useful, containing all data furthermore greatly affect the new innovation. Best Hardware and Networking courses in kanchipuram|

    ReplyDelete
  54. the blog is very useful, interesting and informative. thank you for sharing the blog with us. keep on updating. Best Tally ERP 9 Training institute in kanchipuram|

    ReplyDelete
  55. very nice blogs!!! i have to learning for lot of information for this sites...Sharing for wonderful information. Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing

    Best AWS Training Academy in Kanchipuram

    ReplyDelete
  56. Really very great information to be provided and the All points discussed were worth reading and i’ll surely work with them all one by one.
    Best C and C++ Programming Training Academy in Kanchipuram

    ReplyDelete
  57. Awesome post. Really you are shared very informative concept... Thank you for sharing. Keep on updating...

    Best Graphics Designing Training Academy in Kanchipuram

    ReplyDelete
  58. Awesome post. Really you are shared very informative concept... Thank you for sharing. Keep on updating...

    Best Java Training Academy in Kanchipuram

    ReplyDelete
  59. Your Blog is nice and informative..Thanks for sharing this valuable information with us..I am visiting this website regularly, this web site sharing nice thoughts
    Best MATLAB Training Academy in Kanchipuram

    ReplyDelete
  60. I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing..
    Best Graphic Designing Training Institute in in Kanchipuram

    ReplyDelete
  61. Nice post... Really you are done a wonderful job. Thanks for sharing such wonderful information with us. Please keep on updating...
    Best Graphic designing Training Institute in in Kanchipuram

    ReplyDelete
  62. This information is very useful and innovative..I gained more knowledge from this.Thanks for the nice information its very useful to read your blog...
    Best web designing Training Institute in in Kanchipuram

    ReplyDelete
  63. Wonderful article.It is to define the concepts very well.Clearly explain the information.It has more valuable information for encourage me to achieve my career goal.
    Best dally erp 9 Training Institute in in Kanchipuram

    ReplyDelete
  64. Wow, really I am much interested to know our blog content is really good.Great information. Thank you for Sharing.
    Best C++ Training Institute in Kanchipuram

    ReplyDelete
  65. Awesome Post! I like writing style, the way you described the topics throughout the post. I hope many web reader will keep reading your post at the end, Thanks for sharing your view.
    Best web design Training Institute in Kanchipuram

    ReplyDelete
  66. Your article was so impressive and informative. Its very interesting to read. Thanks for sharing,
    Best web design Training Institute in Kanchipuram

    ReplyDelete
  67. I believe there are many more pleasurable opportunities ahead for individuals that looked at your site
    Best dotnet Training Institute in Kanchipuram

    ReplyDelete
  68. Wonderful article.It is to define the concepts very well.Clearly explain the information.It has more valuable information for encourage me to achieve my career goal.
    Best CCNP Training Institute in Kanchipuram

    ReplyDelete
  69. thank you for such a great article with us. hope it will be much useful for us. please keep on updating..
    Best software testing Training Institute in Kanchipuram

    ReplyDelete
  70. Thanks for sharing such a great information..Its really nice and informative.Great article, thanks for posting...
    Best software testing Training Institute in Kanchipuram

    ReplyDelete
  71. very nice blogs!!! i have to learning for lot of information for this sites...Sharing for wonderful information. Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing
    Best Web Designing and Development Training Institute in Kanchipuram

    ReplyDelete
  72. wonderful information. Thanks for sharing this valuable information to our vision. You have posted a trust worthy blog keep sharing
    Best C++ Training Institute in Kanchipuram

    ReplyDelete
  73. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.
    Devops Training in Electronic City

    ReplyDelete