本文整理汇总了Python中transitions.MachineError方法的典型用法代码示例。如果您正苦于以下问题:Python transitions.MachineError方法的具体用法?Python transitions.MachineError怎么用?Python transitions.MachineError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类transitions
的用法示例。
在下文中一共展示了transitions.MachineError方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: execute
# 需要导入模块: import transitions [as 别名]
# 或者: from transitions import MachineError [as 别名]
def execute(self) -> response.Response:
"""Called when the user specifies an intent for this skill"""
intent = self.attributes.intent
previous_state = self.state
# backup attributes in case of invalid FSM transition
attributes_backup = self.attributes
try:
# trigger is added by transitions library
self.trigger(intent)
current_state = self.state
logger.info(f"Changed from {previous_state} to {current_state} through {intent}")
self.attributes.state = current_state
return self.get_current_state_response()
except MachineError as exception:
logger.error(str(exception))
# reset attributes
self.states.attributes = attributes_backup
return response.NOT_UNDERSTOOD
示例2: test_queued
# 需要导入模块: import transitions [as 别名]
# 或者: from transitions import MachineError [as 别名]
def test_queued(self):
states = ['A', 'B', 'C', 'D']
# Define with list of dictionaries
def change_state(machine):
self.assertEqual(machine.state, 'A')
if machine.has_queue:
machine.run(machine=machine)
self.assertEqual(machine.state, 'A')
else:
with self.assertRaises(MachineError):
machine.run(machine=machine)
transitions = [
{'trigger': 'walk', 'source': 'A', 'dest': 'B', 'before': change_state},
{'trigger': 'run', 'source': 'B', 'dest': 'C'},
{'trigger': 'sprint', 'source': 'C', 'dest': 'D'}
]
m = Machine(states=states, transitions=transitions, initial='A')
m.walk(machine=m)
self.assertEqual(m.state, 'B')
m = Machine(states=states, transitions=transitions, initial='A', queued=True)
m.walk(machine=m)
self.assertEqual(m.state, 'C')
示例3: test_queued_errors
# 需要导入模块: import transitions [as 别名]
# 或者: from transitions import MachineError [as 别名]
def test_queued_errors(self):
def before_change(machine):
if machine.has_queue:
machine.to_A(machine)
machine._queued = False
def after_change(machine):
machine.to_C(machine)
def failed_transition(machine):
raise ValueError('Something was wrong')
states = ['A', 'B', 'C']
transitions = [{'trigger': 'do', 'source': '*', 'dest': 'C', 'before': failed_transition}]
m = Machine(states=states, transitions=transitions, queued=True,
before_state_change=before_change, after_state_change=after_change)
with self.assertRaises(MachineError):
m.to_B(machine=m)
with self.assertRaises(ValueError):
m.do(machine=m)
示例4: test_remove_transition
# 需要导入模块: import transitions [as 别名]
# 或者: from transitions import MachineError [as 别名]
def test_remove_transition(self):
self.stuff.machine.add_transition('go', ['A', 'B', 'C'], 'D')
self.stuff.machine.add_transition('walk', 'A', 'B')
self.stuff.go()
self.assertEqual(self.stuff.state, 'D')
self.stuff.to_A()
self.stuff.machine.remove_transition('go', source='A')
with self.assertRaises(MachineError):
self.stuff.go()
self.stuff.machine.add_transition('go', 'A', 'D')
self.stuff.walk()
self.stuff.go()
self.assertEqual(self.stuff.state, 'D')
self.stuff.to_C()
self.stuff.machine.remove_transition('go', dest='D')
self.assertFalse(hasattr(self.stuff, 'go'))
示例5: main
# 需要导入模块: import transitions [as 别名]
# 或者: from transitions import MachineError [as 别名]
def main():
p = Process("p1")
print("initial state: {}".format(p.state))
old = p.state
print("\nwake_up trigger")
p.wake_up()
print("{} -> {}".format(old, p.state))
old = p.state
print("\nstart trigger")
p.start()
print("{} -> {}".format(old, p.state))
old = p.state
print("\nrandom trigger (stay in current state or go to terminated 50/50)")
p.random_trigger()
print("{} -> {}".format(old, p.state))
old = p.state
print("\ninterrupt trigger")
p.interrupt()
print("{} -> {}".format(old, p.state))
print('\nFrom "terminated" we cannot trigger a "start" event')
try:
p.start()
except MachineError as e:
print(e)
示例6: testUnconnectedDisconnect
# 需要导入模块: import transitions [as 别名]
# 或者: from transitions import MachineError [as 别名]
def testUnconnectedDisconnect(self):
with self.assertRaises(MachineError):
self.stateMachine.disconnect()
示例7: testUnconnectedSelect
# 需要导入模块: import transitions [as 别名]
# 或者: from transitions import MachineError [as 别名]
def testUnconnectedSelect(self):
with self.assertRaises(MachineError):
self.stateMachine.select()
示例8: testUnconnectedDeselect
# 需要导入模块: import transitions [as 别名]
# 或者: from transitions import MachineError [as 别名]
def testUnconnectedDeselect(self):
with self.assertRaises(MachineError):
self.stateMachine.deselect()
示例9: testUnconnectedTimeoutT7
# 需要导入模块: import transitions [as 别名]
# 或者: from transitions import MachineError [as 别名]
def testUnconnectedTimeoutT7(self):
with self.assertRaises(MachineError):
self.stateMachine.timeoutT7()
示例10: testConnectedNotSelectedConnect
# 需要导入模块: import transitions [as 别名]
# 或者: from transitions import MachineError [as 别名]
def testConnectedNotSelectedConnect(self):
self.stateMachine.connect()
with self.assertRaises(MachineError):
self.stateMachine.connect()
示例11: testConnectedSelectedConnect
# 需要导入模块: import transitions [as 别名]
# 或者: from transitions import MachineError [as 别名]
def testConnectedSelectedConnect(self):
self.stateMachine.connect()
self.stateMachine.select()
with self.assertRaises(MachineError):
self.stateMachine.connect()
示例12: testConnectedSelectedSelect
# 需要导入模块: import transitions [as 别名]
# 或者: from transitions import MachineError [as 别名]
def testConnectedSelectedSelect(self):
self.stateMachine.connect()
self.stateMachine.select()
with self.assertRaises(MachineError):
self.stateMachine.select()
示例13: testConnectedSelectedTimeoutT7
# 需要导入模块: import transitions [as 别名]
# 或者: from transitions import MachineError [as 别名]
def testConnectedSelectedTimeoutT7(self):
self.stateMachine.connect()
self.stateMachine.select()
with self.assertRaises(MachineError):
self.stateMachine.timeoutT7()
# tests for callbacks
示例14: shutdown
# 需要导入模块: import transitions [as 别名]
# 或者: from transitions import MachineError [as 别名]
def shutdown(self):
"""
Stop broker instance.
Closes all connected session, stop listening on network socket and free resources.
"""
try:
self._sessions = dict()
self._subscriptions = dict()
self._retained_messages = dict()
self.transitions.shutdown()
except (MachineError, ValueError) as exc:
# Backwards compat: MachineError is raised by transitions < 0.5.0.
self.logger.debug("Invalid method call at this moment: %s" % exc)
raise BrokerException("Broker instance can't be stopped: %s" % exc)
# Fire broker_shutdown event to plugins
yield from self.plugins_manager.fire_event(EVENT_BROKER_PRE_SHUTDOWN)
# Stop broadcast loop
if self._broadcast_task:
self._broadcast_task.cancel()
if self._broadcast_queue.qsize() > 0:
self.logger.warning("%d messages not broadcasted" % self._broadcast_queue.qsize())
for listener_name in self._servers:
server = self._servers[listener_name]
yield from server.close_instance()
self.logger.debug("Broker closing")
self.logger.info("Broker closed")
yield from self.plugins_manager.fire_event(EVENT_BROKER_POST_SHUTDOWN)
self.transitions.stopping_success()
示例15: test_prepare
# 需要导入模块: import transitions [as 别名]
# 或者: from transitions import MachineError [as 别名]
def test_prepare(self):
m = Machine(Stuff(), states=['A', 'B', 'C'], initial='A')
m.add_transition('move', 'A', 'B', prepare='increase_level')
m.add_transition('move', 'B', 'C', prepare='increase_level')
m.add_transition('move', 'C', 'A', prepare='increase_level', conditions='this_fails')
m.add_transition('dont_move', 'A', 'C', prepare='increase_level')
m.prepare_move('increase_level')
m.model.move()
self.assertEqual(m.model.state, 'B')
self.assertEqual(m.model.level, 3)
m.model.move()
self.assertEqual(m.model.state, 'C')
self.assertEqual(m.model.level, 5)
# State does not advance, but increase_level still runs
m.model.move()
self.assertEqual(m.model.state, 'C')
self.assertEqual(m.model.level, 7)
# An invalid transition shouldn't execute the callback
try:
m.model.dont_move()
except MachineError as e:
self.assertTrue("Can't trigger event" in str(e))
self.assertEqual(m.model.state, 'C')
self.assertEqual(m.model.level, 7)