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
Copy file name to clipboardExpand all lines: README.md
+7-7Lines changed: 7 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -377,25 +377,25 @@ There's a lot more to cover, please take a look at our docs:
377
377
https://python-statemachine.readthedocs.io.
378
378
379
379
380
-
## Contributing to the project
380
+
## Contributing
381
381
382
382
* <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>
383
383
* <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>
384
384
* <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>
385
385
386
386
- If you found this project helpful, please consider giving it a star on GitHub.
387
387
388
-
-**Contribute code**: If you would like to contribute code to this project, please submit a pull
388
+
-**Contribute code**: If you would like to contribute code, please submit a pull
389
389
request. For more information on how to contribute, please see our [contributing.md](contributing.md) file.
390
390
391
-
-**Report bugs**: If you find any bugs in this project, please report them by opening an issue
391
+
-**Report bugs**: If you find any bugs, please report them by opening an issue
392
392
on our GitHub issue tracker.
393
393
394
-
-**Suggest features**: If you have a great idea for a new feature, please let us know by opening
395
-
an issue on our GitHub issue tracker.
394
+
-**Suggest features**: If you have an idea for a new feature, of feels something being harder than it should be,
395
+
please let us know by opening an issue on our GitHub issue tracker.
396
396
397
-
-**Documentation**: Help improve this project's documentation by submitting pull requests.
397
+
-**Documentation**: Help improve documentation by submitting pull requests.
398
398
399
-
-**Promote the project**: Help spread the word about this project by sharing it on social media,
399
+
-**Promote the project**: Help spread the word by sharing on social media,
400
400
writing a blog post, or giving a talk about it. Tag me on Twitter
401
401
[@fgmacedo](https://twitter.com/fgmacedo) so I can share it too!
Copy file name to clipboardExpand all lines: docs/async.md
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,9 +4,9 @@
4
4
Support for async code was added!
5
5
```
6
6
7
-
The {ref}`StateMachine` fully supports asynchronous code. You can write async {ref}`actions`, {ref}`guards`, and {ref}`event` triggers, while maintaining the same external API for both synchronous and asynchronous codebases.
7
+
The {ref}`StateMachine` fully supports asynchronous code. You can write async {ref}`actions`, {ref}`guards`, and {ref}`events` triggers, while maintaining the same external API for both synchronous and asynchronous codebases.
8
8
9
-
This is achieved through a new concept called "engine," an internal strategy pattern abstraction that manages transitions and callbacks.
9
+
This is achieved through a new concept called **engine**, an internal strategy pattern abstraction that manages transitions and callbacks.
10
10
11
11
There are two engines, {ref}`SyncEngine` and {ref}`AsyncEngine`.
Conditions also support [Boolean algebra](https://en.wikipedia.org/wiki/Boolean_algebra) expressions, allowing you to use compound logic within transition guards. You can use both standard Python logical operators (`not`, `and`, `or`) as well as classic Boolean algebra symbols:
44
+
### Condition expressions
45
+
46
+
This library supports a mini-language for boolean expressions in conditions, allowing the definition of guards that control transitions based on specified criteria. It includes basic [boolean algebra](https://en.wikipedia.org/wiki/Boolean_algebra) operators, parentheses for controlling precedence, and **names** that refer to attributes on the state machine, its associated model, or registered {ref}`Listeners`.
47
+
48
+
```{tip}
49
+
All condition expressions are evaluated when the State Machine is instantiated. This is by design to help you catch any invalid definitions early, rather than when your state machine is running.
50
+
```
51
+
52
+
The mini-language is based on Python's built-in language and the [`ast`](https://docs.python.org/3/library/ast.html) parser, so there are no surprises if you’re familiar with Python. Below is a formal specification to clarify the structure.
53
+
54
+
#### Syntax elements
55
+
56
+
1.**Names**:
57
+
- Names refer to attributes on the state machine instance, its model or listeners, used directly in expressions to evaluate conditions.
58
+
- Names must consist of alphanumeric characters and underscores (`_`) and cannot begin with a digit (e.g., `is_active`, `count`, `has_permission`).
59
+
- Any property name used in the expression must exist as an attribute on the state machine, model instance, or listeners, otherwise, an `InvalidDefinition` error is raised.
60
+
- Names can be pointed to `properties`, `attributes` or `methods`. If pointed to `attributes`, the library will create a
61
+
wrapper get method so each time the expression is evaluated the current value will be retrieved.
62
+
63
+
2.**Boolean operators and precedence**:
64
+
- The following Boolean operators are supported, listed from highest to lowest precedence:
65
+
1.`not` / `!` — Logical negation
66
+
2.`and` / `^` — Logical conjunction
67
+
3.`or` / `v` — Logical disjunction
68
+
- These operators are case-sensitive (e.g., `NOT` and `Not` are not equivalent to `not` and will raise syntax errors).
69
+
- Both formats can be used interchangeably, so `!sauron_alive` and `not sauron_alive` are equivalent.
46
70
47
-
-`!` for `not`
48
-
-`^` for `and`
49
-
-`v` for `or`
71
+
3.**Parentheses for precedence**:
72
+
- When operators with the same precedence appear in the expression, evaluation proceeds from left to right, unless parentheses specify a different order.
73
+
- Parentheses `(` and `)` are supported to control the order of evaluation in expressions.
74
+
- Expressions within parentheses are evaluated first, allowing explicit precedence control (e.g., `(is_admin or is_moderator) and has_permission`).
50
75
51
-
For example:
76
+
#### Expression Examples
77
+
78
+
Examples of valid boolean expressions include:
79
+
-`is_logged_in and has_permission`
80
+
-`not is_active or is_admin`
81
+
-`!(is_guest ^ has_access)`
82
+
-`(is_admin or is_moderator) and !is_banned`
83
+
-`has_account and (verified or trusted)`
84
+
-`frodo_has_ring and gandalf_present or !sauron_alive`
85
+
86
+
Being used on a transition definition:
52
87
53
88
```python
54
89
start.to(end, cond="frodo_has_ring and gandalf_present or !sauron_alive")
55
90
```
56
91
57
-
Both formats can be used interchangeably, so `!sauron_alive` and `not sauron_alive` are equivalent.
92
+
#### Summary of grammar rules
58
93
94
+
The mini-language is formally specified as follows:
Copy file name to clipboardExpand all lines: docs/transitions.md
+9-9Lines changed: 9 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -160,8 +160,7 @@ the event name is used to describe the transition.
160
160
161
161
```
162
162
163
-
164
-
## Event
163
+
## Events
165
164
166
165
An event is an external signal that something has happened.
167
166
They are send to a state machine and allow the state machine to react.
@@ -175,7 +174,7 @@ In `python-statemachine`, an event is specified as an attribute of the state mac
175
174
### Declaring events
176
175
177
176
The simplest way to declare an {ref}`event`is by assiging a transitions list to a name at the
178
-
State machine class level. The name will be converted to an {ref}`Event (class)`:
177
+
State machine class level. The name will be converted to an {ref}`Event`:
179
178
180
179
```py
181
180
>>>from statemachine import Event
@@ -197,7 +196,7 @@ True
197
196
You can also explict declare an {ref}`Event` instance, this helps IDEs to know that the event iscallableand also with transtation strings.
198
197
```
199
198
200
-
To declare an explicit event you must also import the {ref}`Event (class)`:
199
+
To declare an explicit event you must also import the {ref}`Event`:
201
200
202
201
```py
203
202
>>>from statemachine import Event
@@ -219,7 +218,7 @@ To declare an explicit event you must also import the {ref}`Event (class)`:
219
218
220
219
```
221
220
222
-
An {ref}`Event (class)` instance or an event id string can also be used as the `event` parameter of a {ref}`transition`. So you can mix these options as you need.
221
+
An {ref}`Event` instance or an event id string can also be used as the `event` parameter of a {ref}`transition`. So you can mix these options as you need.
@@ -293,22 +292,23 @@ An {ref}`Event (class)` instance or an event id string can also be used as the `
293
292
```
294
293
295
294
```{tip}
296
-
Avoid mixing these options within the same project; instead, choose the one that best serves your use case. Declaring events as strings has been the standard approach since the library’s inception and can be considered syntactic sugar, as the state machine metaclass will convert all events to {ref}`Event (class)` instances under the hood.
295
+
Avoid mixing these options within the same project; instead, choose the one that best serves your use case. Declaring events as strings has been the standard approach since the library’s inception and can be considered syntactic sugar, as the state machine metaclass will convert all events to {ref}`Event` instances under the hood.
297
296
298
297
```
299
298
300
299
```{note}
301
-
In order to allow the seamless upgrade from using strings to `Event` instances, the {ref}`Event (class)` inherits from`str`.
300
+
In order to allow the seamless upgrade from using strings to `Event` instances, the {ref}`Event` inherits from`str`.
302
301
303
302
Note that this is just an implementation detail and can change in the future.
304
303
305
-
>>>isinstance(TrafficLightMachine.cycle, str)
306
-
True
304
+
>>>isinstance(TrafficLightMachine.cycle, str)
305
+
True
307
306
308
307
```
309
308
310
309
311
310
```{warning}
311
+
312
312
An {ref}`Event` declared as string will have its `name`set equal to its `id`. This isfor backward compatibility when migrating from previous versions.
313
313
314
314
In the next major release, `Event.name` will default to a capitalized version of `id` (i.e., `Event.id.replace("_", "").capitalize()`).
0 commit comments