我正在尝试使用pytransitions版本0.6.4的内部转换功能,但它对我不起作用。我收到错误
File "/python3.6/site-packages/transitions/core.py", line 720, in set_state
mod.state = state.name
AttributeError: 'NoneType' object has no attribute 'name'
我在这里调整了代码
mod.state = state.name if state else mod.state
然后它在其他地方出错了,所以我不确定这个功能是否有问题。
我正在用一个dicts列表初始化机器,因为它的转换是kwarg。一个dict的例子是,我相信这是正确的格式。
{'trigger': 'cartridge_present_switch', 'source': 'post', 'dest': None, 'before': 'on_cartridge_present_switch'}
虽然文档仅显示使用add_transition方法添加的转换,如下所示
machine.add_transition('internal', ['liquid', 'gas'], None, after='change_shape')
有人可以对内部过渡功能发表评论吗?
谢谢,约翰
答案 0 :(得分:0)
在0.6.6
中引入了内部转场,如Changelog所示。传递给构造函数的转换也将使用add_transition(s)
进行处理。更准确地说,分配给构造函数的关键字transitions
的值只是“按原样”传递给add_transitions
,其中参数被解包,并且每个过渡单独调用add_transition
。当然,这个过程不是防弹的。然而,如果您遇到add_transition
有效但通过构造函数传递的问题,我建议您再次检查参数列表。
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