当前位置: 首页>>代码示例>>Python>>正文


Python Fysom.wait方法代码示例

本文整理汇总了Python中fysom.Fysom.wait方法的典型用法代码示例。如果您正苦于以下问题:Python Fysom.wait方法的具体用法?Python Fysom.wait怎么用?Python Fysom.wait使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在fysom.Fysom的用法示例。


在下文中一共展示了Fysom.wait方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: BaseExitSection

# 需要导入模块: from fysom import Fysom [as 别名]
# 或者: from fysom.Fysom import wait [as 别名]
class BaseExitSection(object):

    def __init__(self):
        self._can_depart = False
        self._fsm = Fysom(
            initial=Section.IDLE,
            final=Section.IDLE,
            events=[
                ('wait', Section.IDLE, Section.WAITING),
                ('depart', Section.IDLE, Section.DEPARTING),
                ('depart', Section.WAITING, Section.DEPARTING),
                ('resolve', Section.DEPARTING, Section.IDLE)],
            callbacks=[
                ('onbeforedepart', self._onbeforedepart),
                ('ondepart', self.__ondepart__)])

    @property
    def status(self):
        return self._fsm.current

    @property
    def can_depart(self):
        return self._can_depart

    def request_depart(self):
        try:
            self._fsm.depart()
        except Canceled:
            self._fsm.wait()

    def depart(self):
        self._can_depart = True
        if self.status == Section.WAITING:
            self._fsm.depart()

    def departed(self):
        self._fsm.resolve()

    def _onbeforedepart(self, e):
        return self.can_depart

    def __ondepart__(self, e):
        self._can_depart = False
        self._ondepart(e)

    def _ondepart(self, e):
        pass
开发者ID:stefan-walluhn,项目名称:RPi.TC,代码行数:49,代码来源:__init__.py

示例2: FysomCallbackTests

# 需要导入模块: from fysom import Fysom [as 别名]
# 或者: from fysom.Fysom import wait [as 别名]
class FysomCallbackTests(unittest.TestCase):
    def before_foo(self, e):
        self.before_foo_event = e
        self.fired_callbacks.append('before_foo')

    def before_bar(self, e):
        self.before_bar_event = e
        self.fired_callbacks.append('before_bar')

    def before_wait(self, e):
        self.before_wait_event = e
        self.fired_callbacks.append('before_wait')
        return False

    def on_foo(self, e):
        self.foo_event = e
        self.fired_callbacks.append('after_foo')

    def on_bar(self, e):
        self.bar_event = e
        self.fired_callbacks.append('after_bar')

    def on_baz(self, e):
        raise ValueError('Baz-inga!')

    def on_enter_fooed(self, e):
        self.enter_fooed_event = e

    def on_enter_bared(self, e):
        self.enter_bared_event = e

    def on_leave_sleeping(self, e):
        self.leave_sleeping_event = e

    def on_leave_fooed(self, e):
        self.leave_fooed_event = e

    def setUp(self):
        self.fired_callbacks = []

        self.fsm = Fysom({
            'initial': 'sleeping',
            'events': [
                {'name': 'foo', 'src': 'sleeping', 'dst': 'fooed'},
                {'name': 'bar', 'src': 'fooed', '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,
                'onleavesleeping': self.on_leave_sleeping,
                'onleavefooed': self.on_leave_fooed
            }
        })

    def test_onafter_event_callbacks_should_fire_with_keyword_arguments_when_events_occur(self):
        self.fsm.foo(attribute='test')
        self.assertTrue(hasattr(self, 'foo_event'), 'Callback on_foo did not fire.')
        self.assertTrue(self.foo_event is not None)
        self.assertEqual(self.foo_event.attribute, 'test')

        self.fsm.bar(id=123)
        self.assertTrue(hasattr(self, 'bar_event'), 'Callback on_bar did not fire.')
        self.assertTrue(self.bar_event is not None)
        self.assertEqual(self.bar_event.id, 123)

    def test_onafter_event_callbacks_raising_exceptions_should_not_be_eaten(self):
        self.fsm.foo()
        self.fsm.bar()
        self.assertRaises(ValueError, self.fsm.baz)

    def test_onbefore_event_callbacks_should_fire_before_onafter_callbacks_with_keyword_arguments_when_events_occur(
            self):
        self.fsm.foo(attribute='test')
        self.assertTrue(hasattr(self, 'before_foo_event'), 'Callback onbeforefoo did not fire.')
        self.assertTrue(self.before_foo_event is not None)
        self.assertEqual(self.before_foo_event.attribute, 'test')
        self.fsm.bar(id=123)
        self.assertTrue(hasattr(self, 'before_bar_event'), 'Callback onbeforebar did not fire.')
        self.assertTrue(self.before_bar_event is not None)
        self.assertEqual(self.before_bar_event.id, 123)
        self.assertEqual(['before_foo', 'after_foo', 'before_bar', 'after_bar'], self.fired_callbacks)

    def test_fsm_cancel_transition_when_onbefore_event_callbacks_return_false(self):
        self.fsm.wait()
        self.assertEqual(self.fsm.current, 'sleeping')


    def test_onenter_state_callbacks_should_fire_with_keyword_arguments_when_state_transitions_occur(self):
        self.fsm.foo(attribute='test')
        self.assertTrue(hasattr(self, 'enter_fooed_event'), 'Callback onenterfooed did not fire.')
        self.assertTrue(self.enter_fooed_event is not None)
        self.assertEqual(self.enter_fooed_event.attribute, 'test')
#.........这里部分代码省略.........
开发者ID:Subito-it,项目名称:fysom,代码行数:103,代码来源:fysom_callback_tests.py


注:本文中的fysom.Fysom.wait方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。