Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add support for Travis CI

  • Loading branch information...
commit 068ccc559ab781f4ac78005beff6d239f9045e48 1 parent e06ea72
@kyleconroy authored
View
9 .travis.yml
@@ -0,0 +1,9 @@
+language: python
+python:
+ - "2.6"
+ - "2.7"
+ - "3.2"
+# command to install dependencies
+install: pip install -r requirements.txt --use-mirrors
+# command to run tests
+script: nosetests
View
2  README.md
@@ -23,7 +23,7 @@ This machine won't do much, but we can get the current state
'red'
```
-We can add state transitions using the `event` decorator. These functions must return an iterable of transitions. A transition is just a two-tuple. The first element is an iterable of states (or the wilcard '*') and the second element is the target state.
+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
View
19 examples/traffic_light.py
@@ -1,22 +1,21 @@
-from statemachine import event, Machine
-from statemachine import before_transition, after_transition, around_transition
+import statemachine as fsm
-class TrafficLight(Machine):
- initial_state = 'stop'
+class TrafficLight(fsm.Machine):
+ initial_state = 'red'
count = 0
- @after_transition('stop', 'proceed')
+ @fsm.after_transition('red', 'green')
def chime(self):
self.count += 1
- @after_transition('*', 'stop')
+ @fsm.after_transition('*', 'red')
def apply_brakes(self):
self.stopped = True
- @event
+ @fsm.event
def cycle(self):
- yield 'stop', 'proceed'
- yield 'proceed', 'caution'
- yield 'caution', 'stop'
+ yield 'red', 'green'
+ yield 'green', 'yellow'
+ yield 'yellow', 'red'
View
1  requirements.txt
@@ -0,0 +1 @@
+nose
View
25 statemachine.py
@@ -16,20 +16,21 @@ def __new__(cls, name, bases, d):
before, after = [], []
- for name, method in d.iteritems():
+ for name, func in d.iteritems():
try:
- after += [(from_, to, method) for from_, to in method.after]
+ after += [(start, end, func) for start, end in func.after]
except AttributeError:
pass
try:
- before += [(from_, to, method) for from_, to in method.before]
+ before += [(start, end, func) for start, end in func.before]
except AttributeError:
pass
d['_after_transitions'] = after
d['_before_transitions'] = before
d['_state'] = state
+
return type.__new__(cls, name, bases, d)
@@ -41,24 +42,22 @@ def state(self):
return self._state
-def after_transition(from_state, to_state):
+def create_transition(attr, from_state, to_state):
def wrapper(f):
try:
- f.after.append((from_state, to_state))
+ getattr(f, attr).append((from_state, to_state))
except AttributeError:
- f.after = [(from_state, to_state)]
+ setattr(f, attr, [(from_state, to_state)])
return f
return wrapper
+def after_transition(from_state, to_state):
+ return create_transition('after', from_state, to_state)
+
+
def before_transition(from_state, to_state):
- def wrapper(f):
- try:
- f.before.append((from_state, to_state))
- except AttributeError:
- f.before = [(from_state, to_state)]
- return f
- return wrapper
+ return create_transition('before', from_state, to_state)
def around_transition(f):
View
8 tests/test_traffic_light.py
@@ -5,11 +5,11 @@
def test_traffic_light_cycle():
light = TrafficLight()
light.cycle()
- assert_equals('proceed', light.state)
+ assert_equals('green', light.state)
light.cycle()
- assert_equals('caution', light.state)
+ assert_equals('yellow', light.state)
light.cycle()
- assert_equals('stop', light.state)
+ assert_equals('red', light.state)
def test_traffic_light_cycle():
@@ -20,4 +20,4 @@ def test_traffic_light_cycle():
def test_traffic_light_initial():
light = TrafficLight()
- assert_equals('stop', light.state)
+ assert_equals('red', light.state)
Please sign in to comment.
Something went wrong with that request. Please try again.