Python typing.Iterator() Examples

The following are 30 code examples of typing.Iterator(). 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 typing , or try the search function .
Example #1
Source File: default_resolver.py    From clikit with MIT License 6 votes vote down vote up
def get_options_to_test(self, tokens):  # type: (Iterator[str]) -> List[str]
        options_to_test = []
        token = next(tokens, None)

        while token:
            # "--" stops option parsing
            if token == "--":
                break

            if token[:1] and token[0] == "-":
                if token[:2] == "--" and len(token) > 2:
                    options_to_test.append(token[2:])
                elif len(token) == 2:
                    options_to_test.append(token[1:])

            token = next(tokens, None)

        return options_to_test 
Example #2
Source File: many_to_one.py    From matchpy with MIT License 6 votes vote down vote up
def _match_sequence_variable(self, wildcard: Wildcard, transition: _Transition) -> Iterator[_State]:
        min_count = wildcard.min_count
        if len(self.subjects) < min_count:
            return
        matched_subject = []
        for _ in range(min_count):
            matched_subject.append(self.subjects.popleft())
        while True:
            if self.associative[-1] and wildcard.fixed_size:
                assert min_count == 1, "Fixed wildcards with length != 1 are not supported."
                if len(matched_subject) > 1:
                    wrapped = self.associative[-1](*matched_subject)
                else:
                    wrapped = matched_subject[0]
            else:
                if len(matched_subject) == 0 and wildcard.optional is not None:
                    wrapped = wildcard.optional
                else:
                    wrapped = tuple(matched_subject)
            yield from self._check_transition(transition, wrapped, False)
            if not self.subjects:
                break
            matched_subject.append(self.subjects.popleft())
        self.subjects.extendleft(reversed(matched_subject)) 
Example #3
Source File: keyutils.py    From qutebrowser with GNU General Public License v3.0 6 votes vote down vote up
def _parse_keystring(keystr: str) -> typing.Iterator[str]:
    key = ''
    special = False
    for c in keystr:
        if c == '>':
            if special:
                yield _parse_special_key(key)
                key = ''
                special = False
            else:
                yield '>'
                assert not key, key
        elif c == '<':
            special = True
        elif special:
            key += c
        else:
            yield _parse_single_key(c)
    if special:
        yield '<'
        for c in key:
            yield _parse_single_key(c) 
Example #4
Source File: structures.py    From instaloader with MIT License 6 votes vote down vote up
def get_sidecar_nodes(self) -> Iterator[PostSidecarNode]:
        """Sidecar nodes of a Post with typename==GraphSidecar."""
        if self.typename == 'GraphSidecar':
            edges = self._field('edge_sidecar_to_children', 'edges')
            if any(edge['node']['is_video'] for edge in edges):
                # video_url is only present in full metadata, issue #558.
                edges = self._full_metadata['edge_sidecar_to_children']['edges']
            for idx, edge in enumerate(edges):
                node = edge['node']
                is_video = node['is_video']
                display_url = node['display_url']
                if not is_video and self._context.is_logged_in:
                    try:
                        carousel_media = self._iphone_struct['carousel_media']
                        orig_url = carousel_media[idx]['image_versions2']['candidates'][0]['url']
                        display_url = re.sub(r'&se=\d+(&?)', r'\1', orig_url)
                    except (InstaloaderException, KeyError, IndexError) as err:
                        self._context.error('{} Unable to fetch high quality image version of {}.'.format(err, self))
                yield PostSidecarNode(is_video=is_video, display_url=display_url,
                                      video_url=node['video_url'] if is_video else None) 
Example #5
Source File: base.py    From easy-faster-rcnn.pytorch with MIT License 6 votes vote down vote up
def __iter__(self) -> Iterator[int]:
            image_ratios = torch.tensor(self._image_ratios)
            tall_indices = (image_ratios < 1).nonzero().view(-1)
            fat_indices = (image_ratios >= 1).nonzero().view(-1)

            tall_indices_length = len(tall_indices)
            fat_indices_length = len(fat_indices)

            tall_indices = tall_indices[torch.randperm(tall_indices_length)]
            fat_indices = fat_indices[torch.randperm(fat_indices_length)]

            num_tall_remainder = tall_indices_length % self._num_neighbors
            num_fat_remainder = fat_indices_length % self._num_neighbors

            tall_indices = tall_indices[:tall_indices_length - num_tall_remainder]
            fat_indices = fat_indices[:fat_indices_length - num_fat_remainder]

            tall_indices = tall_indices.view(-1, self._num_neighbors)
            fat_indices = fat_indices.view(-1, self._num_neighbors)
            merge_indices = torch.cat([tall_indices, fat_indices], dim=0)
            merge_indices = merge_indices[torch.randperm(len(merge_indices))].view(-1)

            return iter(merge_indices.tolist()) 
Example #6
Source File: requests.py    From jsonrpcclient with MIT License 6 votes vote down vote up
def __init__(
        self,
        method: str,
        *args: Any,
        id_generator: Optional[Iterator[Any]] = None,
        **kwargs: Any
    ) -> None:
        # If 'request_id' is passed, use the specified id
        if "request_id" in kwargs:
            id_ = kwargs.pop("request_id", None)
        else:  # Get the next id from the generator
            id_generator = (
                id_generator if id_generator is not None else self.id_generator
            )
            id_ = next(id_generator)
        # We call super last, after popping the request_id
        super().__init__(method, *args, **kwargs)
        self.update(id=id_) 
Example #7
Source File: many_to_one.py    From matchpy with MIT License 6 votes vote down vote up
def _match_with_bipartite(
            self,
            subject_ids: MultisetOfInt,
            pattern_set: MultisetOfInt,
            substitution: Substitution,
    ) -> Iterator[Tuple[Substitution, MultisetOfInt]]:
        bipartite = self._build_bipartite(subject_ids, pattern_set)
        for matching in enum_maximum_matchings_iter(bipartite):
            if len(matching) < len(pattern_set):
                break
            if not self._is_canonical_matching(matching):
                continue
            for substs in itertools.product(*(bipartite[edge] for edge in matching.items())):
                try:
                    bipartite_substitution = substitution.union(*substs)
                except ValueError:
                    continue
                matched_subjects = Multiset(subexpression for subexpression, _ in matching)
                yield bipartite_substitution, matched_subjects 
Example #8
Source File: async_client.py    From jsonrpcclient with MIT License 6 votes vote down vote up
def request(
        self,
        method_name: str,
        *args: Any,
        trim_log_values: bool = False,
        validate_against_schema: bool = True,
        id_generator: Optional[Iterator] = None,
        **kwargs: Any
    ) -> Response:
        """
        Async version of Client.request.
        """
        return await self.send(
            Request(method_name, id_generator=id_generator, *args, **kwargs),
            trim_log_values=trim_log_values,
            validate_against_schema=validate_against_schema,
        ) 
Example #9
Source File: many_to_one.py    From matchpy with MIT License 6 votes vote down vote up
def _match_sequence_variables(
            self,
            subjects: MultisetOfExpression,
            pattern_vars: Sequence[VariableWithCount],
            substitution: Substitution,
    ) -> Iterator[Substitution]:
        only_counts = [info for info, _ in pattern_vars]
        wrapped_vars = [name for (name, _, _, _), wrap in pattern_vars if wrap and name]
        for variable_substitution in commutative_sequence_variable_partition_iter(subjects, only_counts):
            for var in wrapped_vars:
                operands = variable_substitution[var]
                if isinstance(operands, (tuple, list, Multiset)):
                    if len(operands) > 1:
                        variable_substitution[var] = self.associative(*operands)
                    else:
                        variable_substitution[var] = next(iter(operands))
            try:
                result_substitution = substitution.union(variable_substitution)
            except ValueError:
                continue
            yield result_substitution 
Example #10
Source File: id_generators.py    From jsonrpcclient with MIT License 6 votes vote down vote up
def hexadecimal(start: int = 1) -> Iterator[str]:
    """
    Incremental hexadecimal numbers.

    e.g. 1, 2, 3, .. 9, a, b, etc.

    Args:
        start: The first value to start with.
    """
    while True:
        yield "%x" % start
        start += 1 
Example #11
Source File: utils.py    From qutebrowser with GNU General Public License v3.0 6 votes vote down vote up
def fake_io(write_func: typing.Callable[[str], int]) -> typing.Iterator[None]:
    """Run code with stdout and stderr replaced by FakeIOStreams.

    Args:
        write_func: The function to call when write is called.
    """
    old_stdout = sys.stdout
    old_stderr = sys.stderr
    fake_stderr = FakeIOStream(write_func)
    fake_stdout = FakeIOStream(write_func)
    sys.stderr = fake_stderr  # type: ignore[assignment]
    sys.stdout = fake_stdout  # type: ignore[assignment]
    try:
        yield
    finally:
        # If the code we did run did change sys.stdout/sys.stderr, we leave it
        # unchanged. Otherwise, we reset it.
        if sys.stdout is fake_stdout:  # type: ignore[comparison-overlap]
            sys.stdout = old_stdout
        if sys.stderr is fake_stderr:  # type: ignore[comparison-overlap]
            sys.stderr = old_stderr 
Example #12
Source File: client.py    From jsonrpcclient with MIT License 6 votes vote down vote up
def __init__(
        self,
        trim_log_values: bool = False,
        validate_against_schema: bool = True,
        id_generator: Optional[Iterator] = None,
        basic_logging: bool = False,
    ) -> None:
        """
        Args:
            trim_log_values: Abbreviate the log entries of requests and responses.
            validate_against_schema: Validate response against the JSON-RPC schema.
            id_generator: Iterable of values to use as the "id" part of the request.
            basic_logging: Will create log handlers to output request & response
                messages.
        """
        self.trim_log_values = trim_log_values
        self.validate_against_schema = validate_against_schema
        self.id_generator = id_generator
        if basic_logging:
            self.basic_logging() 
Example #13
Source File: configfiles.py    From qutebrowser with GNU General Public License v3.0 6 votes vote down vote up
def _handle_error(self, action: str, name: str) -> typing.Iterator[None]:
        """Catch config-related exceptions and save them in self.errors."""
        try:
            yield
        except configexc.ConfigFileErrors as e:
            for err in e.errors:
                new_err = err.with_text(e.basename)
                self.errors.append(new_err)
        except configexc.Error as e:
            text = "While {} '{}'".format(action, name)
            self.errors.append(configexc.ConfigErrorDesc(text, e))
        except urlmatch.ParseError as e:
            text = "While {} '{}' and parsing pattern".format(action, name)
            self.errors.append(configexc.ConfigErrorDesc(text, e))
        except keyutils.KeyParseError as e:
            text = "While {} '{}' and parsing key".format(action, name)
            self.errors.append(configexc.ConfigErrorDesc(text, e)) 
Example #14
Source File: utils.py    From hydrus with MIT License 6 votes vote down vote up
def set_hydrus_server_url(application: Flask, server_url: str) -> Iterator:
    """
    Set the server URL for the app (before it is run in main.py).
    :param application: Flask app object
            <flask.app.Flask>
    :param server_url : Server URL
            <str>

    Raises:
        TypeError: If the `server_url` is not a string.

    """
    if not isinstance(server_url, str):
        raise TypeError("The server_url is not of type <str>")

    def handler(sender: Flask, **kwargs: Any) -> None:
        g.hydrus_server_url = server_url
    with appcontext_pushed.connected_to(handler, application):
        yield 
Example #15
Source File: utils.py    From hydrus with MIT License 6 votes vote down vote up
def set_doc(application: Flask, APIDOC: HydraDoc) -> Iterator:
    """
    Set the API Documentation for the app (before it is run in main.py).
    :param application: Flask app object
            <flask.app.Flask>
    :param APIDOC : Hydra Documentation object
            <hydra_python_core.doc_writer.HydraDoc>

    Raises:
        TypeError: If `APIDOC` is not an instance of `HydraDoc`.

    """
    if not isinstance(APIDOC, HydraDoc):
        raise TypeError(
            "The API Doc is not of type <hydra_python_core.doc_writer.HydraDoc>")

    def handler(sender: Flask, **kwargs: Any) -> None:
        g.doc = APIDOC
    with appcontext_pushed.connected_to(handler, application):
        yield 
Example #16
Source File: utils.py    From hydrus with MIT License 6 votes vote down vote up
def set_pagination(application: Flask, paginate: bool) -> Iterator:
    """
    Enable or disable pagination.
    :param application: Flask app object
            <flask.app.Flask>
    :param paginate : Pagination enabled or not
            <bool>

    Raises:
        TypeError: If `paginate` is not a bool.

    """
    if not isinstance(paginate, bool):
        raise TypeError("The CLI argument 'pagination' is not of type <bool>")

    def handler(sender: Flask, **kwargs: Any) -> None:
        g.paginate = paginate
    with appcontext_pushed.connected_to(handler, application):
        yield 
Example #17
Source File: utils.py    From hydrus with MIT License 6 votes vote down vote up
def set_page_size(application: Flask, page_size: int) -> Iterator:
    """
    Set the page_size of a page view.
    :param application: Flask app object
            <flask.app.Flask>
    :param page_size : Number of maximum elements a page can contain
            <int>

    Raises:
        TypeError: If `page_size` is not an int.

    """
    if not isinstance(page_size, int):
        raise TypeError("The page_size is not of type <int>")

    def handler(sender: Flask, **kwargs: Any) -> None:
        g.page_size = page_size
    with appcontext_pushed.connected_to(handler, application):
        yield 
Example #18
Source File: utils.py    From hydrus with MIT License 6 votes vote down vote up
def set_api_name(application: Flask, api_name: str) -> Iterator:
    """
    Set the server name or EntryPoint for the app (before it is run in main.py).
    :param application: Flask app object
            <flask.app.Flask>
    :param api_name : API/Server name or EntryPoint
            <str>

    Raises:
        TypeError: If `api_name` is not a string.

    """
    if not isinstance(api_name, str):
        raise TypeError("The api_name is not of type <str>")

    def handler(sender: Flask, **kwargs: Any) -> None:
        g.api_name = api_name
    with appcontext_pushed.connected_to(handler, application):
        yield 
Example #19
Source File: utils.py    From hydrus with MIT License 6 votes vote down vote up
def set_authentication(application: Flask, authentication: bool) -> Iterator:
    """
    Set the whether API needs to be authenticated or not (before it is run in main.py).

    :param application: Flask app object
            <flask.app.Flask>
    :param authentication : Bool. API Auth needed or not
            <bool>


    Raises:
        TypeError: If `authentication` is not a boolean value.

    """
    if not isinstance(authentication, bool):
        raise TypeError("Authentication flag must be of type <bool>")

    def handler(sender: Flask, **kwargs: Any) -> None:
        g.authentication_ = authentication
    with appcontext_pushed.connected_to(handler, application):
        yield 
Example #20
Source File: loader.py    From qutebrowser with GNU General Public License v3.0 5 votes vote down vote up
def walk_components() -> typing.Iterator[ExtensionInfo]:
    """Yield ExtensionInfo objects for all modules."""
    if hasattr(sys, 'frozen'):
        yield from _walk_pyinstaller()
    else:
        yield from _walk_normal() 
Example #21
Source File: hints.py    From qutebrowser with GNU General Public License v3.0 5 votes vote down vote up
def filter_prefixes(
            self,
            hints: typing.Iterable[str],
            existing: typing.Iterable[str]
    ) -> typing.Iterator[str]:
        """Filter hints which don't start with the given prefix."""
        return (h for h in hints if not self.any_prefix(h, existing)) 
Example #22
Source File: webelem.py    From qutebrowser with GNU General Public License v3.0 5 votes vote down vote up
def __iter__(self) -> typing.Iterator[str]:
        raise NotImplementedError 
Example #23
Source File: webkitelem.py    From qutebrowser with GNU General Public License v3.0 5 votes vote down vote up
def __iter__(self) -> typing.Iterator[str]:
        self._check_vanished()
        yield from self._elem.attributeNames() 
Example #24
Source File: structures.py    From instaloader with MIT License 5 votes vote down vote up
def get_similar_accounts(self) -> Iterator['Profile']:
        """
        Retrieve list of suggested / similar accounts for this profile.
        To use this, one needs to be logged in.

        .. versionadded:: 4.4
        """
        if not self._context.is_logged_in:
            raise LoginRequiredException("--login required to get a profile's similar accounts.")
        self._obtain_metadata()
        yield from (Profile(self._context, edge["node"]) for edge in
                    self._context.graphql_query("ad99dd9d3646cc3c0dda65debcd266a7",
                                                {"user_id": str(self.userid), "include_chaining": True},
                                                "https://www.instagram.com/{0}/".format(self.username),
                                                self._rhx_gis)["data"]["user"]["edge_chaining"]["edges"]) 
Example #25
Source File: hints.py    From qutebrowser with GNU General Public License v3.0 5 votes vote down vote up
def extract_tag_words(
            self, elem: webelem.AbstractWebElement
    ) -> typing.Iterator[str]:
        """Extract tag words form the given element."""
        _extractor_type = typing.Callable[[webelem.AbstractWebElement], str]
        attr_extractors = {
            "alt": lambda elem: elem["alt"],
            "name": lambda elem: elem["name"],
            "title": lambda elem: elem["title"],
            "placeholder": lambda elem: elem["placeholder"],
            "src": lambda elem: elem["src"].split('/')[-1],
            "href": lambda elem: elem["href"].split('/')[-1],
            "text": str,
        }  # type: typing.Mapping[str, _extractor_type]

        extractable_attrs = collections.defaultdict(list, {
            "img": ["alt", "title", "src"],
            "a": ["title", "href", "text"],
            "input": ["name", "placeholder"],
            "textarea": ["name", "placeholder"],
            "button": ["text"]
        })

        return (attr_extractors[attr](elem)
                for attr in extractable_attrs[elem.tag_name()]
                if attr in elem or attr == "text") 
Example #26
Source File: configfiles.py    From qutebrowser with GNU General Public License v3.0 5 votes vote down vote up
def pattern(self, pattern: str) -> typing.Iterator[config.ConfigContainer]:
        """Get a ConfigContainer for the given pattern."""
        # We need to propagate the exception so we don't need to return
        # something.
        urlpattern = urlmatch.UrlPattern(pattern)
        container = config.ConfigContainer(config=self._config, configapi=self,
                                           pattern=urlpattern)
        yield container 
Example #27
Source File: keyutils.py    From qutebrowser with GNU General Public License v3.0 5 votes vote down vote up
def __iter__(self) -> typing.Iterator[KeyInfo]:
        """Iterate over KeyInfo objects."""
        for key_and_modifiers in self._iter_keys():
            key = Qt.Key(int(key_and_modifiers) & ~Qt.KeyboardModifierMask)
            modifiers = Qt.KeyboardModifiers(  # type: ignore[call-overload]
                int(key_and_modifiers) & Qt.KeyboardModifierMask)
            yield KeyInfo(key=key, modifiers=modifiers) 
Example #28
Source File: keyutils.py    From qutebrowser with GNU General Public License v3.0 5 votes vote down vote up
def _iter_keys(self) -> typing.Iterator[int]:
        sequences = typing.cast(typing.Iterable[typing.Iterable[int]],
                                self._sequences)
        return itertools.chain.from_iterable(sequences) 
Example #29
Source File: standarddir.py    From qutebrowser with GNU General Public License v3.0 5 votes vote down vote up
def _unset_organization() -> typing.Iterator[None]:
    """Temporarily unset QApplication.organizationName().

    This is primarily needed in config.py.
    """
    qapp = QApplication.instance()
    if qapp is not None:
        orgname = qapp.organizationName()
        qapp.setOrganizationName(None)  # type: ignore[arg-type]
    try:
        yield
    finally:
        if qapp is not None:
            qapp.setOrganizationName(orgname) 
Example #30
Source File: configtypes.py    From qutebrowser with GNU General Public License v3.0 5 votes vote down vote up
def __iter__(self) -> typing.Iterator[str]:
        return self.values.__iter__()