-1

I'm trying to use the internal transitions feature of pytransitions version 0.6.4 but it is not working for me. I get an error

  File "/python3.6/site-packages/transitions/core.py", line 720, in set_state
mod.state = state.name
AttributeError: 'NoneType' object has no attribute 'name'

I tweaked the code here

mod.state = state.name if state else mod.state

but then it errored elsewhere so I'm not sure if this feature is known to have problems.

I'm initialising the machine with a list of dicts as its transitions kwarg. An example of one dict is and I believe this is the correct format.

{'trigger': 'cartridge_present_switch', 'source': 'post', 'dest': None, 'before': 'on_cartridge_present_switch'}

Although the docs only show a transition added with the add_transition method as follows

machine.add_transition('internal', ['liquid', 'gas'], None, after='change_shape')

Can anybody comment on the internal transitions feature?

Thanks, John

Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186

1 Answers1

0

Internal transitions have been introduced in 0.6.6 as seen in the Changelog. Transitions passed to the constructor will be processed with add_transition(s) as well. More precisely, the value(s) assigned to the constructor's keyword transitions is just passed 'as is' to add_transitions where parameters are unwrapped and add_transition is called for each transition individually. Of course this process is not bullet proof. Nevertheless if you face issues where add_transition works but passing via the constructor does not I'd suggest checking the parameter list once again.

from transitions import Machine

class Model():

    def model_callback(self):
        print("callback called")
        pass


states = ['A', 'B']
transitions = [{'trigger': 'internal', 'source': 'A', 
                'dest': None, 'after': 'model_callback'}]

# variant 1, passing transitions via constructor
model = Model()
m = Machine(model, states=['A', 'B'], transitions=transitions, initial='A')
model.internal()  # >>> callback called

# variant 2, passing transitions to `add_transitions` for batch processing
model = Model()
m = Machine(model, states=['A', 'B'], initial='A')
m.add_transitions(transitions)
model.internal()  # >>> callback called

# variant 3, passing transitions individually to `add_transition`
model = Model()
m = Machine(model, states=['A', 'B'], initial='A')
# accessing an element of transition list and map the keywords
# to the method parameters; basically what `add_transitions` is doing
m.add_transition(**transitions[0])
model.internal()  # >>> callback called
aleneum
  • 2,083
  • 12
  • 29