Python collections.Generator() Examples

The following are code examples for showing how to use collections.Generator(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: flake8-aaa   Author: jamescooke   File: test_check_act_assert_spacing.py    MIT License 6 votes vote down vote up
def test_comment_before_assert():
    """
    Comment before Assert passes
    """
    line_markers = LineMarkers(8 * [''], 5)
    line_markers[0] = LineType.func_def
    line_markers[1] = LineType.arrange  # x = 1
    line_markers[2] = LineType.arrange  # y = 2
    line_markers[3] = LineType.blank_line
    line_markers[4] = LineType.act  # result = x + y
    line_markers[5] = LineType.blank_line
    line_markers[6] = LineType.unprocessed  # Sum x and y
    line_markers[7] = LineType._assert  # assert result == 2

    result = line_markers.check_act_assert_spacing()

    assert isinstance(result, Generator)
    assert list(result) == [] 
Example 2
Project: flake8-aaa   Author: jamescooke   File: test_check_act_assert_spacing.py    MIT License 6 votes vote down vote up
def test_no_gap():
    """
    No gap raises - error points at act block because no spaces
    """
    line_markers = LineMarkers(6 * [''], 5)
    line_markers[0] = LineType.func_def
    line_markers[1] = LineType.arrange  # x = 1
    line_markers[2] = LineType.blank_line
    line_markers[3] = LineType.act  # result = x + 3
    line_markers[4] = LineType.unprocessed  # Sum do stuff
    line_markers[5] = LineType._assert  # assert result == 4

    result = line_markers.check_act_assert_spacing()

    assert isinstance(result, Generator)
    assert list(result) == [
        AAAError(
            line_number=10,
            offset=0,
            text='AAA04 expected 1 blank line before Assert block, found none',
        ),
    ] 
Example 3
Project: flake8-aaa   Author: jamescooke   File: test_check_act_assert_spacing.py    MIT License 6 votes vote down vote up
def test_too_big_gap():
    """
    Multiple BL raises. First extra line is pointed to
    """
    line_markers = LineMarkers(8 * [''], 5)
    line_markers[0] = LineType.func_def
    line_markers[1] = LineType.arrange  # x = 1
    line_markers[2] = LineType.blank_line
    line_markers[3] = LineType.unprocessed  # Sum do stuff
    line_markers[4] = LineType.act  # result = x + 3
    line_markers[5] = LineType.blank_line
    line_markers[6] = LineType.blank_line
    line_markers[7] = LineType._assert  # assert result == 4

    result = line_markers.check_act_assert_spacing()

    assert isinstance(result, Generator)
    assert list(result) == [
        AAAError(
            line_number=12,
            offset=0,
            text='AAA04 expected 1 blank line before Assert block, found 2',
        ),
    ] 
Example 4
Project: flake8-aaa   Author: jamescooke   File: test_check_arrange_act_spacing.py    MIT License 6 votes vote down vote up
def test_comment_before_act():
    """
    Comment before Act passes
    """
    line_markers = LineMarkers(8 * [''], 5)
    line_markers[0] = LineType.func_def
    line_markers[1] = LineType.arrange  # x = 1
    line_markers[2] = LineType.arrange  # y = 2
    line_markers[3] = LineType.blank_line
    line_markers[4] = LineType.unprocessed  # Sum x and y
    line_markers[5] = LineType.act  # result = x + y
    line_markers[6] = LineType.blank_line
    line_markers[7] = LineType._assert  # assert result == 2

    result = line_markers.check_arrange_act_spacing()

    assert isinstance(result, Generator)
    assert list(result) == [] 
Example 5
Project: flake8-aaa   Author: jamescooke   File: test_check_arrange_act_spacing.py    MIT License 6 votes vote down vote up
def test_no_gap():
    """
    No gap raises - error points at act block
    """
    line_markers = LineMarkers(6 * [''], 5)
    line_markers[0] = LineType.func_def
    line_markers[1] = LineType.arrange  # x = 1
    line_markers[2] = LineType.unprocessed  # Sum do stuff
    line_markers[3] = LineType.act  # result = x + 3
    line_markers[4] = LineType.blank_line
    line_markers[5] = LineType._assert  # assert result == 4

    result = line_markers.check_arrange_act_spacing()

    assert isinstance(result, Generator)
    result_list = list(result)
    assert len(result_list) == 1
    assert result_list[0] == AAAError(
        line_number=8,
        offset=0,
        text='AAA03 expected 1 blank line before Act block, found none',
    ) 
Example 6
Project: flake8-aaa   Author: jamescooke   File: test_check_arrange_act_spacing.py    MIT License 6 votes vote down vote up
def test_too_big_gap():
    """
    Multiple BL raises. Act block is pointed to.
    """
    line_markers = LineMarkers(8 * [''], 5)
    line_markers[0] = LineType.func_def
    line_markers[1] = LineType.arrange  # x = 1
    line_markers[2] = LineType.blank_line
    line_markers[3] = LineType.blank_line
    line_markers[4] = LineType.unprocessed  # Sum do stuff
    line_markers[5] = LineType.act  # result = x + 3
    line_markers[6] = LineType.blank_line
    line_markers[7] = LineType._assert  # assert result == 4

    result = line_markers.check_arrange_act_spacing()

    assert isinstance(result, Generator)
    result_list = list(result)
    assert len(result_list) == 1
    assert result_list[0] == AAAError(
        line_number=10,
        offset=0,
        text='AAA03 expected 1 blank line before Act block, found 2',
    ) 
Example 7
Project: TornadoWeb   Author: VxCoder   File: backports_abc.py    Apache License 2.0 5 votes vote down vote up
def patch(patch_inspect=True):
    """
    Main entry point for patching the ``collections.abc`` and ``inspect``
    standard library modules.
    """
    PATCHED['collections.abc.Generator'] = _collections_abc.Generator = Generator
    PATCHED['collections.abc.Coroutine'] = _collections_abc.Coroutine = Coroutine
    PATCHED['collections.abc.Awaitable'] = _collections_abc.Awaitable = Awaitable

    if patch_inspect:
        import inspect
        PATCHED['inspect.isawaitable'] = inspect.isawaitable = isawaitable 
Example 8
Project: hse-python-ml   Author: saygogoplz   File: test_range.py    MIT License 5 votes vote down vote up
def test_iterable(self):
        self.assertTrue(issubclass(RangeIterator, Iterator))
        self.assertTrue(issubclass(Range, Iterable))
        self.assertTrue(issubclass(Range, Sequence))

        r = Range(2, 10, 5)
        self.assertTrue(isinstance(r, Iterable))
        self.assertTrue(isinstance(r, Sequence))
        self.assertFalse(isinstance(iter(r), Generator))
        self.assertTrue(isinstance(iter(r), Iterator))
        self.assertTrue(isinstance(RangeIterator(r), Iterator))
        self.assertFalse(isinstance(RangeIterator(r), Generator)) 
Example 9
Project: hse-python-ml   Author: saygogoplz   File: test_range.py    MIT License 5 votes vote down vote up
def test_iterable(self):
        self.assertTrue(issubclass(RangeIterator, Iterator))
        self.assertTrue(issubclass(Range, Iterable))
        self.assertTrue(issubclass(Range, Sequence))

        r = Range(2, 10, 5)
        self.assertTrue(isinstance(r, Iterable))
        self.assertTrue(isinstance(r, Sequence))
        self.assertFalse(isinstance(iter(r), Generator))
        self.assertTrue(isinstance(iter(r), Iterator))
        self.assertTrue(isinstance(RangeIterator(r), Iterator))
        self.assertFalse(isinstance(RangeIterator(r), Generator)) 
Example 10
Project: hse-python-ml   Author: saygogoplz   File: test_chain.py    MIT License 5 votes vote down vote up
def test_generator(self):

        self.assertTrue(
            isinstance(chain(1), Generator),
            "Not a generator"
        ) 
Example 11
Project: noc-orchestrator   Author: DirceuSilvaLabs   File: backports_abc.py    MIT License 5 votes vote down vote up
def patch(patch_inspect=True):
    """
    Main entry point for patching the ``collections.abc`` and ``inspect``
    standard library modules.
    """
    PATCHED['collections.abc.Generator'] = _collections_abc.Generator = Generator
    PATCHED['collections.abc.Coroutine'] = _collections_abc.Coroutine = Coroutine
    PATCHED['collections.abc.Awaitable'] = _collections_abc.Awaitable = Awaitable

    if patch_inspect:
        import inspect
        PATCHED['inspect.isawaitable'] = inspect.isawaitable = isawaitable 
Example 12
Project: noc-orchestrator   Author: DirceuSilvaLabs   File: backports_abc.py    MIT License 5 votes vote down vote up
def patch(patch_inspect=True):
    """
    Main entry point for patching the ``collections.abc`` and ``inspect``
    standard library modules.
    """
    PATCHED['collections.abc.Generator'] = _collections_abc.Generator = Generator
    PATCHED['collections.abc.Coroutine'] = _collections_abc.Coroutine = Coroutine
    PATCHED['collections.abc.Awaitable'] = _collections_abc.Awaitable = Awaitable

    if patch_inspect:
        import inspect
        PATCHED['inspect.isawaitable'] = inspect.isawaitable = isawaitable 
Example 13
Project: noc-orchestrator   Author: DirceuSilvaLabs   File: backports_abc.py    MIT License 5 votes vote down vote up
def patch(patch_inspect=True):
    """
    Main entry point for patching the ``collections.abc`` and ``inspect``
    standard library modules.
    """
    PATCHED['collections.abc.Generator'] = _collections_abc.Generator = Generator
    PATCHED['collections.abc.Coroutine'] = _collections_abc.Coroutine = Coroutine
    PATCHED['collections.abc.Awaitable'] = _collections_abc.Awaitable = Awaitable

    if patch_inspect:
        import inspect
        PATCHED['inspect.isawaitable'] = inspect.isawaitable = isawaitable 
Example 14
Project: lazy_dataset   Author: fgnt   File: core.py    MIT License 5 votes vote down vote up
def __iter__(self):
        for batch in self.input_dataset:
            assert isinstance(batch, (list, tuple, collections.Generator))
            for example in batch:
                yield example 
Example 15
Project: flake8-aaa   Author: jamescooke   File: test_check_act_assert_spacing.py    MIT License 5 votes vote down vote up
def test_none():
    line_markers = LineMarkers(2 * [''], 0)
    line_markers[0] = LineType.func_def
    line_markers[1] = LineType.act  # do_thing()

    result = line_markers.check_act_assert_spacing()

    assert isinstance(result, Generator)
    assert list(result) == []


# --- FAILURES --- 
Example 16
Project: ver-observer   Author: neargle   File: process.py    GNU General Public License v3.0 5 votes vote down vote up
def call_multi_process(function, args_lst, timeout=float('inf')):
    """
    use multiprocessing to call the function like pool.map.

    :param function: function to call.
    :param function: iterable object contains args.
    """
    process_pool = init_process_pool()
    results = {}
    childs = []
    start_time = time.time()

    for args in args_lst:
        def _callback(result, args_=args):
            results[args_] = result

        if not isinstance(args, (list, tuple, collections.Generator)):
            args = (args, )
        childs.append(process_pool.apply_async(function, args, callback=_callback))

    try:
        while True:
            time.sleep(0.5)
            now = time.time()
            if (now - start_time) > timeout:
                raise ProcessTimeoutError("main process time cost: {}".format(now))
            if all((child.ready() for child in childs)):
                break
    except (KeyboardInterrupt, ProcessTimeoutError) as ex:
        logger.warning("stopping by user interrupt. exception info: %s", ex)
        process_pool.terminate()
        process_pool.join()
    else:
        process_pool.close()
        process_pool.join()
    return results 
Example 17
Project: crowddynamics   Author: jaantollander   File: test_agents.py    MIT License 5 votes vote down vote up
def test_agent_group(agent_type, attributes):
    group = AgentGroup(size=SIZE, agent_type=agent_type, attributes=attributes)

    assert isinstance(group.size, int)
    assert issubclass(group.agent_type, AgentType)
    assert isinstance(group.attributes, (Collection, Generator, Callable))

    assert isinstance(group.members, list)
    assert len(group.members) == SIZE 
Example 18
Project: crowddynamics   Author: jaantollander   File: agents.py    MIT License 5 votes vote down vote up
def _observe_members(self, change):
        if self.size > 0 and self.attributes is not None and self.agent_type is not None:
            if isinstance(self.attributes, Collection):
                self.members = [self.agent_type(**a) for a in self.attributes]
            elif isinstance(self.attributes, Generator):
                self.members = [self.agent_type(**next(self.attributes)) for _ in range(self.size)]
            elif isinstance(self.attributes, Callable):
                self.members = [self.agent_type(**self.attributes()) for _ in range(self.size)]
            else:
                raise TraitError 
Example 19
Project: rmalt   Author: thautwarm   File: std.py    MIT License 5 votes vote down vote up
def to_linked_list(seq):
    nil: const = LinkedList.Nil
    cons: const = LinkedList.Cons
    if isinstance(seq, LinkedList):
        return seq 
    elif isinstance(seq, (Generator, Iterator)):
        return to_linked_list(tuple(seq))

    else:
        tail = nil
        for each in reversed(seq):
            tail = cons(each, tail)

        return tail 
Example 20
Project: Project-New-Reign---Nemesis-Main   Author: ShikyoKira   File: test_typing.py    GNU General Public License v3.0 5 votes vote down vote up
def test_generator(self):
        def foo():
            yield 42
        g = foo()
        self.assertIsSubclass(type(g), typing.Generator) 
Example 21
Project: Project-New-Reign---Nemesis-Main   Author: ShikyoKira   File: test_typing.py    GNU General Public License v3.0 5 votes vote down vote up
def test_no_generator_instantiation(self):
        with self.assertRaises(TypeError):
            typing.Generator()
        with self.assertRaises(TypeError):
            typing.Generator[T, T, T]()
        with self.assertRaises(TypeError):
            typing.Generator[int, int, int]() 
Example 22
Project: pyqt-async   Author: sashgorokhov   File: pyqt_async.py    MIT License 5 votes vote down vote up
def __init__(self, generator):
        """
        :param collections.Generator generator:
        """
        if not isinstance(generator, collections.Generator):
            raise ValueError('%s is not Generator' % generator)

        super(GeneratorThread, self).__init__()

        self.generator = generator
        self.future = futures.Future()
        self.results = list()

        self._execute_next.connect(self.next)
        self._execute_next.emit() 
Example 23
Project: TornadoWeb   Author: VxCoder   File: backports_abc.py    Apache License 2.0 4 votes vote down vote up
def mk_gen():
    from abc import abstractmethod

    required_methods = (
        '__iter__', '__next__' if hasattr(iter(()), '__next__') else 'next',
         'send', 'throw', 'close')

    class Generator(_collections_abc.Iterator):
        __slots__ = ()

        if '__next__' in required_methods:
            def __next__(self):
                return self.send(None)
        else:
            def next(self):
                return self.send(None)

        @abstractmethod
        def send(self, value):
            raise StopIteration

        @abstractmethod
        def throw(self, typ, val=None, tb=None):
            if val is None:
                if tb is None:
                    raise typ
                val = typ()
            if tb is not None:
                val = val.with_traceback(tb)
            raise val

        def close(self):
            try:
                self.throw(GeneratorExit)
            except (GeneratorExit, StopIteration):
                pass
            else:
                raise RuntimeError('generator ignored GeneratorExit')

        @classmethod
        def __subclasshook__(cls, C):
            if cls is Generator:
                mro = C.__mro__
                for method in required_methods:
                    for base in mro:
                        if method in base.__dict__:
                            break
                    else:
                        return NotImplemented
                return True
            return NotImplemented

    generator = type((lambda: (yield))())
    Generator.register(generator)
    return Generator 
Example 24
Project: noc-orchestrator   Author: DirceuSilvaLabs   File: backports_abc.py    MIT License 4 votes vote down vote up
def mk_gen():
    from abc import abstractmethod

    required_methods = (
        '__iter__', '__next__' if hasattr(iter(()), '__next__') else 'next',
         'send', 'throw', 'close')

    class Generator(_collections_abc.Iterator):
        __slots__ = ()

        if '__next__' in required_methods:
            def __next__(self):
                return self.send(None)
        else:
            def next(self):
                return self.send(None)

        @abstractmethod
        def send(self, value):
            raise StopIteration

        @abstractmethod
        def throw(self, typ, val=None, tb=None):
            if val is None:
                if tb is None:
                    raise typ
                val = typ()
            if tb is not None:
                val = val.with_traceback(tb)
            raise val

        def close(self):
            try:
                self.throw(GeneratorExit)
            except (GeneratorExit, StopIteration):
                pass
            else:
                raise RuntimeError('generator ignored GeneratorExit')

        @classmethod
        def __subclasshook__(cls, C):
            if cls is Generator:
                mro = C.__mro__
                for method in required_methods:
                    for base in mro:
                        if method in base.__dict__:
                            break
                    else:
                        return NotImplemented
                return True
            return NotImplemented

    generator = type((lambda: (yield))())
    Generator.register(generator)
    return Generator 
Example 25
Project: noc-orchestrator   Author: DirceuSilvaLabs   File: backports_abc.py    MIT License 4 votes vote down vote up
def mk_gen():
    from abc import abstractmethod

    required_methods = (
        '__iter__', '__next__' if hasattr(iter(()), '__next__') else 'next',
         'send', 'throw', 'close')

    class Generator(_collections_abc.Iterator):
        __slots__ = ()

        if '__next__' in required_methods:
            def __next__(self):
                return self.send(None)
        else:
            def next(self):
                return self.send(None)

        @abstractmethod
        def send(self, value):
            raise StopIteration

        @abstractmethod
        def throw(self, typ, val=None, tb=None):
            if val is None:
                if tb is None:
                    raise typ
                val = typ()
            if tb is not None:
                val = val.with_traceback(tb)
            raise val

        def close(self):
            try:
                self.throw(GeneratorExit)
            except (GeneratorExit, StopIteration):
                pass
            else:
                raise RuntimeError('generator ignored GeneratorExit')

        @classmethod
        def __subclasshook__(cls, C):
            if cls is Generator:
                mro = C.__mro__
                for method in required_methods:
                    for base in mro:
                        if method in base.__dict__:
                            break
                    else:
                        return NotImplemented
                return True
            return NotImplemented

    generator = type((lambda: (yield))())
    Generator.register(generator)
    return Generator 
Example 26
Project: noc-orchestrator   Author: DirceuSilvaLabs   File: backports_abc.py    MIT License 4 votes vote down vote up
def mk_gen():
    from abc import abstractmethod

    required_methods = (
        '__iter__', '__next__' if hasattr(iter(()), '__next__') else 'next',
         'send', 'throw', 'close')

    class Generator(_collections_abc.Iterator):
        __slots__ = ()

        if '__next__' in required_methods:
            def __next__(self):
                return self.send(None)
        else:
            def next(self):
                return self.send(None)

        @abstractmethod
        def send(self, value):
            raise StopIteration

        @abstractmethod
        def throw(self, typ, val=None, tb=None):
            if val is None:
                if tb is None:
                    raise typ
                val = typ()
            if tb is not None:
                val = val.with_traceback(tb)
            raise val

        def close(self):
            try:
                self.throw(GeneratorExit)
            except (GeneratorExit, StopIteration):
                pass
            else:
                raise RuntimeError('generator ignored GeneratorExit')

        @classmethod
        def __subclasshook__(cls, C):
            if cls is Generator:
                mro = C.__mro__
                for method in required_methods:
                    for base in mro:
                        if method in base.__dict__:
                            break
                    else:
                        return NotImplemented
                return True
            return NotImplemented

    generator = type((lambda: (yield))())
    Generator.register(generator)
    return Generator 
Example 27
Project: noc-orchestrator   Author: DirceuSilvaLabs   File: backports_abc.py    MIT License 4 votes vote down vote up
def mk_gen():
    from abc import abstractmethod

    required_methods = (
        '__iter__', '__next__' if hasattr(iter(()), '__next__') else 'next',
         'send', 'throw', 'close')

    class Generator(_collections_abc.Iterator):
        __slots__ = ()

        if '__next__' in required_methods:
            def __next__(self):
                return self.send(None)
        else:
            def next(self):
                return self.send(None)

        @abstractmethod
        def send(self, value):
            raise StopIteration

        @abstractmethod
        def throw(self, typ, val=None, tb=None):
            if val is None:
                if tb is None:
                    raise typ
                val = typ()
            if tb is not None:
                val = val.with_traceback(tb)
            raise val

        def close(self):
            try:
                self.throw(GeneratorExit)
            except (GeneratorExit, StopIteration):
                pass
            else:
                raise RuntimeError('generator ignored GeneratorExit')

        @classmethod
        def __subclasshook__(cls, C):
            if cls is Generator:
                mro = C.__mro__
                for method in required_methods:
                    for base in mro:
                        if method in base.__dict__:
                            break
                    else:
                        return NotImplemented
                return True
            return NotImplemented

    generator = type((lambda: (yield))())
    Generator.register(generator)
    return Generator 
Example 28
Project: crowddynamics   Author: jaantollander   File: agents.py    MIT License 4 votes vote down vote up
def add_non_overlapping_group(self, group, position_gen, obstacles=None):
        """Add group of agents

        Args:
            group (AgentGroup):
            position_gen (Generator|Callable):
            obstacles (numpy.ndarray):
        """
        if self.agent_type is not group.agent_type:
            raise CrowdDynamicsException

        # resize self.array to fit new agents
        array = np.zeros(group.size, dtype=group.agent_type.dtype())
        self.array = np.concatenate((self.array, array))

        index = 0
        overlaps = 0
        overlaps_max = 10 * group.size

        while index < group.size and overlaps < overlaps_max:
            new_agent = group.members[index]
            new_agent.position = position_gen() if callable(position_gen) \
                else next(position_gen)

            # Overlapping check
            neighbours = self._neighbours.nearest(new_agent.position, radius=1)
            if new_agent.overlapping(self.array[neighbours]):
                # Agent is overlapping other agent.
                overlaps += 1
                continue

            if obstacles is not None and new_agent.overlapping_obstacles(obstacles):
                # Agent is overlapping with an obstacle.
                overlaps += 1
                continue

            # Agent can be successfully placed
            self.array[self.index] = np.array(new_agent)
            self._neighbours[new_agent.position] = self.index
            self.index += 1
            index += 1

        # TODO: remove agents that didn't fit from self.array
        if self.index + 1 < self.array.size:
            pass

        # Array should remain contiguous
        assert self.array.flags.c_contiguous