Skip to content

Commit 8614ce3

Browse files
committed
docs: document transitions.experimental.utils.{add_transitions, event, with_model_definitions, transition}
1 parent 212f237 commit 8614ce3

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

README.md

+47
Original file line numberDiff line numberDiff line change
@@ -1306,6 +1306,53 @@ model = Model()
13061306
machine = Machine(model, **simple_config)
13071307
```
13081308

1309+
Defining model methods that will be overridden adds a bit of extra work.
1310+
It might be cumbersome to switch back and forth to make sure event names are spelled correctly, especially if states and transitions are defined in lists before or after your model. You can cut down on the boilerplate and the uncertainty of working with strings by defining states as enums. You can also define transitions right in your model class with the help of `add_transitions` and `event`.
1311+
It's up to you whether you use the function decorator `add_transitions` or event to assign values to attributes depends on your preferred code style.
1312+
They both work the same way, have the same signature, and should result in (almost) the same IDE type hints.
1313+
As this is still a work in progress, you'll need to create a custom Machine class and use with_model_definitions for transitions to check for transitions defined that way.
1314+
1315+
```python
1316+
from enum import Enum
1317+
1318+
from transitions.experimental.utils import with_model_definitions, event, add_transitions, transition
1319+
from transitions import Machine
1320+
1321+
1322+
class State(Enum):
1323+
A = "A"
1324+
B = "B"
1325+
C = "C"
1326+
1327+
1328+
class Model:
1329+
1330+
state: State = State.A
1331+
1332+
@add_transitions(transition(source=State.A, dest=State.B), [State.C, State.A])
1333+
@add_transitions({"source": State.B, "dest": State.A})
1334+
def foo(self): ...
1335+
1336+
bar = event(
1337+
{"source": State.B, "dest": State.A, "conditions": lambda: False},
1338+
transition(source=State.B, dest=State.C)
1339+
)
1340+
1341+
1342+
@with_model_definitions # don't forget to define your model with this decorator!
1343+
class MyMachine(Machine):
1344+
pass
1345+
1346+
1347+
model = Model()
1348+
machine = MyMachine(model, states=State, initial=model.state)
1349+
model.foo()
1350+
model.bar()
1351+
assert model.state == State.C
1352+
model.foo()
1353+
assert model.state == State.A
1354+
```
1355+
13091356
### <a name="extensions"></a> Extensions
13101357

13111358
Even though the core of transitions is kept lightweight, there are a variety of MixIns to extend its functionality. Currently supported are:

0 commit comments

Comments
 (0)