Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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 statemachine

class 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 statemachine

class 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 statemachine

class 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 statemachine

class 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