Python collections.abc.Mapping() Examples

The following are 30 code examples of collections.abc.Mapping(). 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: utils.py    From flask-smorest with MIT License 6 votes vote down vote up
def deepupdate(original, update):
    """Recursively update a dict.

    Subdict's won't be overwritten but also updated.
    """
    if not isinstance(original, abc.Mapping):
        return update
    for key, value in update.items():
        if isinstance(value, abc.Mapping):
            original[key] = deepupdate(original.get(key, {}), value)
        else:
            original[key] = value
    return original


# XXX: Does this belong here? 
Example #2
Source File: constants.py    From bot with MIT License 6 votes vote down vote up
def _recursive_update(original, new):
    """
    Helper method which implements a recursive `dict.update`
    method, used for updating the original configuration with
    configuration specified by the user.
    """

    for key, value in original.items():
        if key not in new:
            continue

        if isinstance(value, Mapping):
            if not any(isinstance(subvalue, Mapping) for subvalue in value.values()):
                original[key].update(new[key])
            _recursive_update(original[key], new[key])
        else:
            original[key] = new[key] 
Example #3
Source File: antispam.py    From bot with MIT License 6 votes vote down vote up
def validate_config(rules_: Mapping = AntiSpamConfig.rules) -> Dict[str, str]:
    """Validates the antispam configs."""
    validation_errors = {}
    for name, config in rules_.items():
        if name not in RULE_FUNCTION_MAPPING:
            log.error(
                f"Unrecognized antispam rule `{name}`. "
                f"Valid rules are: {', '.join(RULE_FUNCTION_MAPPING)}"
            )
            validation_errors[name] = f"`{name}` is not recognized as an antispam rule."
            continue
        for required_key in ('interval', 'max'):
            if required_key not in config:
                log.error(
                    f"`{required_key}` is required but was not "
                    f"set in rule `{name}`'s configuration."
                )
                validation_errors[name] = f"Key `{required_key}` is required but not set for rule `{name}`"
    return validation_errors 
Example #4
Source File: validation.py    From django-rest-registration with MIT License 6 votes vote down vote up
def run_validators(validators: Iterable[Validator], value: Any) -> None:
    fields_errors = OrderedDict()  # type: Dict[str, Any]
    non_field_errors = []  # type: List[Any]
    for validator in validators:
        try:
            validator(value)
        except ValidationError as exc:
            if isinstance(exc.detail, Mapping):
                for field_name, field_errors in exc.detail.items():
                    fields_errors.setdefault(field_name, []).extend(
                        field_errors)
            elif isinstance(exc.detail, list):
                non_field_errors.extend(exc.detail)

    if fields_errors:
        errors = {}
        errors.update(fields_errors)
        errors.setdefault(
            api_settings.NON_FIELD_ERRORS_KEY, []).extend(non_field_errors)
        raise ValidationError(errors)
    if non_field_errors:
        # TODO: Issue #109 - remove type: ignore
        raise ValidationError(non_field_errors)  # type: ignore 
Example #5
Source File: plugins.py    From flask-smorest with MIT License 6 votes vote down vote up
def path_helper(self, rule, operations, parameters, **kwargs):
        """Get path from flask Rule and set path parameters in operations"""

        for path_p in self.rule_to_params(rule):
            # If a parameter with same name and location is already
            # documented, update. Otherwise, append as new parameter.
            p_doc = next(
                (
                    p for p in parameters
                    if (
                        isinstance(p, Mapping) and
                        p['in'] == 'path' and
                        p['name'] == path_p['name']
                    )
                ),
                None
            )
            if p_doc is not None:
                # If parameter already documented, mutate to update doc
                # Ensure manual doc overwrites auto doc
                p_doc.update({**path_p, **p_doc})
            else:
                parameters.append(path_p)

        return self.flaskpath2openapi(rule.rule) 
Example #6
Source File: utils.py    From flask-smorest with MIT License 6 votes vote down vote up
def prepare_response(response, spec, default_response_content_type):
    """Rework response according to OAS version"""
    if isinstance(response, abc.Mapping):
        # OAS 2
        if spec.openapi_version.major < 3:
            if 'example' in response:
                response['examples'] = {
                    default_response_content_type: response.pop('example')
                }
        # OAS 3
        else:
            for field in ('schema', 'example', 'examples'):
                if field in response:
                    (
                        response
                        .setdefault('content', {})
                        .setdefault(default_response_content_type, {})
                        [field]
                    ) = response.pop(field) 
Example #7
Source File: utils.py    From GCNet with Apache License 2.0 6 votes vote down vote up
def cast_tensor_type(inputs, src_type, dst_type):
    if isinstance(inputs, torch.Tensor):
        return inputs.to(dst_type)
    elif isinstance(inputs, str):
        return inputs
    elif isinstance(inputs, np.ndarray):
        return inputs
    elif isinstance(inputs, abc.Mapping):
        return type(inputs)({
            k: cast_tensor_type(v, src_type, dst_type)
            for k, v in inputs.items()
        })
    elif isinstance(inputs, abc.Iterable):
        return type(inputs)(
            cast_tensor_type(item, src_type, dst_type) for item in inputs)
    else:
        return inputs 
Example #8
Source File: _collections.py    From gist-alfred with MIT License 6 votes vote down vote up
def extend(self, *args, **kwargs):
        """Generic import function for any type of header-like object.
        Adapted version of MutableMapping.update in order to insert items
        with self.add instead of self.__setitem__
        """
        if len(args) > 1:
            raise TypeError("extend() takes at most 1 positional "
                            "arguments ({0} given)".format(len(args)))
        other = args[0] if len(args) >= 1 else ()

        if isinstance(other, HTTPHeaderDict):
            for key, val in other.iteritems():
                self.add(key, val)
        elif isinstance(other, Mapping):
            for key in other:
                self.add(key, other[key])
        elif hasattr(other, "keys"):
            for key in other.keys():
                self.add(key, other[key])
        else:
            for key, value in other:
                self.add(key, value)

        for key, value in kwargs.items():
            self.add(key, value) 
Example #9
Source File: stream.py    From genielibs with Apache License 2.0 6 votes vote down vote up
def _asdict(self):
    d = {}
    for k in dir(self):
        if k.startswith('_'):
            continue
        try:
            v = getattr(self, k)
        except:
            continue
        if callable(v):
            continue
        if getattr(v, 'asdict', None):
            v = v.asdict()
        elif isinstance(v, abc.Mapping):
            v = {k2: v2.asdict() if getattr(v2, 'asdict', None) else v2
                 for k2, v2 in v.items()}
        d[k] = v
    return d 
Example #10
Source File: query.py    From hiku with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def visit_record(self, type_):
        if not isinstance(self.value, collections_abc.Mapping):
            raise _OptionTypeError(self.value, type_)

        unknown = set(self.value).difference(type_.__field_types__)
        if unknown:
            fields = ', '.join(sorted(map(repr, unknown)))
            raise _OptionError('unknown fields: {}'.format(fields))

        missing = set(type_.__field_types__).difference(self.value)
        if missing:
            fields = ', '.join(sorted(missing))
            raise _OptionError('missing fields: {}'.format(fields))

        for key, value_type in type_.__field_types__.items():
            with self.push(self.value[key]):
                self.visit(value_type) 
Example #11
Source File: _collections.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def extend(self, *args, **kwargs):
        """Generic import function for any type of header-like object.
        Adapted version of MutableMapping.update in order to insert items
        with self.add instead of self.__setitem__
        """
        if len(args) > 1:
            raise TypeError("extend() takes at most 1 positional "
                            "arguments ({0} given)".format(len(args)))
        other = args[0] if len(args) >= 1 else ()

        if isinstance(other, HTTPHeaderDict):
            for key, val in other.iteritems():
                self.add(key, val)
        elif isinstance(other, Mapping):
            for key in other:
                self.add(key, other[key])
        elif hasattr(other, "keys"):
            for key in other.keys():
                self.add(key, other[key])
        else:
            for key, value in other:
                self.add(key, value)

        for key, value in kwargs.items():
            self.add(key, value) 
Example #12
Source File: text_logger.py    From DenseMatchingBenchmark with MIT License 6 votes vote down vote up
def _checkout(self, item):
        if isinstance(item, container_abcs.Mapping):
            flag = True
            for key in item.keys():
                flag = flag and self._checkout(item[key])
            return flag

        if isinstance(item, container_abcs.Sequence):
            flag = True
            for val in item:
                flag = flag and self._checkout(val)
            return flag

        if isinstance(item, (np.ndarray)):
            if item.size > 1:
                return False
            else:
                return True
        if isinstance(item, (numbers.Number)):
            return True 
Example #13
Source File: tracing.py    From sentry-python with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def continue_from_headers(
        cls,
        headers,  # type: typing.Mapping[str, str]
        **kwargs  # type: Any
    ):
        # type: (...) -> Transaction
        if cls is Span:
            logger.warning(
                "Deprecated: use Transaction.continue_from_headers "
                "instead of Span.continue_from_headers."
            )
        parent = Transaction.from_traceparent(headers.get("sentry-trace"), **kwargs)
        if parent is None:
            parent = Transaction(**kwargs)
        parent.same_process_as_parent = False
        return parent 
Example #14
Source File: wrapper.py    From flask-apispec with MIT License 6 votes vote down vote up
def call_view(self, *args, **kwargs):
        config = flask.current_app.config
        parser = config.get('APISPEC_WEBARGS_PARSER', flaskparser.parser)
        annotation = utils.resolve_annotations(self.func, 'args', self.instance)
        if annotation.apply is not False:
            for option in annotation.options:
                schema = utils.resolve_schema(option['args'], request=flask.request)
                parsed = parser.parse(schema, locations=option['kwargs']['locations'])
                if getattr(schema, 'many', False):
                    args += tuple(parsed)
                elif isinstance(parsed, Mapping):
                    kwargs.update(parsed)
                else:
                    args += (parsed,)

        return self.func(*args, **kwargs) 
Example #15
Source File: pools.py    From python-pool-performance with MIT License 6 votes vote down vote up
def run_test(work_type: FunctionType, job_sets: Sequence, trials: int,
             pool_class: type, worker_count: int) -> Mapping:
    pool = pool_class(worker_count)
    if work_type == 'compute':
        test_func = pool.run_compute_test
    elif work_type == 'network':
        test_func = pool.run_network_test
    else:
        raise Exception("Invalid work type: {}".format(work_type))
    results = map(
        lambda jobs: test_func(jobs, trials, show_progress=True),
        tqdm(job_sets, desc=pool_class.__name__),
    )
    summarized_results = list(map(summarize_test, results))
    pool.destroy_pool()
    return summarized_results 
Example #16
Source File: test_client.py    From sentry-python with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test_mapping_sends_exception(sentry_init, capture_events):
    sentry_init()
    events = capture_events()

    class C(Mapping):
        def __iter__(self):
            try:
                1 / 0
            except ZeroDivisionError:
                capture_exception()
            yield "hi"

        def __len__(self):
            """List length"""
            return 1

        def __getitem__(self, ii):
            """Get a list item"""
            if ii == "hi":
                return "hi"

            raise KeyError()

    try:
        a = C()  # noqa
        1 / 0
    except Exception:
        capture_exception()

    (event,) = events

    assert event["exception"]["values"][0]["stacktrace"]["frames"][0]["vars"]["a"] == {
        "hi": "'hi'"
    } 
Example #17
Source File: _validate.py    From UWGeodynamics with GNU General Public License v3.0 5 votes vote down vote up
def _listify_validator(scalar_validator, allow_stringlist=False):
    def f(s):
        if isinstance(s, six.string_types):
            try:
                return [scalar_validator(v.strip()) for v in s.split(',')
                        if v.strip()]
            except Exception:
                if allow_stringlist:
                    # Sometimes, a list of colors might be a single string
                    # of single-letter colornames. So give that a shot.
                    return [scalar_validator(v.strip()) for v in s if v.strip()]
                else:
                    raise
        # We should allow any generic sequence type, including generators,
        # Numpy ndarrays, and pandas data structures.  However, unordered
        # sequences, such as sets, should be allowed but discouraged unless the
        # user desires pseudorandom behavior.
        elif isinstance(s, abc.Iterable) and not isinstance(s, abc.Mapping):
            # The condition on this list comprehension will preserve the
            # behavior of filtering out any empty strings (behavior was
            # from the original validate_stringlist()), while allowing
            # any non-string/text scalar values such as numbers and arrays.
            return [scalar_validator(v) for v in s
                    if not isinstance(v, six.string_types) or v]
        else:
            msg = "{0!r} must be of type: string or non-dictionary iterable.".format(s)
            raise ValueError(msg)
    f.__doc__ = scalar_validator.__doc__
    return f 
Example #18
Source File: utils.py    From dimod with Apache License 2.0 5 votes vote down vote up
def deserialize_ndarrays(obj):
    """Inverse of dfs_serialize_ndarray."""
    if isinstance(obj, abc.Mapping):
        if obj.get('type', '') == 'array':
            return deserialize_ndarray(obj)
        return {key: deserialize_ndarrays(val) for key, val in obj.items()}
    elif isinstance(obj, abc.Sequence) and not isinstance(obj, str):
        return list(map(deserialize_ndarrays, obj))
    return obj 
Example #19
Source File: sampleset.py    From dimod with Apache License 2.0 5 votes vote down vote up
def relabel_variables(self, mapping, inplace=True):
        """Relabel the variables of a :class:`SampleSet` according to the specified mapping.

        Args:
            mapping (dict):
                Mapping from current variable labels to new, as a dict. If incomplete mapping is
                specified, unmapped variables keep their current labels.

            inplace (bool, optional, default=True):
                If True, the current :class:`SampleSet` is updated; otherwise, a new
                :class:`SampleSet` is returned.

        Returns:
            :class:`.SampleSet`: SampleSet with relabeled variables. If `inplace` is True, returns
            itself.

        Examples:
            This example creates a relabeled copy of a :class:`SampleSet`.

            >>> sampleset = dimod.ExactSolver().sample_ising({'a': -0.5, 'b': 1.0}, {('a', 'b'): -1})
            >>> new_sampleset = sampleset.relabel_variables({'a': 0, 'b': 1}, inplace=False)
            >>> new_sampleset.variables
            Variables([0, 1])

        """
        if not inplace:
            return self.copy().relabel_variables(mapping, inplace=True)

        self.variables.relabel(mapping)
        return self 
Example #20
Source File: samples.py    From dimod with Apache License 2.0 5 votes vote down vote up
def __iter__(self):
        # Inherited __init__ puts the Mapping into self._mapping
        return zip(self._mapping._variables, self._mapping._data.flat) 
Example #21
Source File: tracing.py    From sentry-python with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def continue_from_environ(
        cls,
        environ,  # type: typing.Mapping[str, str]
        **kwargs  # type: Any
    ):
        # type: (...) -> Transaction
        if cls is Span:
            logger.warning(
                "Deprecated: use Transaction.continue_from_environ "
                "instead of Span.continue_from_environ."
            )
        return Transaction.continue_from_headers(EnvironHeaders(environ), **kwargs) 
Example #22
Source File: tracing.py    From sentry-python with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __init__(
        self,
        environ,  # type: typing.Mapping[str, str]
        prefix="HTTP_",  # type: str
    ):
        # type: (...) -> None
        self.environ = environ
        self.prefix = prefix 
Example #23
Source File: show_result.py    From DenseMatchingBenchmark with MIT License 5 votes vote down vote up
def vis_per_conf(self, Conf, color_map='gray'):
        error_msg = "Confidence must contain torch.Tensors or numpy.ndarray, dicts or lists; found {}"
        if isinstance(Conf, torch.Tensor):
            return self.conf2color(Conf.clone().detach().cpu().numpy(), color_map).transpose((2, 0, 1))
        elif isinstance(Conf, np.ndarray):
            return self.conf2color(Conf.copy(), color_map).transpose((2, 0, 1))
        elif isinstance(Conf, container_abcs.Mapping):
            return {key: self.vis_per_conf(Conf[key]) for key in Conf}
        elif isinstance(Conf, container_abcs.Sequence):
            return [self.vis_per_conf(samples) for samples in Conf]

        raise TypeError((error_msg.format(type(Conf)))) 
Example #24
Source File: test_client.py    From sentry-python with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def test_broken_mapping(sentry_init, capture_events):
    sentry_init()
    events = capture_events()

    class C(Mapping):
        def broken(self, *args, **kwargs):
            raise Exception("broken")

        __getitem__ = broken
        __setitem__ = broken
        __delitem__ = broken
        __iter__ = broken
        __len__ = broken

        def __repr__(self):
            return "broken"

    try:
        a = C()  # noqa
        1 / 0
    except Exception:
        capture_exception()

    (event,) = events
    assert (
        event["exception"]["values"][0]["stacktrace"]["frames"][0]["vars"]["a"]
        == "<failed to serialize, use init(debug=True) to see error logs>"
    ) 
Example #25
Source File: helpers.py    From poetry with MIT License 5 votes vote down vote up
def merge_dicts(d1, d2):
    for k, v in d2.items():
        if k in d1 and isinstance(d1[k], dict) and isinstance(d2[k], Mapping):
            merge_dicts(d1[k], d2[k])
        else:
            d1[k] = d2[k] 
Example #26
Source File: npyio.py    From lambda-packs with MIT License 5 votes vote down vote up
def __del__(self):
        self.close()

    # Implement the Mapping ABC 
Example #27
Source File: dataloader.py    From dgl with Apache License 2.0 5 votes vote down vote up
def __init__(self, g, nids, block_sampler):
        self.g = g
        if not isinstance(nids, Mapping):
            assert len(g.ntypes) == 1, \
                "nids should be a dict of node type and ids for graph with multiple node types"
        self.nids = nids
        self.block_sampler = block_sampler

        if isinstance(nids, Mapping):
            self._dataset = utils.FlattenedDict(nids)
        else:
            self._dataset = nids 
Example #28
Source File: utils.py    From dgl with Apache License 2.0 5 votes vote down vote up
def is_dict_like(obj):
    """Return true if the object can be treated as a dictionary."""
    return isinstance(obj, Mapping) 
Example #29
Source File: show_result.py    From DenseMatchingBenchmark with MIT License 5 votes vote down vote up
def vis_per_conf_hist(self, Conf, bins=100, ):
        def conf2hist2vis(array, bins):
            counts, bin_edges = self.conf2hist(array, bins)
            fig = self.hist2vis(counts, bin_edges)
            return fig

        error_msg = "Confidence must contain torch.Tensors or numpy.ndarray, dicts or lists; found {}"
        if isinstance(Conf, (torch.Tensor, np.ndarray)):
            return conf2hist2vis(Conf, bins)
        elif isinstance(Conf, container_abcs.Mapping):
            return {key: self.vis_per_conf_hist(Conf[key]) for key in Conf}
        elif isinstance(Conf, container_abcs.Sequence):
            return [self.vis_per_conf_hist(samples) for samples in Conf]

        raise TypeError((error_msg.format(type(Conf)))) 
Example #30
Source File: recdict.py    From MPContribs with MIT License 5 votes vote down vote up
def iterate(self, nested_dict=None):
        """http://stackoverflow.com/questions/10756427/loop-through-all-nested-dictionary-values"""
        d = self if nested_dict is None else nested_dict
        if nested_dict is None:
            self.level = 0
        for key in list(d.keys()):
            value = d[key]
            if isinstance(value, _Mapping):
                if value.get("@class") == "Structure":
                    from pymatgen import Structure

                    yield key, Structure.from_dict(value)
                    continue
                yield (self.level, key), None
                if value.get("@class") == "Table":
                    from mpcontribs.io.core.components.tdata import Table

                    yield key, Table.from_dict(value)
                    continue
                # if Quantity is not None and value.get('@class') == 'Quantity':
                #     quantity = Quantity.from_dict(value)
                #     yield key, quantity
                #     continue
                if "display" in value and "value" in value:  # 'unit' is optional
                    yield (self.level, key), value["display"]
                    continue
                self.level += 1
                for inner_key, inner_value in self.iterate(nested_dict=value):
                    yield inner_key, inner_value
                self.level -= 1
            else:
                yield (self.level, key), value

    # insertion mechanism from https://gist.github.com/jaredks/6276032