You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[](https://github.com/fgmacedo/python-statemachine/compare/main...develop)
But for your convenience, can easily ask if a state is active at any time:
134
129
130
+
```py
135
131
>>> traffic_light.green.is_active
136
132
False
137
133
@@ -141,56 +137,75 @@ True
141
137
>>> traffic_light.red.is_active
142
138
False
143
139
140
+
```
141
+
144
142
Easily iterate over all states:
145
143
144
+
```py
146
145
>>> [s.id for s in traffic_light.states]
147
146
['green', 'red', 'yellow']
148
147
148
+
```
149
+
149
150
Or over events:
150
151
152
+
```py
151
153
>>> [t.name for t in traffic_light.events]
152
154
['cycle', 'go', 'slowdown', 'stop']
153
155
156
+
```
157
+
154
158
Call an event by its name:
155
159
160
+
```py
156
161
>>> traffic_light.cycle()
157
162
Don't move.
158
163
'Running cycle from yellow to red'
159
164
165
+
```
160
166
Or send an event with the event name:
161
167
168
+
```py
162
169
>>> traffic_light.send('cycle')
163
170
Go ahead!
164
171
'Running cycle from red to green'
165
172
166
173
>>> traffic_light.green.is_active
167
174
True
168
175
176
+
```
169
177
You can't run a transition from an invalid state:
170
178
179
+
```py
171
180
>>> traffic_light.go()
172
181
Traceback (most recent call last):
173
182
statemachine.exceptions.TransitionNotAllowed: Can't go when in Green.
174
183
184
+
```
175
185
Keeping the same state as expected:
176
186
187
+
```py
177
188
>>> traffic_light.green.is_active
178
189
True
179
190
191
+
```
192
+
180
193
And you can pass arbitrary positional or keyword arguments to the event, and
181
194
they will be propagated to all actions and callbacks:
182
195
196
+
```py
183
197
>>> traffic_light.cycle(message="Please, now slowdown.")
184
198
'Running cycle from green to yellow. Please, now slowdown.'
185
199
200
+
```
186
201
187
-
Models
188
-
------
202
+
## Models
189
203
190
204
If you need to persist the current state on another object, or you're using the
191
205
state machine to control the flow of another object, you can pass this object
192
-
to the ``StateMachine`` constructor:
206
+
to the `StateMachine` constructor:
193
207
208
+
```py
194
209
>>>classMyModel(object):
195
210
...def__init__(self, state):
196
211
...self.state = state
@@ -219,13 +234,13 @@ True
219
234
>>> traffic_light.yellow.is_active
220
235
True
221
236
237
+
```
222
238
223
-
A more useful example
224
-
---------------------
225
-
226
-
A simple didactic state machine for controlling an ``Order``:
239
+
## A more useful example
227
240
241
+
A simple didactic state machine for controlling an `Order`:
228
242
243
+
```py
229
244
>>>classOrderControl(StateMachine):
230
245
... waiting_for_payment = State("Waiting for payment", initial=True)
231
246
... processing = State("Processing")
@@ -263,10 +278,11 @@ A simple didactic state machine for controlling an ``Order``:
263
278
...defon_enter_waiting_for_payment(self):
264
279
...self.payment_received =False
265
280
266
-
281
+
```
267
282
268
283
You can use this machine as follows.
269
284
285
+
```py
270
286
>>> control = OrderControl()
271
287
272
288
>>> control.add_to_order(3)
@@ -308,6 +324,31 @@ True
308
324
>>> control.completed.is_active
309
325
True
310
326
327
+
```
311
328
312
329
There's a lot more to cover, please take a look at our docs:
313
330
https://python-statemachine.readthedocs.io.
331
+
332
+
333
+
## Contributing
334
+
335
+
<aclass="github-button"href="https://github.com/fgmacedo/python-statemachine"data-icon="octicon-star"aria-label="Star fgmacedo/python-statemachine on GitHub">Star this project</a>
336
+
<aclass="github-button"href="https://github.com/fgmacedo/python-statemachine/issues"data-icon="octicon-issue-opened"aria-label="Issue fgmacedo/python-statemachine on GitHub">Open an Issue</a>
337
+
<aclass="github-button"href="https://github.com/fgmacedo/python-statemachine/fork"data-icon="octicon-repo-forked"aria-label="Fork fgmacedo/python-statemachine on GitHub">Fork</a>
338
+
339
+
- If you found this project helpful, please consider giving it a star on GitHub.
340
+
341
+
-**Contribute code**: If you would like to contribute code to this project, please submit a pull
342
+
request. For more information on how to contribute, please see our [contributing.md](contributing.md) file.
343
+
344
+
-**Report bugs**: If you find any bugs in this project, please report them by opening an issue
345
+
on our GitHub issue tracker.
346
+
347
+
-**Suggest features**: If you have a great idea for a new feature, please let us know by opening
348
+
an issue on our GitHub issue tracker.
349
+
350
+
-**Documentation**: Help improve this project's documentation by submitting pull requests.
351
+
352
+
-**Promote the project**: Help spread the word about this project by sharing it on social media,
353
+
writing a blog post, or giving a talk about it. Tag me on Twitter
354
+
[@fgmacedo](https://twitter.com/fgmacedo) so I can share it too!
0 commit comments