本文整理汇总了Python中fysom.Fysom类的典型用法代码示例。如果您正苦于以下问题:Python Fysom类的具体用法?Python Fysom怎么用?Python Fysom使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Fysom类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_pickling
def test_pickling(self):
fsm = Fysom({
'initial': 'green',
'events': [
{'name': 'warn', 'src': 'green', 'dst': 'yellow'},
{'name': 'panic', 'src': 'yellow', 'dst': 'red'},
{'name': 'calm', 'src': 'red', 'dst': 'yellow'},
{'name': 'clear', 'src': 'yellow', 'dst': 'green'}
]
})
pickled = dumps(fsm)
assert pickled
fsm = loads(pickled)
self.assertTrue(isinstance(fsm, Fysom))
self.assertEquals('green', fsm.current)
fsm.warn()
pickled = dumps(fsm)
assert pickled
fsm = loads(pickled)
self.assertEquals('yellow', fsm.current)
示例2: __init__
def __init__(self, name, runner, references, config, **kwargs):
""" Create a PrePostTask.
@param str name: unique name of the task
@param object runner: TaskRunner that manages this task
@param dict references: contains references to all required modules
@param dict config: configuration parameter dictionary
"""
_default_callbacks = {'onprerun': self._pre, 'onpostrun': self._post}
_stateList = {
'initial': 'stopped',
'events': [
{'name': 'prerun', 'src': 'stopped', 'dst': 'paused'},
{'name': 'postrun', 'src': 'paused', 'dst': 'stopped'}
],
'callbacks': _default_callbacks
}
if 'PyQt5' in sys.modules:
super().__init__(cfg=_stateList, **kwargs)
else:
QtCore.QObject.__init__(self)
Fysom.__init__(self, _stateList)
self.lock = Mutex()
self.name = name
self.runner = runner
self.ref = references
self.config = config
示例3: test_onchangestate_should_fire_for_all_state_changes
def test_onchangestate_should_fire_for_all_state_changes(self):
def on_change_state(e):
self.current_event = e
fsm = Fysom({
'initial': 'foo',
'events': [
{'name': 'footobar', 'src': 'foo', 'dst': 'bar'},
{'name': 'bartobaz', 'src': 'bar', 'dst': 'baz'},
],
'callbacks': {
'onchangestate': on_change_state
}
})
fsm.footobar(id=123)
self.assertEqual(self.current_event.event, 'footobar')
self.assertEqual(self.current_event.src, 'foo')
self.assertEqual(self.current_event.dst, 'bar')
self.assertEqual(self.current_event.id, 123)
self.assertTrue(self.current_event.fsm is fsm)
fsm.bartobaz('positional', named_attribute='test')
self.assertEqual(self.current_event.event, 'bartobaz')
self.assertEqual(self.current_event.src, 'bar')
self.assertEqual(self.current_event.dst, 'baz')
self.assertEqual(self.current_event.named_attribute, 'test')
self.assertEqual(self.current_event.args[0], 'positional')
self.assertTrue(self.current_event.fsm is fsm)
示例4: FysomRepeatedAfterEventCallbackTests
class FysomRepeatedAfterEventCallbackTests(unittest.TestCase):
def setUp(self):
self.fired = []
def record_event(event):
self.fired.append(event.msg)
return 42
self.fsm = Fysom({
'initial': 'notcalled',
'events': [
{'name': 'multiple', 'src': 'notcalled', 'dst': 'called'},
{'name': 'multiple', 'src': 'called', 'dst': 'called'},
],
'callbacks': {
'onaftermultiple': record_event
}
})
def test_should_fire_onafter_event_repeatedly(self):
self.fsm.multiple(msg="first")
self.fsm.multiple(msg="second")
self.assertEqual(self.fired, ["first", "second"])
示例5: FysomEventWithStateCallbackTests
class FysomEventWithStateCallbackTests(unittest.TestCase):
def setUp(self):
self.fired_nocalled_to_called = []
self.fired_called_to_called = []
def record_event_nocalled_to_called(event):
self.fired_nocalled_to_called.append(event.msg)
return 42
def record_event_called_to_called(event):
self.fired_called_to_called.append(event.msg)
return 42
self.fsm = Fysom({
'initial': 'notcalled',
'events': [
{'name': 'multiple', 'src': 'notcalled', 'dst': 'called'},
{'name': 'multiple', 'src': 'called', 'dst': 'called'},
],
'callbacks': {
'onaftermultiple_notcalled_called': record_event_nocalled_to_called,
'onaftermultiple_called_called': record_event_called_to_called
}
})
def test_should_fire_on_event(self):
self.fsm.multiple(msg="first")
self.fsm.multiple(msg="second")
self.assertEqual(self.fired_nocalled_to_called, ["first"])
self.assertEqual(self.fired_called_to_called, ["second"])
示例6: __init__
def __init__(self, callbacks_dict=None, interactive_callbacks=None):
self.__NOT_IMPLEMENTED_ERROR = "Classes that inherit from KnittingPlugin should implment {0}"
self.interactive_callbacks = {}
if interactive_callbacks is None:
self.register_interactive_callbacks({
"info": BaseKnittingPlugin.__interactive_info,
"user_action": BaseKnittingPlugin.__interactive_info,
"warning": BaseKnittingPlugin.__interactive_warn,
"error": BaseKnittingPlugin.__interactive_error,
"progress": BaseKnittingPlugin.__log_progress
})
else:
self.register_interactive_callbacks(interactive_callbacks)
if callbacks_dict is None:
callbacks_dict = {
'onknit': self.onknit,
'onconfigure': self.onconfigure,
'onfinish': self.onfinish,
}
Fysom.__init__(self, {
'initial': 'activated',
'events': [ # TODO: add more states for handling error management.
{'name': 'configure', 'src': 'activated', 'dst': 'configured'},
{'name': 'configure', 'src': 'configured', 'dst': 'configured'},
{'name': 'configure', 'src': 'finished', 'dst': 'configured'},
{'name': 'configure', 'src': 'error', 'dst': 'configured'},
{'name': 'knit', 'src': 'configured', 'dst': 'knitting'},
{'name': 'finish', 'src': 'knitting', 'dst': 'finished'},
{'name': 'fail', 'src': 'knitting', 'dst': 'error'}],
'callbacks': callbacks_dict
})
示例7: create_voting_fsm
def create_voting_fsm(tracking_id,
vote,
broadcast_vote,
spawn_yadtshell,
fold,
cleanup_fsm):
fsm = Fysom({
'initial': 'negotiating',
'events': [
{'name': 'call', 'src': 'negotiating', 'dst': 'negotiating'},
{'name': 'fold', 'src': 'negotiating', 'dst': 'finish'},
{'name': 'showdown', 'src': 'negotiating', 'dst': 'spawning'},
{'name': 'spawned', 'src': 'spawning', 'dst': 'finish'},
{'name': 'showdown', 'src': 'finish', 'dst': 'finish'}
],
'callbacks': {
'onnegotiating': broadcast_vote,
'onspawning': spawn_yadtshell,
'onfinish': cleanup_fsm,
'onfold': fold,
}
})
fsm.tracking_id = tracking_id
fsm.vote = vote
return fsm
示例8: test_init_kwargs_only
def test_init_kwargs_only(self):
fsm = Fysom(initial='green',
events=[('panic', 'green', 'red'),
('calm', 'red', 'green')])
self.assertEqual(fsm.current, "green")
fsm.panic()
self.assertEqual(fsm.current, "red")
fsm.calm()
self.assertEqual(fsm.current, "green")
示例9: test_deferred_initial_state_should_be_none_then_state
def test_deferred_initial_state_should_be_none_then_state(self):
fsm = Fysom({
'initial': {'state': 'green', 'event': 'init', 'defer': True},
'events': [
{'name': 'panic', 'src': 'green', 'dst': 'red'},
{'name': 'calm', 'src': 'red', 'dst': 'green'},
]
})
self.assertEqual(fsm.current, 'none')
fsm.init()
self.assertEqual(fsm.current, 'green')
示例10: test_kwargs_override_cfg
def test_kwargs_override_cfg(self):
fsm = Fysom({
'initial': 'green',
'events': [
{'name': 'panic', 'src': 'green', 'dst': 'red'},
{'name': 'calm', 'src': 'red', 'dst': 'green'},
]},
# override initial state and calm event
initial='red', events=[('calm', 'red', 'black')])
self.assertEqual(fsm.current, "red")
fsm.calm()
self.assertEqual(fsm.current, "black")
示例11: __init__
def __init__(self, plugin_id, current_state):
Fysom.__init__(self, {
'initial': current_state,
'events': events,
'callbacks': {
'oninstall': self.install,
'oninitialize': self.initialize
}
})
self.plugin_id = plugin_id
self.current_state = current_state
self.onchangestate = self._onchangestate
示例12: test_goto
def test_goto(self):
fsm = Fysom({
'initial': 'green',
'events': [
{'name': 'warn', 'src': 'green', 'dst': 'yellow'},
{'name': 'panic', 'src': 'yellow', 'dst': 'red'},
{'name': 'panic', 'src': 'green', 'dst': 'red'},
{'name': 'calm', 'src': 'red', 'dst': 'yellow'},
{'name': 'clear', 'src': 'yellow', 'dst': 'green'}
]
})
self.assertRaises(MultiplePossibilitesFound, fsm.next)
self.assertEqual('red', fsm.goto('panic'))
示例13: test_gc_should_not_break_callback
def test_gc_should_not_break_callback(self):
class MyTestObject(object):
def __init__(self):
self._states = []
self._fsm = None
def warn(self):
self._fsm.warn()
def panic(self):
self._fsm.panic()
def calm(self):
self._fsm.calm()
def clear(self):
self._fsm.clear()
def _on_green(self, *args, **kwargs):
self._states.append('green')
def _on_yellow(self, *args, **kwargs):
self._states.append('yellow')
def _on_red(self, *args, **kwargs):
self._states.append('red')
obj = MyTestObject()
fsm = Fysom({
'initial': 'green',
'events': [
{'name': 'warn', 'src': 'green', 'dst': 'yellow'},
{'name': 'panic', 'src': 'yellow', 'dst': 'red'},
{'name': 'calm', 'src': 'red', 'dst': 'yellow'},
{'name': 'clear', 'src': 'yellow', 'dst': 'green'}
],
'callbacks': {
'ongreen': obj._on_green,
'onyellow': obj._on_yellow,
'onred': obj._on_red
}
})
obj._fsm = fsm
obj.warn()
obj.clear()
del obj
fsm.warn()
示例14: __init__
def __init__(self,
left_button=None,
left_led=None,
right_button=None,
right_led=None,
track=None,
strawberry_factory=None,
chocolate_factory=None):
self.buttons_locked = False
self.micro_lock = threading.Lock()
self.pending_cookies = Queue.Queue()
self.left_button = left_button
self.left_button.register(self.button_pressed)
self.left_led = left_led
self.left_led.off()
self.right_button = right_button
self.right_button.register(self.button_pressed)
self.right_led = right_led
self.right_led.off()
self.track = track
self.strawberry_factory = strawberry_factory
self.chocolate_factory = chocolate_factory
self.evolution = Evolution()
self._fsm = Fysom(
initial='waiting',
events=[('reproduce', 'waiting', 'waiting')],
callbacks=[('on_state_waiting', self._unlock),
('onreproduce', self._reproduce)])
示例15: setUp
def setUp(self):
self.fired_callbacks = []
self.fsm = Fysom({
'initial': 'sleeping',
'events': [
{'name': 'foo', 'src': 'sleeping', 'dst': 'fooed'},
{'name': 'foo', 'src': 'fooed', 'dst': 'fooed'},
{'name': 'bar', 'src': 'fooed', 'dst': 'bared'},
{'name': 'bar', 'src': 'bared', 'dst': 'bared'},
{'name': 'baz', 'src': 'bared', 'dst': 'bazed'},
{'name': 'wait', 'src': 'sleeping', 'dst': 'waiting'}
],
'callbacks': {
'onfoo': self.on_foo,
'onbar': self.on_bar,
'onbaz': self.on_baz,
'onbeforefoo': self.before_foo,
'onbeforebar': self.before_bar,
'onbeforewait': self.before_wait,
'onenterfooed': self.on_enter_fooed,
'onenterbared': self.on_enter_bared,
'onreenterfooed': self.on_reenter_fooed,
'onreenterbared': self.on_reenter_bared,
'onleavesleeping': self.on_leave_sleeping,
'onleavefooed': self.on_leave_fooed
}
})