Python collections.abc.Generator() Examples

The following are 30 code examples of collections.abc.Generator(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module collections.abc , or try the search function .
Example #1
Source File: parser.py    From pvl with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def parse_WSC_until(self, token: str, tokens: abc.Generator) -> bool:
        """Consumes objects from *tokens*, if the object's *.is_WSC()*
        function returns *True*, it will continue until *token* is
        encountered and will return *True*.  If it encounters an object
        that does not meet these conditions, it will 'return' that
        object to *tokens* and will return *False*.

        *tokens* is expected to be a *generator iterator* which
        provides ``pvl.token`` objects.
        """
        for t in tokens:
            if t == token:
                return True
            elif t.is_WSC():
                # If there's a comment, could parse here.
                pass
            else:
                tokens.send(t)
                return False 
Example #2
Source File: testing.py    From teleport with Apache License 2.0 6 votes vote down vote up
def __init__(self, methodName: str = "runTest") -> None:
        super(AsyncTestCase, self).__init__(methodName)
        self.__stopped = False
        self.__running = False
        self.__failure = None  # type: Optional[_ExcInfoTuple]
        self.__stop_args = None  # type: Any
        self.__timeout = None  # type: Optional[object]

        # It's easy to forget the @gen_test decorator, but if you do
        # the test will silently be ignored because nothing will consume
        # the generator.  Replace the test method with a wrapper that will
        # make sure it's not an undecorated generator.
        setattr(self, methodName, _TestMethodWrapper(getattr(self, methodName)))

        # Not used in this class itself, but used by @gen_test
        self._test_generator = None  # type: Optional[Union[Generator, Coroutine]] 
Example #3
Source File: testing.py    From V1EngineeringInc-Docs with Creative Commons Attribution Share Alike 4.0 International 6 votes vote down vote up
def __init__(self, methodName: str = "runTest") -> None:
        super(AsyncTestCase, self).__init__(methodName)
        self.__stopped = False
        self.__running = False
        self.__failure = None  # type: Optional[_ExcInfoTuple]
        self.__stop_args = None  # type: Any
        self.__timeout = None  # type: Optional[object]

        # It's easy to forget the @gen_test decorator, but if you do
        # the test will silently be ignored because nothing will consume
        # the generator.  Replace the test method with a wrapper that will
        # make sure it's not an undecorated generator.
        setattr(self, methodName, _TestMethodWrapper(getattr(self, methodName)))

        # Not used in this class itself, but used by @gen_test
        self._test_generator = None  # type: Optional[Union[Generator, Coroutine]] 
Example #4
Source File: kepler.py    From traffic with MIT License 6 votes vote down vote up
def map_add_data(_map, data, *args, **kwargs):
    if any(isinstance(data, c) for c in (Airspace, Flight, Traffic)):
        layer = data.kepler()
        return _old_add_data(_map, layer, *args, **kwargs)

    if any(isinstance(data, c) for c in (list, Generator)):
        layer = dict(
            type="FeatureCollection", features=[elt.kepler() for elt in data]
        )
        return _old_add_data(_map, layer, *args, **kwargs)

    # convenient for airports, navaids, etc.
    if hasattr(data, "data"):
        data = data.data

    return _old_add_data(_map, data, *args, **kwargs) 
Example #5
Source File: parser.py    From pvl with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def parse_module_post_hook(self, module, tokens: abc.Generator):
        """This function is meant to be overridden by subclasses
        that may want to perform some extra processing if
        'normal' parse_module() operations fail to complete.
        See OmniParser for an example.

        This function shall return a two-tuple, with the first item
        being the *module* (altered by processing or unaltered), and
        the second item being a boolean that will signal whether
        the tokens should continue to be parsed to accumulate more
        elements into the returned *module*, or whether the
        *module* is in a good state and should be returned by
        parse_module().

        If the operations within this function are unsuccessful,
        it should raise an exception (any exception descended from
        Exception), which will result in the operation of parse_module()
        as if it were not overridden.
        """
        raise Exception 
Example #6
Source File: testing.py    From opendevops with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, methodName: str = "runTest") -> None:
        super(AsyncTestCase, self).__init__(methodName)
        self.__stopped = False
        self.__running = False
        self.__failure = None  # type: Optional[_ExcInfoTuple]
        self.__stop_args = None  # type: Any
        self.__timeout = None  # type: Optional[object]

        # It's easy to forget the @gen_test decorator, but if you do
        # the test will silently be ignored because nothing will consume
        # the generator.  Replace the test method with a wrapper that will
        # make sure it's not an undecorated generator.
        setattr(self, methodName, _TestMethodWrapper(getattr(self, methodName)))

        # Not used in this class itself, but used by @gen_test
        self._test_generator = None  # type: Optional[Union[Generator, Coroutine]] 
Example #7
Source File: parser.py    From pvl with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def parse_value_post_hook(self, tokens: abc.Generator):
        """Overrides the parent function to allow for more
        permissive parsing.

        If the next token is a reserved word or delimiter,
        then it is returned to the *tokens* and an
        EmptyValueAtLine object is returned as the value.
        """

        t = next(tokens)
        # print(f't: {t}')
        truecase_reserved = [x.casefold() for x in
                             self.grammar.reserved_keywords]
        trucase_delim = [x.casefold() for x in
                         self.grammar.delimiters]
        if t.casefold() in (truecase_reserved + trucase_delim):
            # print(f'kw: {kw}')
            # if kw.casefold() == t.casefold():
            # print('match')
            tokens.send(t)
            return self._empty_value(t.pos)
        else:
            raise ValueError 
Example #8
Source File: parser.py    From pvl with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def parse_set(self, tokens: abc.Generator) -> set:
        """Parses a PVL Set.

         <Set> ::= "{" <WSC>*
                   [ <Value> <WSC>* ( "," <WSC>* <Value> <WSC>* )* ]
                   "}"

        Returns the decoded <Set> as a Python ``frozenset``.  The PVL
        specification doesn't seem to indicate that a PVL Set
        has distinct values (like a Python ``set``), only that the
        ordering of the values is unimportant.  For now, we will
        implement PVL Sets as Python ``frozenset`` objects.

        They are returned as ``frozenset`` objects because PVL Sets
        can contain as their elements other PVL Sets, but since Python
        ``set`` objects are non-hashable, they cannot be members of a set,
        however, ``frozenset`` objects can.
        """
        return frozenset(self._parse_set_seq(self.grammar.set_delimiters,
                                             tokens)) 
Example #9
Source File: parser.py    From pvl with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def parse_around_equals(self, tokens: abc.Generator) -> None:
        """Parses white space and comments on either side
        of an equals sign.

        *tokens* is expected to be a *generator iterator* which
        provides ``pvl.token`` objects.

        This is shared functionality for Begin Aggregation Statements
        and Assignment Statements.  It basically covers parsing
        anything that has a syntax diagram like this:

          <WSC>* '=' <WSC>*

        """
        if not self.parse_WSC_until('=', tokens):
            try:
                t = next(tokens)
                tokens.send(t)
                raise ValueError(f'Expecting "=", got: {t}')
            except StopIteration:
                raise ParseError('Expecting "=", but ran out of tokens.')

        self.parse_WSC_until(None, tokens)
        return 
Example #10
Source File: testing.py    From teleport with Apache License 2.0 6 votes vote down vote up
def __init__(self, methodName: str = "runTest") -> None:
        super(AsyncTestCase, self).__init__(methodName)
        self.__stopped = False
        self.__running = False
        self.__failure = None  # type: Optional[_ExcInfoTuple]
        self.__stop_args = None  # type: Any
        self.__timeout = None  # type: Optional[object]

        # It's easy to forget the @gen_test decorator, but if you do
        # the test will silently be ignored because nothing will consume
        # the generator.  Replace the test method with a wrapper that will
        # make sure it's not an undecorated generator.
        setattr(self, methodName, _TestMethodWrapper(getattr(self, methodName)))

        # Not used in this class itself, but used by @gen_test
        self._test_generator = None  # type: Optional[Union[Generator, Coroutine]] 
Example #11
Source File: parser.py    From pvl with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def parse_aggregation_block(self, tokens: abc.Generator):
        """Parses the tokens for an Aggregation Block, and returns
        the modcls object that is the result of the parsing and
        decoding.

         <Aggregation-Block> ::= <Begin-Aggegation-Statement>
             (<WSC>* (Assignment-Statement | Aggregation-Block) <WSC>*)+
             <End-Aggregation-Statement>

        The Begin-Aggregation-Statement Name must match the Block-Name
        in the paired End-Aggregation-Statement if a Block-Name is
        present in the End-Aggregation-Statement.
        """
        (begin, block_name) = self.parse_begin_aggregation_statement(tokens)

        agg = self.aggregation_cls(begin)

        while True:
            self.parse_WSC_until(None, tokens)
            try:
                agg.append(*self.parse_aggregation_block(tokens))
            except ValueError:
                try:
                    agg.append(*self.parse_assignment_statement(tokens))
                    # print(f'agg: {agg}')
                    # t = next(tokens)
                    # print(f'next token is: {t}')
                    # tokens.send(t)
                except LexerError:
                    raise
                except ValueError:
                    # t = next(tokens)
                    # print(f'parsing agg block, next token is: {t}')
                    # tokens.send(t)
                    self.parse_end_aggregation(begin, block_name, tokens)
                    break

        return (block_name, agg) 
Example #12
Source File: parser.py    From pvl with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def parse_assignment_statement(self, tokens: abc.Generator) -> tuple:
        """Parses the tokens for an Assignment Statement.

        The returned two-tuple contains the Parameter Name in the
        first element, and the Value in the second.

         <Assignment-Statement> ::= <Parameter-Name> <WSC>* '=' <WSC>*
                                     <Value> [<Statement-Delimiter>]

        """
        parameter_name = None
        try:
            t = next(tokens)
            if t.is_parameter_name():
                parameter_name = str(t)
            else:
                tokens.send(t)
                raise ValueError('Expecting a Parameter Name, but '
                                 f'found: "{t}"')
        except StopIteration:
            raise ValueError('Ran out of tokens before starting to parse '
                             'an Assignment-Statement.')

        Value = None
        self.parse_around_equals(tokens)

        try:
            # print(f'parameter name: {parameter_name}')
            Value = self.parse_value(tokens)
        except StopIteration:
            raise ParseError('Ran out of tokens to parse after the equals '
                             'sign in an Assignment-Statement: '
                             f'"{parameter_name} =".', t)

        self.parse_statement_delimiter(tokens)

        return(parameter_name, Value) 
Example #13
Source File: parser.py    From pvl with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def parse_end_statement(self, tokens: abc.Generator) -> None:
        """Parses the tokens for an End Statement.

        <End-Statement> ::= "END" ( <WSC>* | [<Statement-Delimiter>] )

        """
        try:
            end = next(tokens)
            if not end.is_end_statement():
                tokens.send(end)
                raise ValueError('Expecting an End Statement, like '
                                 f'"{self.grammar.end_statements}" but found '
                                 f'"{end}"')

            try:
                t = next(tokens)
                if t.is_WSC():
                    # maybe process comment
                    return
                else:
                    tokens.send(t)
                    return
            except LexerError:
                pass
        except StopIteration:
            pass

        return 
Example #14
Source File: parser.py    From pvl with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def parse_begin_aggregation_statement(self, tokens: abc.Generator) -> tuple:
        """Parses the tokens for a Begin Aggregation Statement, and returns
        the name Block Name as a ``str``.

        <Begin-Aggregation-Statement-block> ::=
             <Begin-Aggegation-Statement> <WSC>* '=' <WSC>*
             <Block-Name> [<Statement-Delimiter>]

        Where <Block-Name> ::= <Parameter-Name>

        """
        try:
            begin = next(tokens)
            if not begin.is_begin_aggregation():
                tokens.send(begin)
                raise ValueError('Expecting a Begin-Aggegation-Statement, but '
                                 f'found: {begin}')
        except StopIteration:
            raise ValueError('Ran out of tokens before starting to parse '
                             'a Begin-Aggegation-Statement.')

        try:
            self.parse_around_equals(tokens)
        except ValueError:
            tokens.throw(ValueError,
                         f'Expecting an equals sign after "{begin}" ')

        block_name = next(tokens)
        if not block_name.is_parameter_name():
            tokens.throw(ValueError,
                         f'Expecting a Block-Name after "{begin} =" '
                         f'but found: "{block_name}"')

        self.parse_statement_delimiter(tokens)

        return(begin, str(block_name)) 
Example #15
Source File: typing.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def __new__(cls, *args, **kwds):
            if _geqv(cls, ChainMap):
                return collections.ChainMap(*args, **kwds)
            return _generic_new(collections.ChainMap, cls, *args, **kwds)


# Determine what base class to use for Generator. 
Example #16
Source File: typing.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
def __new__(cls, *args, **kwds):
        if _geqv(cls, Generator):
            raise TypeError("Type Generator cannot be instantiated; "
                            "create a subclass instead")
        return _generic_new(_G_base, cls, *args, **kwds) 
Example #17
Source File: typing.py    From review-heatmap with GNU Affero General Public License v3.0 5 votes vote down vote up
def __new__(cls, *args, **kwds):
            if cls._gorg is ChainMap:
                return collections.ChainMap(*args, **kwds)
            return _generic_new(collections.ChainMap, cls, *args, **kwds)


# Determine what base class to use for Generator. 
Example #18
Source File: parser.py    From pvl with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _parse_set_seq(self, delimiters, tokens: abc.Generator) -> list:
        """The internal parsing of PVL Sets and Sequences are very
        similar, and this function provides that shared logic.

        *delimiters* are a two-tuple containing the start and end
        characters for the PVL Set or Sequence.
        """
        t = next(tokens)
        if t != delimiters[0]:
            tokens.send(t)
            raise ValueError(f'Expecting a begin delimiter "{delimiters[0]}" '
                             f'but found: "{t}"')
        set_seq = list()
        # Initial WSC and/or empty
        if self.parse_WSC_until(delimiters[1], tokens):
            return set_seq

        # First item:
        set_seq.append(self.parse_value(tokens))
        if self.parse_WSC_until(delimiters[1], tokens):
            return set_seq

        # Remaining items, if any
        for t in tokens:
            # print(f'in loop, t: {t}, set_seq: {set_seq}')
            if t == ',':
                self.parse_WSC_until(None, tokens)  # consume WSC after ','
                set_seq.append(self.parse_value(tokens))
                if self.parse_WSC_until(delimiters[1], tokens):
                    return set_seq
            else:
                tokens.send(t)
                tokens.throw(ValueError,
                             'While parsing, expected a comma (,)'
                             f'but found: "{t}"') 
Example #19
Source File: parser.py    From pvl with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def parse_statement_delimiter(self, tokens: abc.Generator) -> bool:
        """Parses the tokens for a Statement Delimiter.

        *tokens* is expected to be a *generator iterator* which
        provides ``pvl.token`` objects.

         <Statement-Delimiter> ::= <WSC>*
                     (<white-space-character> | <comment> | ';' | <EOF>)

        Although the above structure comes from Figure 2-4
        of the Blue Book, the <white-space-character> and <comment>
        elements are redundant with the presence of [WSC]*
        so it can be simplified to:

         <Statement-Delimiter> ::= <WSC>* [ ';' | <EOF> ]

        Typically written [<Statement-Delimiter>].
        """
        for t in tokens:
            if t.is_WSC():
                # If there's a comment, could parse here.
                pass
            elif t.is_delimiter():
                return True
            else:
                tokens.send(t)  # Put the next token back into the generator
                return False 
Example #20
Source File: parser.py    From pvl with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def parse_units(self, value, tokens: abc.Generator) -> str:
        """Parses PVL Units Expression.

         <Units-Expression> ::= "<" [<white-space>] <Units-Value>
                                    [<white-space>] ">"

        and

         <Units-Value> ::= <units-character>
                             [ [ <units-character> | <white-space> ]*
                                 <units-character> ]

        Returns the *value* and the <Units-Value> as a ``Units()``
        object.
        """
        t = next(tokens)

        if not t.startswith(self.grammar.units_delimiters[0]):
            tokens.send(t)
            raise ValueError('Was expecting the start units delimiter, ' +
                             '"{}" '.format(self.grammar.units_delimiters[0]) +
                             f'but found "{t}"')

        if not t.endswith(self.grammar.units_delimiters[1]):
            tokens.send(t)
            raise ValueError('Was expecting the end units delimiter, ' +
                             '"{}" '.format(self.grammar.units_delimiters[1]) +
                             f'at the end, but found "{t}"')

        delim_strip = t.strip(''.join(self.grammar.units_delimiters))

        units_value = delim_strip.strip(''.join(self.grammar.whitespace))

        for d in self.grammar.units_delimiters:
            if d in units_value:
                tokens.throw(ValueError,
                             'Was expecting a units character, but found a '
                             f'unit delimiter, "{d}" instead.')

        return self.decoder.decode_quantity(value, units_value) 
Example #21
Source File: parser.py    From pvl with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def parse_set(self, tokens: abc.Generator) -> set:
        """Overrides the parent function to return
        the decoded <Set> as a Python ``set``.

        The ODL specification only allows scalar_values in Sets,
        since ODL Sets cannot contain other ODL Sets, an ODL Set
        can be represented as a Python ``set`` (unlike PVL Sets,
        which must be represented as a Python ``frozenset`` objects).
        """
        return set(self._parse_set_seq(self.grammar.set_delimiters,
                                       tokens)) 
Example #22
Source File: parser.py    From pvl with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def parse_units(self, value, tokens: abc.Generator) -> str:
        """Extends the parent function, since ODL only allows units
        on numeric values, any others will result in a ValueError.
        """

        if isinstance(value, int) or isinstance(value, float):
            return super().parse_units(value, tokens)

        else:
            raise ValueError('ODL Units Expressions can only follow '
                             'numeric values.') 
Example #23
Source File: parser.py    From pvl with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def parse_assignment_statement(self, tokens: abc.Generator) -> tuple:
        """Extends the parent function to allow for more
        permissive parsing.  If an Assignment-Statement
        is blank, then the value will be assigned an
        EmptyValueAtLine object.
        """
        try:
            return super().parse_assignment_statement(tokens)
        except ParseError as err:
            if err.token is not None:
                after_eq = self.doc.find('=', err.token.pos) + 1
                return (str(err.token), self._empty_value(after_eq))
            else:
                raise 
Example #24
Source File: gen.py    From V1EngineeringInc-Docs with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
def __init__(
        self,
        gen: "Generator[_Yieldable, Any, _T]",
        result_future: "Future[_T]",
        first_yielded: _Yieldable,
    ) -> None:
        self.gen = gen
        self.result_future = result_future
        self.future = _null_future  # type: Union[None, Future]
        self.running = False
        self.finished = False
        self.io_loop = IOLoop.current()
        if self.handle_yield(first_yielded):
            gen = result_future = first_yielded = None  # type: ignore
            self.run() 
Example #25
Source File: testing.py    From V1EngineeringInc-Docs with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
def __call__(self, *args: Any, **kwargs: Any) -> None:
        result = self.orig_method(*args, **kwargs)
        if isinstance(result, Generator) or inspect.iscoroutine(result):
            raise TypeError(
                "Generator and coroutine test methods should be"
                " decorated with tornado.testing.gen_test"
            )
        elif result is not None:
            raise ValueError("Return value from test method ignored: %r" % result) 
Example #26
Source File: testing.py    From V1EngineeringInc-Docs with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
def gen_test(
    *, timeout: float = None
) -> Callable[[Callable[..., Union[Generator, "Coroutine"]]], Callable[..., None]]:
    pass 
Example #27
Source File: testing.py    From V1EngineeringInc-Docs with Creative Commons Attribution Share Alike 4.0 International 5 votes vote down vote up
def gen_test(func: Callable[..., Union[Generator, "Coroutine"]]) -> Callable[..., None]:
    pass 
Example #28
Source File: testing.py    From teleport with Apache License 2.0 5 votes vote down vote up
def gen_test(
    *, timeout: float = None
) -> Callable[[Callable[..., Union[Generator, "Coroutine"]]], Callable[..., None]]:
    pass 
Example #29
Source File: prettier.py    From python-devtools with MIT License 5 votes vote down vote up
def __init__(
        self,
        indent_step=4,
        indent_char=' ',
        repr_strings=False,
        simple_cutoff=10,
        width=120,
        yield_from_generators=True,
    ):
        self._indent_step = indent_step
        self._c = indent_char
        self._repr_strings = repr_strings
        self._repr_generators = not yield_from_generators
        self._simple_cutoff = simple_cutoff
        self._width = width
        self._type_lookup = [
            (dict, self._format_dict),
            ((str, bytes), self._format_str_bytes),
            (tuple, self._format_tuples),
            ((list, set, frozenset), self._format_list_like),
            (Generator, self._format_generators),
        ] 
Example #30
Source File: prettier.py    From python-devtools with MIT License 5 votes vote down vote up
def _format(self, value: 'Any', indent_current: int, indent_first: bool):
        if indent_first:
            self._stream.write(indent_current * self._c)

        try:
            pretty_func = getattr(value, '__pretty__')
        except AttributeError:
            pass
        else:
            # `pretty_func.__class__.__name__ == 'method'` should only be true for bound methods,
            # `hasattr(pretty_func, '__self__')` is more canonical but weirdly is true for unbound cython functions
            from unittest.mock import _Call as MockCall

            if pretty_func.__class__.__name__ == 'method' and not isinstance(value, MockCall):
                try:
                    gen = pretty_func(fmt=fmt, skip_exc=SkipPretty)
                    self._render_pretty(gen, indent_current)
                except SkipPretty:
                    pass
                else:
                    return

        value_repr = repr(value)
        if len(value_repr) <= self._simple_cutoff and not isinstance(value, Generator):
            self._stream.write(value_repr)
        else:
            indent_new = indent_current + self._indent_step
            for t, func in self._type_lookup:
                if isinstance(value, t):
                    func(value, value_repr, indent_current, indent_new)
                    return

            # very blunt check for things that look like dictionaries but do not necessarily inherit from Mapping
            # e.g. asyncpg Records
            # HELP: are there any other checks we should include here?
            if hasattr(value, '__getitem__') and hasattr(value, 'items') and callable(value.items):
                self._format_dict(value, value_repr, indent_current, indent_new)
                return

            self._format_raw(value, value_repr, indent_current, indent_new)