Python typing.MutableMapping() Examples

The following are 30 code examples of typing.MutableMapping(). 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: server_info.py    From CloudBot with GNU General Public License v3.0 6 votes vote down vote up
def _get_set_clear(
    mapping: MutableMapping[K, V],
    key: K,
    default_factory: Callable[[], V],
    *,
    clear: bool = False
) -> V:
    try:
        out = mapping[key]
    except KeyError:
        mapping[key] = out = default_factory()

    if clear:
        out.clear()

    return out 
Example #2
Source File: celery_executor.py    From airflow with Apache License 2.0 6 votes vote down vote up
def _get_many_using_multiprocessing(self, async_results) -> Mapping[str, EventBufferValueType]:
        num_process = min(len(async_results), self._sync_parallelism)

        with Pool(processes=num_process) as sync_pool:
            chunksize = max(1, math.floor(math.ceil(1.0 * len(async_results) / self._sync_parallelism)))

            task_id_to_states_and_info = sync_pool.map(
                fetch_celery_task_state,
                async_results,
                chunksize=chunksize)

            states_and_info_by_task_id: MutableMapping[str, EventBufferValueType] = {}
            for task_id, state_or_exception, info in task_id_to_states_and_info:
                if isinstance(state_or_exception, ExceptionWithTraceback):
                    self.log.error(  # pylint: disable=logging-not-lazy
                        CELERY_FETCH_ERR_MSG_HEADER + ":%s\n%s\n",
                        state_or_exception.exception, state_or_exception.traceback
                    )
                else:
                    states_and_info_by_task_id[task_id] = state_or_exception, info
        return states_and_info_by_task_id 
Example #3
Source File: test_typing.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
def test_subclassing(self):

        class MMA(typing.MutableMapping):
            pass

        with self.assertRaises(TypeError):  # It's abstract
            MMA()

        class MMC(MMA):
            def __len__(self):
                return 0

        assert len(MMC()) == 0

        class MMB(typing.MutableMapping[KT, VT]):
            def __len__(self):
                return 0

        assert len(MMB()) == 0
        assert len(MMB[str, str]()) == 0
        assert len(MMB[KT, VT]()) == 0 
Example #4
Source File: runners.py    From qutebrowser with GNU General Public License v3.0 6 votes vote down vote up
def replace_variables(win_id, arglist):
    """Utility function to replace variables like {url} in a list of args."""
    tabbed_browser = objreg.get('tabbed-browser', scope='window',
                                window=win_id)
    values = {}  # type: typing.MutableMapping[str, str]
    args = []

    def repl_cb(matchobj):
        """Return replacement for given match."""
        var = matchobj.group("var")
        if var not in values:
            values[var] = VARIABLE_REPLACEMENTS[var](tabbed_browser)
        return values[var]

    try:
        for arg in arglist:
            # using re.sub with callback function replaces all variables in a
            # single pass and avoids expansion of nested variables (e.g.
            # "{url}" from clipboard is not expanded)
            args.append(VARIABLE_REPLACEMENT_PATTERN.sub(repl_cb, arg))
    except utils.ClipboardError as e:
        raise cmdutils.CommandError(e)
    return args 
Example #5
Source File: merakianalyticscdn.py    From cassiopeia with MIT License 6 votes vote down vote up
def get_champion_all_rates(self, query: MutableMapping[str, Any], context: PipelineContext = None) -> ChampionAllRatesDto:
        try:
            return self._cache[ChampionRatesDto]
        except KeyError:
            pass
        url = "http://cdn.merakianalytics.com/riot/lol/resources/latest/en-US/championrates.json"
        try:
            body = self._client.get(url)[0]
            body = json.decode(body)
            body["data"] = {int(k): v for k, v in body["data"].items()}
        except HTTPError as e:
            raise NotFoundError(str(e)) from e

        result = ChampionAllRatesDto(**body)
        self._cache[ChampionRatesDto] = result
        return result 
Example #6
Source File: smartstack_tools.py    From paasta with Apache License 2.0 6 votes vote down vote up
def build_smartstack_location_dict(
    location: str,
    matched_backends_and_tasks: List[
        Tuple[
            Optional[HaproxyBackend],
            Optional[Union[marathon_tools.MarathonTask, V1Pod]],
        ]
    ],
    should_return_individual_backends: bool = False,
) -> MutableMapping[str, Any]:
    running_backends_count = 0
    backends = []
    for backend, task in matched_backends_and_tasks:
        if backend is None:
            continue
        if backend_is_up(backend):
            running_backends_count += 1
        if should_return_individual_backends:
            backends.append(build_smartstack_backend_dict(backend, task))

    return {
        "name": location,
        "running_backends_count": running_backends_count,
        "backends": backends,
    } 
Example #7
Source File: ddragon.py    From cassiopeia with MIT License 6 votes vote down vote up
def get_profile_icon(self, query: MutableMapping[str, Any], context: PipelineContext = None) -> ProfileIconDataDto:
        locale = query["locale"] if "locale" in query else query["platform"].default_locale

        url = "https://ddragon.leagueoflegends.com/cdn/{version}/data/{locale}/profileicon.json".format(
            version=query["version"],
            locale=locale
        )
        try:
            body = json.loads(self._client.get(url)[0])
        except HTTPError as e:
            raise NotFoundError(str(e)) from e

        body["region"] = query["platform"].region.value
        body["locale"] = locale
        body["version"] = query["version"]
        for pi in body["data"].values():
            pi["region"] = body["region"]
            pi["version"] = body["version"]
            pi["locale"] = locale
        return ProfileIconDataDto(body) 
Example #8
Source File: instance.py    From paasta with Apache License 2.0 6 votes vote down vote up
def get_mesos_non_running_task_dict(
    task: Task, num_tail_lines: int
) -> MutableMapping[str, Any]:
    if num_tail_lines > 0:
        tail_lines = await get_tail_lines_for_mesos_task(
            task, get_short_task_id, num_tail_lines
        )
    else:
        tail_lines = {}

    task_dict = {
        "id": get_short_task_id(task["id"]),
        "hostname": await results_or_unknown(get_short_hostname_from_task(task)),
        "state": task["state"],
        "tail_lines": tail_lines,
    }

    task_start_time = get_first_status_timestamp(task)
    if task_start_time is not None:
        task_dict["deployed_timestamp"] = task_start_time

    return task_dict 
Example #9
Source File: ddragon.py    From cassiopeia with MIT License 6 votes vote down vote up
def get_language_strings(self, query: MutableMapping[str, Any], context: PipelineContext = None) -> LanguageStringsDto:
        locale = query["locale"] if "locale" in query else query["platform"].default_locale

        url = "https://ddragon.leagueoflegends.com/cdn/{version}/data/{locale}/language.json".format(
            version=query["version"],
            locale=locale
        )
        try:
            body = json.loads(self._client.get(url)[0])
        except HTTPError as e:
            raise NotFoundError(str(e)) from e

        body["region"] = query["platform"].region.value
        body["locale"] = locale
        return LanguageStringsDto(body)

    #########
    # Runes #
    ######### 
Example #10
Source File: instance.py    From paasta with Apache License 2.0 6 votes vote down vote up
def _build_smartstack_location_dict_for_backends(
    synapse_host: str,
    registration: str,
    tasks: Sequence[MarathonTask],
    location: str,
    should_return_individual_backends: bool,
) -> MutableMapping[str, Any]:
    sorted_smartstack_backends = sorted(
        smartstack_tools.get_backends(
            registration,
            synapse_host=synapse_host,
            synapse_port=settings.system_paasta_config.get_synapse_port(),
            synapse_haproxy_url_format=settings.system_paasta_config.get_synapse_haproxy_url_format(),
        ),
        key=lambda backend: backend["status"],
        reverse=True,  # put 'UP' backends above 'MAINT' backends
    )
    matched_smartstack_backends_and_tasks = smartstack_tools.match_backends_and_tasks(
        sorted_smartstack_backends, tasks
    )
    return smartstack_tools.build_smartstack_location_dict(
        location,
        matched_smartstack_backends_and_tasks,
        should_return_individual_backends,
    ) 
Example #11
Source File: ddragon.py    From cassiopeia with MIT License 5 votes vote down vote up
def get_summoner_spell(self, query: MutableMapping[str, Any], context: PipelineContext = None) -> SummonerSpellDto:
        summoner_spells_query = copy.deepcopy(query)
        if "id" in summoner_spells_query:
            summoner_spells_query.pop("id")
        if "name" in summoner_spells_query:
            summoner_spells_query.pop("name")
        summoner_spells = context[context.Keys.PIPELINE].get(SummonerSpellListDto, query=summoner_spells_query)

        def find_matching_attribute(list_of_dtos, attrname, attrvalue):
            for dto in list_of_dtos:
                if dto.get(attrname, None) == attrvalue:
                    return dto

        # The `data` is a list of summoner_spell data instances
        if "id" in query:
            find = "id", query["id"]
        elif "name" in query:
            find = "name", query["name"]
        else:
            raise RuntimeError("Impossible!")
        summoner_spell = find_matching_attribute(summoner_spells["data"].values(), *find)
        if summoner_spell is None:
            raise NotFoundError
        summoner_spell["region"] = query["platform"].region.value
        summoner_spell["version"] = query["version"]
        if "locale" in query:
            summoner_spell["locale"] = query["locale"]
        if "includedData" in query:
            summoner_spell["includedData"] = query["includedData"]
        return SummonerSpellDto(summoner_spell) 
Example #12
Source File: test_typing.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def test_mutablemapping(self):
        assert isinstance({}, typing.MutableMapping)
        assert not isinstance(42, typing.MutableMapping) 
Example #13
Source File: ghost.py    From cassiopeia with MIT License 5 votes vote down vote up
def get_item(self, query: MutableMapping[str, Any], context: PipelineContext = None) -> Item:
        query["region"] = query.pop("platform").region
        query["includedData"] = query.pop("includedData")
        return Item._construct_normally(**query) 
Example #14
Source File: ghost.py    From cassiopeia with MIT License 5 votes vote down vote up
def get_rune(self, query: MutableMapping[str, Any], context: PipelineContext = None) -> Rune:
        query["region"] = query.pop("platform").region
        query["includedData"] = query.pop("includedData")
        return Rune._construct_normally(**query) 
Example #15
Source File: ghost.py    From cassiopeia with MIT License 5 votes vote down vote up
def get_champion(self, query: MutableMapping[str, Any], context: PipelineContext = None) -> Champion:
        query["region"] = query.pop("platform").region
        return Champion._construct_normally(**query) 
Example #16
Source File: ghost.py    From cassiopeia with MIT License 5 votes vote down vote up
def get(self, type: Type[T], query: MutableMapping[str, Any], context: PipelineContext = None) -> T:
        pass 
Example #17
Source File: merakianalyticscdn.py    From cassiopeia with MIT License 5 votes vote down vote up
def get_champion_rates(self, query: MutableMapping[str, Any], context: PipelineContext = None) -> ChampionRatesDto:
        data = self.get_champion_all_rates(query, context)
        rates = data["data"][query["id"]]
        return ChampionRatesDto(**rates) 
Example #18
Source File: merakianalyticscdn.py    From cassiopeia with MIT License 5 votes vote down vote up
def get_patch_list(self, query: MutableMapping[str, Any], context: PipelineContext = None) -> PatchListDto:
        url = "https://cdn.merakianalytics.com/riot/lol/resources/patches.json"
        try:
            body = self._client.get(url)[0]
            body = json.decode(body)
        except HTTPError as e:
            raise NotFoundError(str(e)) from e

        return PatchListDto(**body)


    ##################
    # Champion Rates #
    ################## 
Example #19
Source File: merakianalyticscdn.py    From cassiopeia with MIT License 5 votes vote down vote up
def get_many(self, type: Type[T], query: MutableMapping[str, Any], context: PipelineContext = None) -> Iterable[T]:
        pass 
Example #20
Source File: ddragon.py    From cassiopeia with MIT License 5 votes vote down vote up
def get_versions(self, query: MutableMapping[str, Any], context: PipelineContext = None) -> VersionListDto:
        url = "https://ddragon.leagueoflegends.com/api/versions.json"
        try:
            body = json.loads(self._client.get(url)[0])
        except HTTPError as e:
            raise NotFoundError(str(e)) from e

        return VersionListDto({
            "region": query["platform"].region.value,
            "versions": body
        })

    ##########
    # Realms #
    ########## 
Example #21
Source File: ddragon.py    From cassiopeia with MIT License 5 votes vote down vote up
def get(self, type: Type[T], query: MutableMapping[str, Any], context: PipelineContext = None) -> T:
        pass 
Example #22
Source File: envoy_tools.py    From paasta with Apache License 2.0 5 votes vote down vote up
def build_envoy_location_dict(
    location: str,
    matched_envoy_backends_and_tasks: List[
        Tuple[Optional[EnvoyBackend], Optional[marathon_tools.MarathonTask]]
    ],
    should_return_individual_backends: bool,
    casper_proxied_backends: AbstractSet[Tuple[str, int]],
) -> MutableMapping[str, Any]:
    running_backends_count = 0
    envoy_backends = []
    is_proxied_through_casper = False
    for backend, task in matched_envoy_backends_and_tasks:
        if backend is None:
            continue
        if backend["eds_health_status"] == "HEALTHY":
            running_backends_count += 1
        if should_return_individual_backends:
            backend["has_associated_task"] = task is not None
            envoy_backends.append(backend)
        if (backend["address"], backend["port_value"]) in casper_proxied_backends:
            is_proxied_through_casper = True
    return {
        "name": location,
        "running_backends_count": running_backends_count,
        "backends": envoy_backends,
        "is_proxied_through_casper": is_proxied_through_casper,
    } 
Example #23
Source File: kubernetes_tools.py    From paasta with Apache License 2.0 5 votes vote down vote up
def get_active_shas_for_service(
    obj_list: Sequence[Union[V1Pod, V1ReplicaSet, V1Deployment, V1StatefulSet]],
) -> Mapping[str, Set[str]]:
    ret: MutableMapping[str, Set[str]] = {"config_sha": set(), "git_sha": set()}
    for obj in obj_list:
        config_sha = obj.metadata.labels.get("paasta.yelp.com/config_sha")
        if config_sha is not None:
            ret["config_sha"].add(config_sha)
        git_sha = obj.metadata.labels.get("paasta.yelp.com/git_sha")
        if git_sha is not None:
            ret["git_sha"].add(git_sha)
    return ret 
Example #24
Source File: kubernetes_tools.py    From paasta with Apache License 2.0 5 votes vote down vote up
def get_tail_lines_for_kubernetes_container(
    kube_client: KubeClient,
    pod: V1Pod,
    container: V1ContainerStatus,
    num_tail_lines: int,
) -> MutableMapping[str, Any]:
    tail_lines: MutableMapping[str, Any] = {
        "stdout": [],
        "stderr": [],
        "error_message": "",
    }

    if container.name != HACHECK_POD_NAME:
        error = ""
        if container.state.waiting:
            error = container.state.waiting.message or ""
        elif container.state.terminated:
            error = container.state.terminated.message or ""
        tail_lines["error_message"] = error

        try:
            if num_tail_lines > 0:
                log = kube_client.core.read_namespaced_pod_log(
                    name=pod.metadata.name,
                    namespace=pod.metadata.namespace,
                    container=container.name,
                    tail_lines=num_tail_lines,
                )
                tail_lines["stdout"].extend(log.split("\n"))
        except ApiException as e:
            # there is a potential race condition in which a pod's containers
            # have not failed, but have when we get the container's logs. in this
            # case, use the error from the exception, though it is less accurate.
            if error == "":
                body = json.loads(e.body)
                error = body.get("message", "")
            tail_lines["error_message"] = f"couldn't read stdout/stderr: '{error}'"

    return tail_lines 
Example #25
Source File: instance.py    From paasta with Apache License 2.0 5 votes vote down vote up
def build_marathon_task_dict(marathon_task: MarathonTask) -> MutableMapping[str, Any]:
    task_dict = {
        "id": get_short_task_id(marathon_task.id),
        "host": marathon_task.host.split(".")[0],
        "port": marathon_task.ports[0],
        "deployed_timestamp": marathon_task.staged_at.timestamp(),
    }

    if marathon_task.health_check_results:
        task_dict["is_healthy"] = marathon_tools.is_task_healthy(marathon_task)

    return task_dict 
Example #26
Source File: instance.py    From paasta with Apache License 2.0 5 votes vote down vote up
def marathon_app_status(
    app: MarathonApp,
    marathon_client: MarathonClient,
    dashboard_link: Optional[str],
    deploy_status: int,
    list_tasks: bool = False,
) -> MutableMapping[str, Any]:
    app_status = {
        "tasks_running": app.tasks_running,
        "tasks_healthy": app.tasks_healthy,
        "tasks_staged": app.tasks_staged,
        "tasks_total": app.instances,
        "create_timestamp": isodate.parse_datetime(app.version).timestamp(),
        "deploy_status": marathon_tools.MarathonDeployStatus.tostring(deploy_status),
    }

    app_queue = marathon_tools.get_app_queue(marathon_client, app.id)
    if deploy_status == marathon_tools.MarathonDeployStatus.Delayed:
        _, backoff_seconds = marathon_tools.get_app_queue_status_from_queue(app_queue)
        app_status["backoff_seconds"] = backoff_seconds

    unused_offers_summary = marathon_tools.summarize_unused_offers(app_queue)
    if unused_offers_summary is not None:
        app_status["unused_offers"] = unused_offers_summary

    if dashboard_link:
        app_status["dashboard_url"] = "{}/ui/#/apps/%2F{}".format(
            dashboard_link.rstrip("/"), app.id.lstrip("/")
        )

    if list_tasks is True:
        app_status["tasks"] = []
        for task in app.tasks:
            app_status["tasks"].append(build_marathon_task_dict(task))

    return app_status 
Example #27
Source File: smartstack_tools.py    From paasta with Apache License 2.0 5 votes vote down vote up
def build_smartstack_backend_dict(
    smartstack_backend: HaproxyBackend,
    task: Union[V1Pod, Optional[marathon_tools.MarathonTask]],
) -> MutableMapping[str, Any]:
    svname = smartstack_backend["svname"]
    if isinstance(task, V1Pod):
        node_hostname = svname.split("_")[0]
        pod_ip = svname.split("_")[1].split(":")[0]
        hostname = f"{node_hostname}:{pod_ip}"
    else:
        hostname = svname.split("_")[0]
    port = svname.split("_")[-1].split(":")[-1]

    smartstack_backend_dict = {
        "hostname": hostname,
        "port": int(port),
        "status": smartstack_backend["status"],
        "check_status": smartstack_backend["check_status"],
        "check_code": smartstack_backend["check_code"],
        "last_change": int(smartstack_backend["lastchg"]),
        "has_associated_task": task is not None,
    }

    check_duration = smartstack_backend["check_duration"]
    if check_duration:
        smartstack_backend_dict["check_duration"] = int(check_duration)

    return smartstack_backend_dict 
Example #28
Source File: device.py    From btle-sniffer with MIT License 5 votes vote down vote up
def __init__(self, uuid: str, value: Optional[Sequence[int]],
                 flags: Sequence[str]):
        self.uuid = uuid
        self.value = value
        self.flags = flags
        self.descriptors: MutableMapping[str, GATTDescriptor] = dict() 
Example #29
Source File: util.py    From qiskit-terra with Apache License 2.0 5 votes vote down vote up
def swap_permutation(swaps: Iterable[Iterable[Swap[_K]]],
                     mapping: MutableMapping[_K, _V],
                     allow_missing_keys: bool = False) -> None:
    """Given a circuit of swaps, apply them to the permutation (in-place).

    Args:
      swaps: param mapping: A mapping of Keys to Values, where the Keys are being swapped.
      mapping: The permutation to have swaps applied to.
      allow_missing_keys: Whether to allow swaps of missing keys in mapping.
    """
    for swap_step in swaps:
        for sw1, sw2 in swap_step:
            # Take into account non-existent keys.
            val1 = None  # type: Optional[_V]
            val2 = None  # type: Optional[_V]
            if allow_missing_keys:
                val1 = mapping.pop(sw1, None)
                val2 = mapping.pop(sw2, None)
            else:
                # Asserts that both keys exist
                val1, val2 = mapping.pop(sw1), mapping.pop(sw2)

            if val1 is not None:
                mapping[sw2] = val1
            if val2 is not None:
                mapping[sw1] = val2 
Example #30
Source File: formatter.py    From fuzz-lightyear with Apache License 2.0 5 votes vote down vote up
def format_summary(
    stats: MutableMapping[str, int],
    timing: timedelta,
) -> str:
    num_passed = stats['success']
    num_failures = stats['failure']
    num_warnings = stats['warnings']

    summary = []
    if num_passed:
        summary.append(f'{num_passed} passed')
        color = AnsiColor.LIGHT_GREEN

    if num_warnings:
        summary.append(f'{num_warnings} warnings')
        color = AnsiColor.YELLOW

    if num_failures:
        summary = [f'{num_failures} failed', *summary]
        color = AnsiColor.RED

    if not summary:
        return colorize(format_header('No tests run!'), AnsiColor.BOLD)

    summary_string = '{} in {} seconds'.format(
        ', '.join(summary),
        round(timing.total_seconds(), 2),
    )

    return colorize(
        colorize(
            format_header(summary_string),
            color,
        ),
        AnsiColor.BOLD,
    )