bjorn
bjorn

Reputation: 126

Set current state using python-statemachine

I'm using the python-statemachine library to (surprise) implement a state machine. In our application, after each event the state is saved to disk.

What I want to do is assign the current value of the state from disk to the state machine on instantiation. Something like:

sm = StateMachine(current_state='saved_state')

It seems like there should be an API for this but I couldn't find anything in the docs.

I did find a way by accessing the current_state_value property directly. I posted that below as a possible answer but I don't love it. Better answers welcome!

Upvotes: 1

Views: 125

Answers (1)

bjorn
bjorn

Reputation: 126

One way to do this is assign to the current_state_value property directly, like so:

from statemachine import StateMachine, State

class MyStateMachine(StateMachine):
    first_state = State('First State', initial=True)
    second_state = State('Second State')

sm = MyStateMachine()
sm.current_state_value = sm.second_state.value

Then:

print(sm.current_state.name)
Second State

Upvotes: 0

Related Questions