Python typing.Iterable() Examples

The following are 30 code examples of typing.Iterable(). 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: filters.py    From mutatest with MIT License 7 votes vote down vote up
def __init__(self, codes: Optional[Iterable[str]] = None):
        """Initialize the filter.

        Args:
            codes: An optional iterable of two-letter category codes for filtering.
                Optional to set at initialization of the class, can be set through properties.
                The codes property must be set prior to filtering.
                Only codes that are valid categories are added, others are discarded.
                Make sure you set appropriately as an iterable for single string values e.g.,
                ``codes=("bn",)``; otherwise, the codes property will set as empty.
        """
        # managed by class properties, no direct setters
        self._valid_categories = CATEGORIES  # defined in transformers.py
        self._codes: Set[str] = set()

        # initialize through properties
        self.codes = set(codes) if codes else set() 
Example #2
Source File: run_checks.py    From OpenFermion-Cirq with Apache License 2.0 7 votes vote down vote up
def topologically_sorted_checks_with_deps(checks: Iterable[check.Check]
                                          ) -> List[check.Check]:
    result = []
    seen = set()  # type: Set[check.Check]

    def handle(item: check.Check):
        if item in seen:
            return
        seen.add(item)

        # Dependencies first.
        for dep in item.dependencies:
            handle(dep)

        result.append(item)

    for e in checks:
        handle(e)

    return result 
Example #3
Source File: swap_network_trotter.py    From OpenFermion-Cirq with Apache License 2.0 6 votes vote down vote up
def params(self) -> Iterable[sympy.Symbol]:
        """The parameters of the ansatz."""
        for i in range(self.iterations):
            for p in range(len(self.qubits)):
                if (self.include_all_z or not
                        numpy.isclose(self.hamiltonian.one_body[p, p], 0)):
                    yield LetterWithSubscripts('U', p, i)
            for p, q in itertools.combinations(range(len(self.qubits)), 2):
                if (self.include_all_xxyy or not
                        numpy.isclose(self.hamiltonian.one_body[p, q].real, 0)):
                    yield LetterWithSubscripts('T', p, q, i)
                if (self.include_all_yxxy or not
                        numpy.isclose(self.hamiltonian.one_body[p, q].imag, 0)):
                    yield LetterWithSubscripts('W', p, q, i)
                if (self.include_all_cz or not
                        numpy.isclose(self.hamiltonian.two_body[p, q], 0)):
                    yield LetterWithSubscripts('V', p, q, i) 
Example #4
Source File: utils.py    From pyfilter with MIT License 6 votes vote down vote up
def _mcmc_move(params: Iterable[Parameter], dist: Distribution, stacked: StackedObject, shape: int):
    """
    Performs an MCMC move to rejuvenate parameters.
    :param params: The parameters to use for defining the distribution
    :param dist: The distribution to use for sampling
    :param stacked: The mask to apply for parameters
    :param shape: The shape to sample
    :return: Samples from a multivariate normal distribution
    """

    rvs = dist.sample((shape,))

    for p, msk, ps in zip(params, stacked.mask, stacked.prev_shape):
        p.t_values = unflattify(rvs[:, msk], ps)

    return True 
Example #5
Source File: base.py    From pyfilter with MIT License 6 votes vote down vote up
def update(self, parameters: Iterable[Parameter], filter_: BaseFilter, weights: torch.Tensor):
        """
        Defines the function for updating the parameters.
        :param parameters: The parameters of the model to update
        :param filter_: The filter
        :param weights: The weights to use for propagating.
        :return: Self
        """

        w = normalize(weights)

        if self._record_stats:
            self.record_stats(parameters, w)

        self._update(parameters, filter_, w)

        return self 
Example #6
Source File: shamir.py    From python-shamir-mnemonic with MIT License 6 votes vote down vote up
def combine_mnemonics(mnemonics: Iterable[str], passphrase: bytes = b"") -> bytes:
    """
    Combine mnemonic shares to obtain the master secret which was previously split
    using Shamir's secret sharing scheme.

    This is the user-friendly method to recover a backed-up secret optionally protected
    by a passphrase.

    :param mnemonics: List of mnemonics.
    :param passphrase: The passphrase used to encrypt the master secret.
    :return: The master secret.
    """

    if not mnemonics:
        raise MnemonicError("The list of mnemonics is empty.")

    identifier, iteration_exponent, encrypted_master_secret = recover_ems(mnemonics)
    return cipher.decrypt(
        encrypted_master_secret, passphrase, iteration_exponent, identifier,
    ) 
Example #7
Source File: rs1024.py    From python-shamir-mnemonic with MIT License 6 votes vote down vote up
def _polymod(values: Iterable[int]) -> int:
    GEN = (
        0xE0E040,
        0x1C1C080,
        0x3838100,
        0x7070200,
        0xE0E0009,
        0x1C0C2412,
        0x38086C24,
        0x3090FC48,
        0x21B1F890,
        0x3F3F120,
    )
    chk = 1
    for v in values:
        b = chk >> 20
        chk = (chk & 0xFFFFF) << 10 ^ v
        for i in range(10):
            chk ^= GEN[i] if ((b >> i) & 1) else 0
    return chk 
Example #8
Source File: predicate.py    From python-clean-architecture with MIT License 6 votes vote down vote up
def _build_predicate(
            self,
            test: t.Callable,
            operation: Operation,
            args: t.Iterable
            ) -> Predicate:
        """
        Generate a Predicate object based on a test function.

        :param test: The test the Predicate executes.
        :param operation: An `Operation` instance for the Predicate.
        :return: A `Predicate` object
        """
        if not self._path:
            raise ValueError('Var has no path')
        return Predicate(
            check_path(test, self._path),
            operation,
            args,
            self._name
        ) 
Example #9
Source File: operators.py    From python-clean-architecture with MIT License 6 votes vote down vote up
def check_path(
    test: t.Callable[[t.Any, t.Any], bool],
    path: t.Iterable[str]
) -> t.Callable[..., bool]:
    def check_path_curried(value):
        orig_value = value
        for part in path:
            try:
                value = getattr(value, part)
            except AttributeError:
                try:
                    value = value[part]
                except (KeyError, TypeError):
                    return False
        return test(value, orig_value)

    return check_path_curried 
Example #10
Source File: env_tools.py    From OpenFermion-Cirq with Apache License 2.0 6 votes vote down vote up
def get_unhidden_ungenerated_python_files(directory: str) -> Iterable[str]:
    """Iterates through relevant python files within the given directory.

    Args:
        directory: The top-level directory to explore.

    Yields:
        File paths.
    """
    for dirpath, dirnames, filenames in os.walk(directory, topdown=True):
        if os.path.split(dirpath)[-1].startswith('.'):
            dirnames.clear()
            continue

        for filename in filenames:
            if filename.endswith('.py') and not filename.endswith('_pb2.py'):
                yield os.path.join(dirpath, filename) 
Example #11
Source File: kaldi_io.py    From audio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def read_vec_int_ark(file_or_fd: Any) -> Iterable[Tuple[str, Tensor]]:
    r"""Create generator of (key,vector<int>) tuples, which reads from the ark file/stream.

    Args:
        file_or_fd (str/FileDescriptor): ark, gzipped ark, pipe or opened file descriptor

    Returns:
        Iterable[Tuple[str, Tensor]]: The string is the key and the tensor is the vector read from file

    Example
        >>> # read ark to a 'dictionary'
        >>> d = { u:d for u,d in torchaudio.kaldi_io.read_vec_int_ark(file) }
    """
    # Requires convert_contiguous to be True because elements from int32 vector are
    # sored in tuples: (sizeof(int32), value) so strides are (5,) instead of (4,) which will throw an error
    # in from_numpy as it expects strides to be a multiple of 4 (int32).
    return _convert_method_output_to_tensor(file_or_fd, kaldi_io.read_vec_int_ark, convert_contiguous=True) 
Example #12
Source File: kaldi_io.py    From audio with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def _convert_method_output_to_tensor(file_or_fd: Any,
                                     fn: Callable,
                                     convert_contiguous: bool = False) -> Iterable[Tuple[str, Tensor]]:
    r"""Takes a method invokes it. The output is converted to a tensor.

    Args:
        file_or_fd (str/FileDescriptor): File name or file descriptor
        fn (Callable): Function that has the signature (file name/descriptor) and converts it to
            Iterable[Tuple[str, Tensor]].
        convert_contiguous (bool, optional): Determines whether the array should be converted into a
            contiguous layout. (Default: ``False``)

    Returns:
        Iterable[Tuple[str, Tensor]]: The string is the key and the tensor is vec/mat
    """
    for key, np_arr in fn(file_or_fd):
        if convert_contiguous:
            np_arr = np.ascontiguousarray(np_arr)
        yield key, torch.from_numpy(np_arr) 
Example #13
Source File: routing.py    From quart with MIT License 6 votes vote down vote up
def __init__(
        self,
        string: str,
        defaults: Optional[dict] = None,
        subdomain: Optional[str] = None,
        methods: Optional[Iterable[str]] = None,
        endpoint: Optional[str] = None,
        strict_slashes: Optional[bool] = None,
        merge_slashes: Optional[bool] = None,
        host: Optional[str] = None,
        websocket: bool = False,
        provide_automatic_options: bool = False,
    ) -> None:
        super().__init__(  # type: ignore
            string,
            defaults=defaults,
            subdomain=subdomain,
            methods=methods,
            endpoint=endpoint,
            strict_slashes=strict_slashes,
            merge_slashes=merge_slashes,
            host=host,
            websocket=websocket,
        )
        self.provide_automatic_options = provide_automatic_options 
Example #14
Source File: base_text_preprocessor.py    From interpret-text with MIT License 5 votes vote down vote up
def decode_single(self, id_list: Iterable[Any]) -> Iterable[Any]:
        """ Decodes a single list of token ids to tokens
        :param id_list: list of token ids
        :type id_list: List
        :return: list of tokens
        :rtype list
        """
        pass 
Example #15
Source File: wallet.py    From indy-anoncreds with Apache License 2.0 5 votes vote down vote up
def getAllSchemas(self) -> Iterable[Schema]:
        return self._schemasByKey.values() 
Example #16
Source File: core.py    From lineflow with MIT License 5 votes vote down vote up
def __init__(self, iterable: Iterable) -> None:
        self._length = None
        self._iterable = iterable
        self._computed = False 
Example #17
Source File: kaldi_io.py    From audio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def read_vec_flt_scp(file_or_fd: Any) -> Iterable[Tuple[str, Tensor]]:
    r"""Create generator of (key,vector<float32/float64>) tuples, read according to Kaldi scp.

    Args:
        file_or_fd (str/FileDescriptor): scp, gzipped scp, pipe or opened file descriptor

    Returns:
        Iterable[Tuple[str, Tensor]]: The string is the key and the tensor is the vector read from file

    Example
        >>> # read scp to a 'dictionary'
        >>> # d = { u:d for u,d in torchaudio.kaldi_io.read_vec_flt_scp(file) }
    """
    return _convert_method_output_to_tensor(file_or_fd, kaldi_io.read_vec_flt_scp) 
Example #18
Source File: core.py    From lineflow with MIT License 5 votes vote down vote up
def lineflow_flat_map(
        map_func: Callable[[Iterable[Any]], Any],
        dataset: DatasetMixin,
        lazy: bool = False) -> Union[Iterator[Any], List[Any]]:
    iterator = chain.from_iterable(map(map_func, dataset))
    if lazy:
        return iterator
    else:
        return list(iterator) 
Example #19
Source File: utils.py    From pyfilter with MIT License 5 votes vote down vote up
def stacker(parameters: Iterable[Parameter], selector=lambda u: u.values, dim=1):
    """
    Stacks the parameters and returns a n-tuple containing the mask for each parameter.
    :param parameters: The parameters
    :type parameters: tuple[Parameter]|list[Parameter]
    :param selector: The selector
    :param dim: The dimension to start flattening from
    """

    to_conc = tuple()
    mask = tuple()
    prev_shape = tuple()

    i = 0
    # TODO: Currently only supports one sampling dimension...
    for p in parameters:
        s = selector(p)
        flat = s if s.dim() <= dim else s.flatten(dim)

        if flat.dim() == dim:
            to_conc += (flat.unsqueeze(-1),)
            slc = i
        else:
            to_conc += (flat,)
            slc = slice(i, i + flat.shape[-1])

        mask += (slc,)
        i += to_conc[-1].shape[-1]
        prev_shape += (s.shape[dim:],)

    return StackedObject(torch.cat(to_conc, dim=-1), mask, prev_shape) 
Example #20
Source File: inputs.py    From NGU-scripts with GNU Lesser General Public License v3.0 5 votes vote down vote up
def check_pixel_color(x :int, y :int, checks :Iterable[str], debug :bool =False) -> bool:
        """Check if coordinate matches with one or more colors."""
        color = Inputs.get_pixel_color(x, y, debug=debug)
        if isinstance(checks, list):
            for check in checks:
                if check == color:
                    return True

        return color == checks 
Example #21
Source File: utils.py    From pyfilter with MIT License 5 votes vote down vote up
def _eval_kernel(params: Iterable[Parameter], dist: Distribution, n_params: Iterable[Parameter]):
    """
    Evaluates the kernel used for performing the MCMC move.
    :param params: The current parameters
    :param dist: The distribution to use for evaluating the prior
    :param n_params: The new parameters to evaluate against
    :return: The log difference in priors
    """

    p_vals = stacker(params, lambda u: u.t_values)
    n_p_vals = stacker(n_params, lambda u: u.t_values)

    return dist.log_prob(p_vals.concated) - dist.log_prob(n_p_vals.concated) 
Example #22
Source File: cli.py    From quart with MIT License 5 votes vote down vote up
def list_commands(self, ctx: click.Context) -> Iterable[str]:
        commands = set(click.Group.list_commands(self, ctx))
        info = ctx.ensure_object(ScriptInfo)
        commands.update(info.load_app().cli.list_commands(ctx))
        return commands 
Example #23
Source File: blueprints.py    From quart with MIT License 5 votes vote down vote up
def add_url_rule(
        self,
        path: str,
        endpoint: Optional[str] = None,
        view_func: Optional[Callable] = None,
        *,
        methods: Optional[Iterable[str]] = None,
        defaults: Optional[dict] = None,
        host: Optional[str] = None,
        subdomain: Optional[str] = None,
        provide_automatic_options: Optional[bool] = None,
        is_websocket: bool = False,
        strict_slashes: Optional[bool] = None,
    ) -> None:
        if self.url_prefix is not None:
            path = f"{self.url_prefix}{path}"
        if subdomain is None:
            subdomain = self.subdomain
        endpoint = f"{self.blueprint.name}.{endpoint}"
        url_defaults = self.url_defaults
        if defaults is not None:
            url_defaults = {**url_defaults, **defaults}
        self.app.add_url_rule(
            path,
            endpoint,
            view_func,
            provide_automatic_options=provide_automatic_options,
            methods=methods,
            defaults=url_defaults,
            host=host,
            subdomain=subdomain,
            is_websocket=is_websocket,
            strict_slashes=strict_slashes,
        ) 
Example #24
Source File: exceptions.py    From quart with MIT License 5 votes vote down vote up
def __init__(self, allowed_methods: Optional[Iterable[str]] = None) -> None:
        super().__init__(HTTPStatus.METHOD_NOT_ALLOWED)
        self.allowed_methods = allowed_methods 
Example #25
Source File: misc.py    From tfont with Apache License 2.0 5 votes vote down vote up
def transformSequence(self, sequence: Iterable,
                          selectionOnly: bool = False) -> bool:
        changed = False
        for element in sequence:
            doTransform = not selectionOnly or element.selected
            changed |= doTransform
            if doTransform:
                x, y = element.x, element.y
                element.x = x * self.xScale + y * self.yxScale + self.xOffset
                element.y = y * self.yScale + x * self.xyScale + self.yOffset
        return changed 
Example #26
Source File: runner.py    From fishroom with GNU General Public License v3.0 5 votes vote down vote up
def run_threads(thread_target_args: Iterable[Tuple[AnyFunc, AnyArgs]]):
    from .telegram import Telegram
    from .config import config

    tasks = []
    DEAD = threading.Event()

    # wrapper to send report traceback info to telegram
    def die(f: AnyFunc):
        tg = Telegram(config["telegram"]["token"])
        logger = get_logger(__name__)

        def send_all(text):
            for adm in config["telegram"]["admin"]:
                try:
                    tg.send_msg(adm, text, escape=False)
                except:
                    pass

        def wrapper(*args, **kwargs):
            try:
                f(*args, **kwargs)
            except:
                logger.exception("thread failed")
                exc = traceback.format_exc()
                send_all("<code>{}</code>".format(exc))
                DEAD.set()

        return wrapper

    for target, args in thread_target_args:
        t = threading.Thread(target=die(target), args=args)
        t.setDaemon(True)
        t.start()
        tasks.append(t)

    DEAD.wait()
    logger.error("Everybody died, I don't wanna live any more! T_T")
    os._exit(1) 
Example #27
Source File: base.py    From pyfilter with MIT License 5 votes vote down vote up
def _update(self, parameters: Iterable[Parameter], filter_: BaseFilter, weights: torch.Tensor):
        """
        The method for the user to override.
        :param parameters: The parameters of the model to update
        :param filter_: The filter
        :param weights: The weights to be passed. A normalized copy of log_weights
        :return: Self
        """

        raise NotImplementedError() 
Example #28
Source File: example_classes.py    From OpenFermion-Cirq with Apache License 2.0 5 votes vote down vote up
def params(self) -> Iterable[sympy.Symbol]:
        for i in range(2):
            yield sympy.Symbol('theta{}'.format(i)) 
Example #29
Source File: split_operator_trotter.py    From OpenFermion-Cirq with Apache License 2.0 5 votes vote down vote up
def params(self) -> Iterable[sympy.Symbol]:
        """The names of the parameters of the ansatz."""
        for i in range(self.iterations):
            for p in range(len(self.qubits)):
                if (self.include_all_z or not
                        numpy.isclose(self.orbital_energies[p], 0)):
                    yield LetterWithSubscripts('U', p, i)
            for p, q in itertools.combinations(range(len(self.qubits)), 2):
                if (self.include_all_cz or not
                        numpy.isclose(self.hamiltonian.two_body[p, q], 0)):
                    yield LetterWithSubscripts('V', p, q, i) 
Example #30
Source File: kaldi_io.py    From audio with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def read_vec_flt_ark(file_or_fd: Any) -> Iterable[Tuple[str, Tensor]]:
    r"""Create generator of (key,vector<float32/float64>) tuples, which reads from the ark file/stream.

    Args:
        file_or_fd (str/FileDescriptor): ark, gzipped ark, pipe or opened file descriptor

    Returns:
        Iterable[Tuple[str, Tensor]]: The string is the key and the tensor is the vector read from file

    Example
        >>> # read ark to a 'dictionary'
        >>> d = { u:d for u,d in torchaudio.kaldi_io.read_vec_flt_ark(file) }
    """
    return _convert_method_output_to_tensor(file_or_fd, kaldi_io.read_vec_flt_ark)