statemachineSimple Finite-State Machines | |
Download |
statemachine Ranking & Summary
Advertisement
- License:
- MIT/X Consortium Lic...
- Price:
- FREE
- Publisher Name:
- Kyle Conroy
- Publisher web site:
- http://github.com/derferman/
statemachine Tags
statemachine Description
statemachine is a Python module that offers a simple and easy-to-use finite-state machine that can adapted into almost any code base.UsageTo create a state machine, mix in the statemachine.Machine class. The only requirement is an initial state, which are represented as strings.import statemachineclass TrafficLight(statemachine.Machine): initial_state = 'red'This machine won't do much, but we can get the current state>>> stoplight = TrafficLight()>>> stoplight.state'red'We can add state transitions using the event decorator. These functions return an iterable of transitions. A transition is just a two-tuple. The first element is an iterable of states, the wilcard '*', or a single state. The second element is the target state.import statemachineclass TrafficLight(statemachine.Machine): initial_state = 'red' @statemachine.event def cycle(self): yield 'red', 'green' yield 'green', 'yellow' yield 'yellow', 'red'Calling the cycle method will transition the machine into the next state.>>> stoplight = TrafficLight()>>> stoplight.state'red'>>> stoplight.cycle()>>> stoplight.state'green'Installationpip install statemachineProduct's homepage
statemachine Related Software