Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kyleconroy/statemachine
Simple finite-state machines in Python
https://github.com/kyleconroy/statemachine
Last synced: 11 days ago
JSON representation
Simple finite-state machines in Python
- Host: GitHub
- URL: https://github.com/kyleconroy/statemachine
- Owner: kyleconroy
- License: mit
- Archived: true
- Created: 2012-04-26T07:17:55.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2012-04-26T20:25:21.000Z (over 12 years ago)
- Last Synced: 2024-08-21T14:08:33.525Z (3 months ago)
- Language: Python
- Homepage:
- Size: 130 KB
- Stars: 39
- Watchers: 6
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# `statemachine`
[![Build Status](https://secure.travis-ci.org/tnhu/jsface.png?branch=master)](http://travis-ci.org/tnhu/jsface)
If you aren't programming with state machines, [you should be](http://www.shopify.com/technology/3383012-why-developers-should-be-force-fed-state-machines).
`statemachine` offers a simple and easy-to-use finite-state machine that can adapted into almost any code base.
## Usage
To create a state machine, mix in the `statemachine.Machine` class. The only requirement is an initial state, which are represented as strings.
```python
import statemachineclass TrafficLight(statemachine.Machine):
initial_state = 'red'
```This machine won't do much, but we can get the current state
```python
>>> 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.
```python
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.
```python
>>> stoplight = TrafficLight()
>>> stoplight.state
'red'
>>> stoplight.cycle()
>>> stoplight.state
'green'
```You can listen for transition events using the `before_transition` and `after_transition` decorators`.
```python
import statemachineclass TrafficLight(statemachine.Machine):
initial_state = 'red'@statemachine.event
def cycle(self):
yield 'red', 'green'
yield 'green', 'yellow'
yield 'yellow', 'red'@statemachine.after_transition('red', 'green')
def announce(self):
print "GO GO GO"
```Initiate the transition by calling `cycle`
```python
>>> stoplight = TrafficLight()
>>> stoplight.cycle()
'GO GO GO'
```If you only care about where you're coming from (or where you're going), use the `transition_from` and `transition_to` decorator
```python
import statemachineclass TrafficLight(statemachine.Machine):
initial_state = 'red'@statemachine.event
def cycle(self):
yield 'red', 'green'
yield 'green', 'yellow'
yield 'yellow', 'red'@transition_to('yellow')
def safety(self):
print "SLOW DOWN"@transition_from('red')
def announce(self):
print "GO GO GO"
```## Installation
$ pip install statemachine