@@ -26,6 +26,8 @@ when something changes, and are not bound to a specific state or event:
2626
2727- ` after_transition() `
2828
29+ - ` finalize() `
30+
2931The following example offers an overview of the "generic" callbacks available:
3032
3133``` py
@@ -54,6 +56,10 @@ The following example offers an overview of the "generic" callbacks available:
5456...
5557... def after_transition (self , event , state ):
5658... print (f " After ' { event} ', on the ' { state.id} ' state. " )
59+ ...
60+ ... def finalize (self , event , source , target , state ):
61+ ... print (f " Finalizing transition { event} from { source.id} to { target.id} " )
62+ ... print (f " Current state: { state.id} " )
5763
5864
5965>> > sm = ExampleStateMachine() # On initialization, the machine run a special event `__initial__`
@@ -65,6 +71,8 @@ Exiting 'initial' state from 'loop' event.
6571On ' loop' , on the ' initial' state.
6672Entering ' initial' state from ' loop' event.
6773After ' loop' , on the ' initial' state.
74+ Finalizing transition loop from initial to initial
75+ Current state: initial
6876[' before_transition_return' , ' on_transition_return' ]
6977
7078>> > sm.go()
@@ -73,6 +81,8 @@ Exiting 'initial' state from 'go' event.
7381On ' go' , on the ' initial' state.
7482Entering ' final' state from ' go' event.
7583After ' go' , on the ' final' state.
84+ Finalizing transition go from initial to final
85+ Current state: final
7686[' before_transition_return' , ' on_transition_return' ]
7787
7888```
@@ -101,14 +111,14 @@ model, using the patterns:
101111>> > from statemachine import StateMachine, State
102112
103113>> > class ExampleStateMachine (StateMachine ):
104- ... initial = State(initial = True )
114+ ... initial = State(' Initial ' , initial = True )
105115...
106116... loop = initial.to.itself()
107117...
108- ... def on_enter_initial (self ):
118+ ... def on_enter_Initial (self ):
109119... pass
110120...
111- ... def on_exit_initial (self ):
121+ ... def on_exit_Initial (self ):
112122... pass
113123
114124```
@@ -121,14 +131,14 @@ Use the `enter` or `exit` params available on the `State` constructor.
121131>> > from statemachine import StateMachine, State
122132
123133>> > class ExampleStateMachine (StateMachine ):
124- ... initial = State(initial = True , enter = " entering_initial " , exit = " leaving_initial " )
134+ ... initial = State(' Initial ' , initial = True , enter = " entering_Initial " , exit = " leaving_Initial " )
125135...
126136... loop = initial.to.itself()
127137...
128- ... def entering_initial (self ):
138+ ... def entering_Initial (self ):
129139... pass
130140...
131- ... def leaving_initial (self ):
141+ ... def leaving_Initial (self ):
132142... pass
133143
134144```
@@ -144,16 +154,16 @@ It's also possible to use an event name as action.
144154>> > from statemachine import StateMachine, State
145155
146156>> > class ExampleStateMachine (StateMachine ):
147- ... initial = State(initial = True )
157+ ... initial = State(' Initial ' , initial = True )
148158...
149159... loop = initial.to.itself()
150160...
151161... @ initial.enter
152- ... def entering_initial (self ):
162+ ... def entering_Initial (self ):
153163... pass
154164...
155165... @ initial.exit
156- ... def leaving_initial (self ):
166+ ... def leaving_Initial (self ):
157167... pass
158168
159169```
@@ -180,7 +190,7 @@ using the patterns:
180190>> > from statemachine import StateMachine, State
181191
182192>> > class ExampleStateMachine (StateMachine ):
183- ... initial = State(initial = True )
193+ ... initial = State(' Initial ' , initial = True )
184194...
185195... loop = initial.to.itself()
186196...
@@ -202,7 +212,7 @@ using the patterns:
202212>> > from statemachine import StateMachine, State
203213
204214>> > class ExampleStateMachine (StateMachine ):
205- ... initial = State(initial = True )
215+ ... initial = State(' Initial ' , initial = True )
206216...
207217... loop = initial.to.itself(before = " just_before" , on = " its_happening" , after = " loop_completed" )
208218...
@@ -230,7 +240,7 @@ The action will be registered for every {ref}`transition` in the list associated
230240>> > from statemachine import StateMachine, State
231241
232242>> > class ExampleStateMachine (StateMachine ):
233- ... initial = State(initial = True )
243+ ... initial = State(' Initial ' , initial = True )
234244...
235245... loop = initial.to.itself()
236246...
@@ -264,7 +274,7 @@ You can also declare an event while also adding a callback:
264274>> > from statemachine import StateMachine, State
265275
266276>> > class ExampleStateMachine (StateMachine ):
267- ... initial = State(initial = True )
277+ ... initial = State(' Initial ' , initial = True )
268278...
269279... @ initial.to.itself()
270280... def loop (self ):
@@ -346,6 +356,10 @@ Actions registered on the same group don't have order guaranties and are execute
346356 - `after_<event>()`, `after_transition()`
347357 - `destination`
348358 - Callbacks declared in the transition or event.
359+ * - Finalize
360+ - `finalize()`
361+ - `destination`
362+ - Guaranteed to run after every transition attempt, whether successful or failed.
349363
350364```
351365
@@ -368,7 +382,7 @@ defined explicitly. The following provides an example:
368382
369383``` py
370384>> > class ExampleStateMachine (StateMachine ):
371- ... initial = State(initial = True )
385+ ... initial = State(' Initial ' , initial = True )
372386...
373387... loop = initial.to.itself()
374388...
@@ -381,6 +395,9 @@ defined explicitly. The following provides an example:
381395... def on_loop (self ):
382396... return " On loop"
383397...
398+ ... def finalize (self ):
399+ ... # Finalize return values are not included in results
400+ ... return " Finalize"
384401
385402>> > sm = ExampleStateMachine()
386403
0 commit comments