Ejemplo n.º 1
0
def test_Example5():
    statemachine = Example5(XIter(testdata), init_state='low')
    print(statemachine.__dict__)

    # Calling statemachine() should iterate over the test data and return the
    # processed values as list::

    print(statemachine())  # only printed if something goes wrong
    # reset the data iterator as it is "used up" now
    statemachine.data = XIter(testdata)
    assert statemachine() == [
        'l(1)', 'l(2)', 'l(3)', 'h(4)', 'h(5)', 'h(4)', None, 'l(3)', 'l(2)',
        'l(1)'
    ]
Ejemplo n.º 2
0
    def __iter__(self):
        """Generate and return an iterator

        * convert `data` to an iterator queue
        * convert the state generators into iterators
        * (re) set the state_handler attribute to the init-state
        * pass control to the active states state_handler
          which should call and process next(self.data_iterator.next)
        """
        self.data_iterator = XIter(self.data)  # queue with `appendleft` method
        self._initialize_state_generators()
        self.state_handler = getattr(self, self.init_state)
        # now start the iteration
        while True:
            yield self.state_handler()