def test_with_other_functions(self): def myfunc1(): return myfunc2 def myfunc2(): return myfunc3 def myfunc3(): return 1 self.assertEqual(func.trampoline(myfunc1), 1)
do_start = State(name='start', function=setup, on_success='do_first_step', on_failure='do_stop') do_first_step = State(name='first_step', function=first_step, on_success='do_second_step', on_failure='do_stop') do_second_step = State(name='second_step', function=second_step, on_success='do_wait', on_failure='do_stop') do_wait = State(name='wait', function=wait, on_success='do_first_step', on_failure='do_stop') do_stop = State(name='stop', function=stop, on_success=None, on_failure=None) def state_machine(state): if state.function(): new_state = globals().get(state.on_success, None) else: new_state = globals().get(state.on_failure, None) if new_state: return partial(state_machine, new_state) if __name__ == '__main__': trampoline(state_machine, do_start)
def test_with_args(self): def myfunc(a): return a self.assertEqual(func.trampoline(myfunc, 1), 1)
def test_no_func(self): # Test that if no function is given, trampoline is basically a glorified # apply. def myfunc(): return 1 self.assertEqual(func.trampoline(myfunc), 1)
print 'First step...' return True def second_step(): print 'Second step...' return True def wait(): print 'Waiting...' return True def stop(): print 'Stopping...' return True State = collections.namedtuple('State', 'name function on_success on_failure') do_start = State(name='start', function=setup, on_success='do_first_step', on_failure='do_stop') do_first_step = State(name='first_step', function=first_step, on_success='do_second_step', on_failure='do_stop') do_second_step = State(name='second_step', function=second_step, on_success='do_wait', on_failure='do_stop') do_wait = State(name='wait', function=wait, on_success='do_first_step', on_failure='do_stop') do_stop = State(name='stop', function=stop, on_success=None, on_failure=None) def state_machine(state): if state.function(): new_state = globals().get(state.on_success, None) else: new_state = globals().get(state.on_failure, None) if new_state: return partial(state_machine, new_state) if __name__ == '__main__': trampoline(state_machine, do_start)