Python collections.Mapping() Examples
The following are 30
code examples of collections.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
, or try the search function
.

Example #1
Source File: config.py From esmlab with Apache License 2.0 | 7 votes |
def normalize_nested_keys(config): """ Replaces underscores with hyphens for keys for a nested Mapping Examples -------- >>> a = {'x': 1, 'y_1': {'a_2': 2}} >>> normalize_nested_keys(a) {'x': 1, 'y-1': {'a-2': 2}} """ config_norm = {} for key, value in config.items(): if isinstance(value, Mapping): value = normalize_nested_keys(value) key_norm = normalize_key(key) config_norm[key_norm] = value return config_norm
Example #2
Source File: foundation.py From django-accounting with MIT License | 7 votes |
def update(d, u, depth=-1): """ Recursively merge or update dict-like objects. >>> update({'k1': {'k2': 2}}, {'k1': {'k2': {'k3': 3}}, 'k4': 4}) {'k1': {'k2': {'k3': 3}}, 'k4': 4} """ for k, v in u.iteritems(): if isinstance(v, Mapping) and not depth == 0: r = update(d.get(k, {}), v, depth=max(depth - 1, -1)) d[k] = r elif isinstance(d, Mapping): d[k] = u[k] else: d = {k: u[k]} return d
Example #3
Source File: geocoder.py From python-opencage-geocoder with MIT License | 6 votes |
def floatify_latlng(input_value): """ Work around a JSON dict with string, not float, lat/lngs. Given anything (list/dict/etc) it will return that thing again, *but* any dict (at any level) that has only 2 elements lat & lng, will be replaced with the lat & lng turned into floats. If the API returns the lat/lng as strings, and not numbers, then this function will 'clean them up' to be floats. """ if isinstance(input_value, collections.Mapping): if len(input_value) == 2 and sorted(input_value.keys()) == ['lat', 'lng']: # This dict has only 2 keys 'lat' & 'lon' return {'lat': float_if_float(input_value["lat"]), 'lng': float_if_float(input_value["lng"])} else: return dict((key, floatify_latlng(value)) for key, value in input_value.items()) elif isinstance(input_value, collections.MutableSequence): return [floatify_latlng(x) for x in input_value] else: return input_value
Example #4
Source File: _collections.py From jawfish with MIT License | 6 votes |
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 ({} 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 #5
Source File: _collections.py From gist-alfred with MIT License | 6 votes |
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 #6
Source File: api_jwt.py From gist-alfred with MIT License | 6 votes |
def encode(self, payload, key, algorithm='HS256', headers=None, json_encoder=None): # Check that we get a mapping if not isinstance(payload, Mapping): raise TypeError('Expecting a mapping object, as JWT only supports ' 'JSON objects as payloads.') # Payload for time_claim in ['exp', 'iat', 'nbf']: # Convert datetime to a intDate value in known time-format claims if isinstance(payload.get(time_claim), datetime): payload[time_claim] = timegm(payload[time_claim].utctimetuple()) json_payload = json.dumps( payload, separators=(',', ':'), cls=json_encoder ).encode('utf-8') return super(PyJWT, self).encode( json_payload, key, algorithm, headers, json_encoder )
Example #7
Source File: random_model.py From respy with MIT License | 6 votes |
def _update_nested_dictionary(dict_, other): """Update a nested dictionary with another dictionary. The basic ``.update()`` method of dictionaries adds non-existing keys or replaces existing keys which works fine for unnested dictionaries. For nested dictionaries, levels under the current level are not updated but overwritten. This function recursively loops over keys and values and inserts the value if it is not a dictionary. If it is a dictionary, it applies the same process again. """ for key, value in other.items(): if isinstance(value, collections.Mapping): dict_[key] = _update_nested_dictionary(dict_.get(key, {}), value) else: dict_[key] = value return dict_
Example #8
Source File: misc.py From misp42splunk with GNU Lesser General Public License v3.0 | 6 votes |
def execfile(filename, myglobals=None, mylocals=None): """ Read and execute a Python script from a file in the given namespaces. The globals and locals are dictionaries, defaulting to the current globals and locals. If only globals is given, locals defaults to it. """ if myglobals is None: # There seems to be no alternative to frame hacking here. caller_frame = inspect.stack()[1] myglobals = caller_frame[0].f_globals mylocals = caller_frame[0].f_locals elif mylocals is None: # Only if myglobals is given do we set mylocals to it. mylocals = myglobals if not isinstance(myglobals, Mapping): raise TypeError('globals must be a mapping') if not isinstance(mylocals, Mapping): raise TypeError('locals must be a mapping') with open(filename, "rbU") as fin: source = fin.read() code = compile(source, filename, "exec") exec_(code, myglobals, mylocals)
Example #9
Source File: misc.py From misp42splunk with GNU Lesser General Public License v3.0 | 6 votes |
def execfile(filename, myglobals=None, mylocals=None): """ Read and execute a Python script from a file in the given namespaces. The globals and locals are dictionaries, defaulting to the current globals and locals. If only globals is given, locals defaults to it. """ if myglobals is None: # There seems to be no alternative to frame hacking here. caller_frame = inspect.stack()[1] myglobals = caller_frame[0].f_globals mylocals = caller_frame[0].f_locals elif mylocals is None: # Only if myglobals is given do we set mylocals to it. mylocals = myglobals if not isinstance(myglobals, Mapping): raise TypeError('globals must be a mapping') if not isinstance(mylocals, Mapping): raise TypeError('locals must be a mapping') with open(filename, "rbU") as fin: source = fin.read() code = compile(source, filename, "exec") exec_(code, myglobals, mylocals)
Example #10
Source File: types.py From recipes-py with Apache License 2.0 | 6 votes |
def thaw(obj): """Takes a a frozen object, and returns a mutable version of it. Conversions: * collections.Mapping -> dict * tuple -> list * frozenset -> set Close to the opposite of freeze(). Does not convert dict keys. """ if isinstance(obj, (dict, collections.OrderedDict, FrozenDict)): return {k: thaw(v) for k, v in obj.iteritems()} elif isinstance(obj, (list, tuple)): return [thaw(i) for i in obj] elif isinstance(obj, (set, frozenset)): return {thaw(i) for i in obj} else: return obj
Example #11
Source File: tools.py From hierarchical_loc with BSD 3-Clause "New" or "Revised" License | 6 votes |
def dict_update(d, u): """Improved update for nested dictionaries. Arguments: d: The dictionary to be updated. u: The update dictionary. Returns: The updated dictionary. """ d = d.copy() for k, v in u.items(): if isinstance(v, collections.Mapping): d[k] = dict_update(d.get(k, {}), v) else: d[k] = v return d
Example #12
Source File: _collections.py From core with MIT License | 6 votes |
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 #13
Source File: _utils.py From python-zhmcclient with Apache License 2.0 | 6 votes |
def repr_dict(_dict, indent): """Return a debug representation of a dict or OrderedDict.""" # pprint represents OrderedDict objects using the tuple init syntax, # which is not very readable. Therefore, dictionaries are iterated over. if _dict is None: return 'None' if not isinstance(_dict, Mapping): raise TypeError("Object must be a mapping, but is a %s" % type(_dict)) if isinstance(_dict, OrderedDict): kind = 'ordered' ret = '%s {\n' % kind # non standard syntax for the kind indicator for key in six.iterkeys(_dict): value = _dict[key] ret += _indent('%r: %r,\n' % (key, value), 2) else: # dict kind = 'sorted' ret = '%s {\n' % kind # non standard syntax for the kind indicator for key in sorted(six.iterkeys(_dict)): value = _dict[key] ret += _indent('%r: %r,\n' % (key, value), 2) ret += '}' ret = repr_text(ret, indent=indent) return ret.lstrip(' ')
Example #14
Source File: _collections.py From ServerlessCrawler-VancouverRealState with MIT License | 6 votes |
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 #15
Source File: _collections.py From ServerlessCrawler-VancouverRealState with MIT License | 6 votes |
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 #16
Source File: _collections.py From plugin.video.emby with GNU General Public License v3.0 | 6 votes |
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 #17
Source File: iam.py From aegea with Apache License 2.0 | 5 votes |
def configure(args): for group, policies in config.managed_iam_groups.items(): print("Creating group", group) formatted_policies = [(IAMPolicyBuilder(**p) if isinstance(p, collections.Mapping) else p) for p in policies] ensure_iam_group(group, policies=formatted_policies) msg = 'Created group {g}. Use the AWS console or "aws iam add-user-to-group --user-name USER --group-name {g}" to add users to it.' # noqa print(BOLD(msg.format(g=group)))
Example #18
Source File: utils.py From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 | 5 votes |
def namedtuple_with_defaults(typename, field_names, default_values=()): """ create a namedtuple with default values """ T = collections.namedtuple(typename, field_names) T.__new__.__defaults__ = (None, ) * len(T._fields) if isinstance(default_values, collections.Mapping): prototype = T(**default_values) else: prototype = T(*default_values) T.__new__.__defaults__ = tuple(prototype) return T
Example #19
Source File: config.py From esmlab with Apache License 2.0 | 5 votes |
def update(old, new, priority='new'): """ Update a nested dictionary with values from another This is like dict.update except that it smoothly merges nested values This operates in-place and modifies old Parameters ---------- priority: string {'old', 'new'} If new (default) then the new dictionary has preference. Otherwise the old dictionary does. """ for k, v in new.items(): if k not in old and isinstance(v, Mapping): old[k] = {} if isinstance(v, Mapping): if old[k] is None: old[k] = {} update(old[k], v, priority=priority) else: if priority == 'new' or k not in old: old[k] = v return old
Example #20
Source File: config.py From esmlab with Apache License 2.0 | 5 votes |
def expand_environment_variables(config): ''' Expand environment variables in a nested config dictionary This function will recursively search through any nested dictionaries and/or lists. Parameters ---------- config : dict, iterable, or str Input object to search for environment variables Returns ------- config : same type as input Examples -------- >>> expand_environment_variables({'x': [1, 2, '$USER']}) # doctest: +SKIP {'x': [1, 2, 'my-username']} ''' if isinstance(config, Mapping): return {k: expand_environment_variables(v) for k, v in config.items()} elif isinstance(config, str): return os.path.expanduser(os.path.expandvars(config)) elif isinstance(config, (list, tuple, set)): return type(config)([expand_environment_variables(v) for v in config]) else: return config
Example #21
Source File: _collections.py From jawfish with MIT License | 5 votes |
def __eq__(self, other): if not isinstance(other, Mapping) and not hasattr(other, 'keys'): return False if not isinstance(other, type(self)): other = type(self)(other) return dict((k1, self[k1]) for k1 in self) == dict((k2, other[k2]) for k2 in other)
Example #22
Source File: structures.py From jawfish with MIT License | 5 votes |
def __eq__(self, other): if isinstance(other, collections.Mapping): other = CaseInsensitiveDict(other) else: return NotImplemented # Compare insensitively return dict(self.lower_items()) == dict(other.lower_items()) # Copy is required
Example #23
Source File: sessions.py From jawfish with MIT License | 5 votes |
def merge_setting(request_setting, session_setting, dict_class=OrderedDict): """ Determines appropriate setting for a given request, taking into account the explicit setting on that request, and the setting in the session. If a setting is a dictionary, they will be merged together using `dict_class` """ if session_setting is None: return request_setting if request_setting is None: return session_setting # Bypass if not a dictionary (e.g. verify) if not ( isinstance(session_setting, Mapping) and isinstance(request_setting, Mapping) ): return request_setting merged_setting = dict_class(to_key_val_list(session_setting)) merged_setting.update(to_key_val_list(request_setting)) # Remove keys that are set to None. for (k, v) in request_setting.items(): if v is None: del merged_setting[k] merged_setting = dict((k, v) for (k, v) in merged_setting.items() if v is not None) return merged_setting
Example #24
Source File: utils.py From jawfish with MIT License | 5 votes |
def to_key_val_list(value): """Take an object and test to see if it can be represented as a dictionary. If it can be, return a list of tuples, e.g., :: >>> to_key_val_list([('key', 'val')]) [('key', 'val')] >>> to_key_val_list({'key': 'val'}) [('key', 'val')] >>> to_key_val_list('string') ValueError: cannot encode objects that are not 2-tuples. """ if value is None: return None if isinstance(value, (str, bytes, bool, int)): raise ValueError('cannot encode objects that are not 2-tuples') if isinstance(value, collections.Mapping): value = value.items() return list(value) # From mitsuhiko/werkzeug (used with permission).
Example #25
Source File: common.py From dataflow with Apache License 2.0 | 5 votes |
def _get_msg(self, dp): msg = [colored(u"datapoint %i/%i with %i components consists of" % (self.cnt, self.num, len(dp)), "cyan")] is_dict = isinstance(dp, Mapping) for k, entry in enumerate(dp): if is_dict: key, value = entry, dp[entry] else: key, value = k, entry msg.append(self._analyze_input_data(value, key, max_depth=self.max_depth, max_list=self.max_list)) return u'\n'.join(msg)
Example #26
Source File: _collections.py From gist-alfred with MIT License | 5 votes |
def __eq__(self, other): if not isinstance(other, Mapping) and not hasattr(other, 'keys'): return False if not isinstance(other, type(self)): other = type(self)(other) return (dict((k.lower(), v) for k, v in self.itermerged()) == dict((k.lower(), v) for k, v in other.itermerged()))
Example #27
Source File: api_jws.py From gist-alfred with MIT License | 5 votes |
def _load(self, jwt): if isinstance(jwt, text_type): jwt = jwt.encode('utf-8') if not issubclass(type(jwt), binary_type): raise DecodeError("Invalid token type. Token must be a {0}".format( binary_type)) try: signing_input, crypto_segment = jwt.rsplit(b'.', 1) header_segment, payload_segment = signing_input.split(b'.', 1) except ValueError: raise DecodeError('Not enough segments') try: header_data = base64url_decode(header_segment) except (TypeError, binascii.Error): raise DecodeError('Invalid header padding') try: header = json.loads(header_data.decode('utf-8')) except ValueError as e: raise DecodeError('Invalid header string: %s' % e) if not isinstance(header, Mapping): raise DecodeError('Invalid header string: must be a json object') try: payload = base64url_decode(payload_segment) except (TypeError, binascii.Error): raise DecodeError('Invalid payload padding') try: signature = base64url_decode(crypto_segment) except (TypeError, binascii.Error): raise DecodeError('Invalid crypto padding') return (payload, signing_input, header, signature)
Example #28
Source File: api_jwt.py From gist-alfred with MIT License | 5 votes |
def decode(self, jwt, key='', verify=True, algorithms=None, options=None, **kwargs): if verify and not algorithms: warnings.warn( 'It is strongly recommended that you pass in a ' + 'value for the "algorithms" argument when calling decode(). ' + 'This argument will be mandatory in a future version.', DeprecationWarning ) payload, signing_input, header, signature = self._load(jwt) if options is None: options = {'verify_signature': verify} else: options.setdefault('verify_signature', verify) decoded = super(PyJWT, self).decode( jwt, key=key, algorithms=algorithms, options=options, **kwargs ) try: payload = json.loads(decoded.decode('utf-8')) except ValueError as e: raise DecodeError('Invalid payload string: %s' % e) if not isinstance(payload, Mapping): raise DecodeError('Invalid payload string: must be a json object') if verify: merged_options = merge_dict(self.options, options) self._validate_claims(payload, merged_options, **kwargs) return payload
Example #29
Source File: misc.py From verge3d-blender-addon with GNU General Public License v3.0 | 5 votes |
def update(*args, **kwds): '''Like dict.update() but add counts instead of replacing them. Source can be an iterable, a dictionary, or another Counter instance. >>> c = Counter('which') >>> c.update('witch') # add elements from another iterable >>> d = Counter('watch') >>> c.update(d) # add elements from another counter >>> c['h'] # four 'h' in which, witch, and watch 4 ''' # The regular dict.update() operation makes no sense here because the # replace behavior results in the some of original untouched counts # being mixed-in with all of the other counts for a mismash that # doesn't have a straight-forward interpretation in most counting # contexts. Instead, we implement straight-addition. Both the inputs # and outputs are allowed to contain zero and negative counts. if not args: raise TypeError("descriptor 'update' of 'Counter' object " "needs an argument") self = args[0] args = args[1:] if len(args) > 1: raise TypeError('expected at most 1 arguments, got %d' % len(args)) iterable = args[0] if args else None if iterable is not None: if isinstance(iterable, Mapping): if self: self_get = self.get for elem, count in iterable.items(): self[elem] = count + self_get(elem, 0) else: super(Counter, self).update(iterable) # fast path when counter is empty else: _count_elements(self, iterable) if kwds: self.update(kwds)
Example #30
Source File: misc.py From verge3d-blender-addon with GNU General Public License v3.0 | 5 votes |
def subtract(*args, **kwds): '''Like dict.update() but subtracts counts instead of replacing them. Counts can be reduced below zero. Both the inputs and outputs are allowed to contain zero and negative counts. Source can be an iterable, a dictionary, or another Counter instance. >>> c = Counter('which') >>> c.subtract('witch') # subtract elements from another iterable >>> c.subtract(Counter('watch')) # subtract elements from another counter >>> c['h'] # 2 in which, minus 1 in witch, minus 1 in watch 0 >>> c['w'] # 1 in which, minus 1 in witch, minus 1 in watch -1 ''' if not args: raise TypeError("descriptor 'subtract' of 'Counter' object " "needs an argument") self = args[0] args = args[1:] if len(args) > 1: raise TypeError('expected at most 1 arguments, got %d' % len(args)) iterable = args[0] if args else None if iterable is not None: self_get = self.get if isinstance(iterable, Mapping): for elem, count in iterable.items(): self[elem] = self_get(elem, 0) - count else: for elem in iterable: self[elem] = self_get(elem, 0) - 1 if kwds: self.subtract(kwds)