项目作者: frodsan

项目描述 :
Minimal Finite State Machine
高级语言: Python
项目地址: git://github.com/frodsan/chappie.git
创建时间: 2016-05-31T10:36:42Z
项目社区:https://github.com/frodsan/chappie

开源协议:MIT License

下载


chappie

A minimal Finite State Machine library.

Ported from soveran/micromachine.

Installation

  1. $ pip install chappie

Usage

  1. from chappie import Machine
  2. # Setting an initial state
  3. machine = Machine.new("new")
  4. # Define the possible transitions
  5. machine.when("confirm", {"new": "confirmed"})
  6. machine.when("ignore", {"new": "ignored"})
  7. machine.when("reset", {"confirmed": "new", "ignored": "new"})
  8. # Trigger events
  9. machine.trigger("confirm") # => True
  10. machine.state # => "confirmed"
  11. machine.trigger("ignore") # => False
  12. machine.state # => "confirmed"
  13. machine.trigger("reset") # => True
  14. machine.state # => "new"
  15. # Includes support for callbacks
  16. def on_ignore
  17. print("Don't laugh, I'm being cool")
  18. def on_any
  19. print("I'm consciousness. I'm alive. I'm Chappie")
  20. machine.on("ignored", on_ignore)
  21. machine.on("any", on_any)
  22. machine.trigger("ignore")
  23. # => "Don't laugh, I'm being cool"
  24. # => "I'm consciousness. I'm alive. I'm Chappie"
  25. machine.state
  26. # => "ignored"

License

Released under the MIT License.