Get or set value using a dot-notation key in a multilevel dict.
421710
def dict_dot(d, k, val=None, default=None):
    """"""
    if val is None and k == '':
        return d

    def set_default(dict_or_model, key, default_value):
        """Set default field value."""
        if isinstance(dict_or_model, models.Model):
            if not hasattr(dict_or_model, key):
                setattr(dict_or_model, key, default_value)

            return getattr(dict_or_model, key)
        else:
            return dict_or_model.setdefault(key, default_value)

    def get_item(dict_or_model, key):
        """Get field value."""
        if isinstance(dict_or_model, models.Model):
            return getattr(dict_or_model, key)
        else:
            return dict_or_model[key]

    def set_item(dict_or_model, key, value):
        """Set field value."""
        if isinstance(dict_or_model, models.Model):
            setattr(dict_or_model, key, value)
        else:
            dict_or_model[key] = value

    if val is None and callable(default):
        # Get value, default for missing
        return functools.reduce(lambda a, b: set_default(a, b, default()), k.split('.'), d)

    elif val is None:
        # Get value, error on missing
        return functools.reduce(get_item, k.split('.'), d)

    else:
        # Set value
        try:
            k, k_last = k.rsplit('.', 1)
            set_item(dict_dot(d, k, default=dict), k_last, val)
        except ValueError:
            set_item(d, k, val)
        return val
56097
def _get_value(obj, key, default=missing):
    
    if "." in key:
        return _get_value_for_keys(obj, key.split("."), default)
    else:
        return _get_value_for_key(obj, key, default)
391488
def value_for_keypath(dict, keypath):
    

    if len(keypath) == 0:
        return dict

    keys = keypath.split('.')
    value = dict
    for key in keys:
        if key in value:
            value = value[key]
        else:
            return None

    return value
292058
def get_es_value(obj, def_obj):
    
    def get_dict_val(item):
        """
        Returns the string representation of the dict item
        """
        if isinstance(item, dict):
            return str(item.get('value'))
        return str(item)

    value_flds = []
    if def_obj.es_defs.get('kds_esValue'):
        value_flds = def_obj.es_defs['kds_esValue'].copy()
    else:
        # pdb.set_trace()
        value_flds = set(obj).difference(__ALL_IGN__)
        value_flds = list(value_flds)
    value_flds += __COMBINED__
    try:
        obj['value'] = [obj.get(label) for label in value_flds
                        if obj.get(label)][0]
    except IndexError:
        obj['value'] = ", ".join(["%s: %s" % (value.get('label'),
                                              value.get('value'))
                                  for prop, value in obj.items()
                                  if isinstance(value, dict) and \
                                  value.get('label')])

    if isinstance(obj['value'], list):
        obj['value'] = ", ".join([get_dict_val(item) for item in obj['value']])
    else:
        obj['value'] = get_dict_val(obj['value'])
    if str(obj['value']).strip().endswith("/"):
        obj['value'] = str(obj['value']).strip()[:-1].strip()
    if not obj['value']:
        obj['value'] = obj.get('uri', '')
    return obj
1012529
def _recursive_getitem(d, key):
    
    if key in d:
        return d
    else:
        for v in d.values():
            return _recursive_getitem(v, key)
        else:
            raise KeyError('Key not found: {}'.format(key))
412779
def set_dotted(self, key, value):
        
        parts = key.split('.')
        cobj = self
        if len(parts) > 1:
            key = parts.pop()
            for name in parts:
                cobj = getattr(cobj, name)
        setattr(cobj, key, value)
188677
def reconstruct_dict(dot_paths, values):
    
    '''  '''
    
    output_dict = {}
    
    for i in range(len(dot_paths)):
        if i + 1 <= len(values):
            path_segments = segment_path(dot_paths[i])
            current_nest = output_dict
            for j in range(len(path_segments)):
                key_name = path_segments[j]
                try:
                    key_name = int(key_name)
                except:
                    pass
                if j + 1 == len(path_segments):
                    if isinstance(key_name, int):
                        current_nest.append(values[i])
                    else:
                        current_nest[key_name] = values[i]
                else:
                    next_key = path_segments[j+1]
                    try:
                        next_key = int(next_key)
                    except:
                        pass
                    if isinstance(next_key, int):
                        if not key_name in current_nest.keys():
                            current_nest[key_name] = []
                        current_nest = current_nest[key_name]
                    else:
                        if isinstance(key_name, int):
                            current_nest.append({})
                            current_nest = current_nest[len(current_nest) - 1]
                        else:
                            if not key_name in current_nest.keys():
                                current_nest[key_name] = {}
                            current_nest = current_nest[key_name]
    
    return output_dict
1132516
def read_keys(self, keys, dict_cls=AttrDict, path="/"):
        
        od = dict_cls()
        for k in keys:
            try:
                # Try to read a variable.
                od[k] = self.read_value(k, path=path)
            except self.Error:
                try:
                    # Try to read a dimension.
                    od[k] = self.read_dimvalue(k, path=path)
                except self.Error:
                    od[k] = None

        return od
679169
def get_value(self, key, args, kwargs):
        """"""
        if (self.tolerant and
                not isinstance(key, basestring) and
                key not in kwargs):
            key = str(key)

        return kwargs[key]
368785
def _get_value(data_structure, key):
    
    if len(key) == 0:
        raise KeyError()
    value = data_structure[key[0]]
    if len(key) > 1:
        return _get_value(value, key[1:])
    return value
674462
def get_name2value_dict(self):
        """"""
        x = {}
        for val, num in self._values:
            x[val] = num
        return x
466158
def key_value_contents(use_dict=None, as_class=dict, key_values=()):
    """"""
    if _debug: key_value_contents._debug("key_value_contents use_dict=%r as_class=%r key_values=%r", use_dict, as_class, key_values)

    # make/extend the dictionary of content
    if use_dict is None:
        use_dict = as_class()

    # loop through the values and save them
    for k, v in key_values:
        if v is not None:
            if hasattr(v, 'dict_contents'):
                v = v.dict_contents(as_class=as_class)
            use_dict.__setitem__(k, v)

    # return what we built/updated
    return use_dict
1206215
def get(gandi, g, key):
    """"""
    val = gandi.get(key=key, global_=g)
    if not val:
        gandi.echo("No value found.")
        sys.exit(1)
    gandi.echo(val)
499800
def _mappingGetValueSet(mapping, keys):
    
    setUnion = set()
    for k in keys:
        setUnion = setUnion.union(mapping[k])
    return setUnion
735756
def value(self):
        """"""
        val = {}
        for k in self.__allowed_keys:
            value = getattr(self, "_" + k)
            if value is not None:
                val[k] = value
        return val
532960
def get_key_by_value(dictionary, search_value):
    
    for key, value in dictionary.iteritems():
        if value == search_value:
            return ugettext(key)
587482
def get_dotted(self, key):
        
        parts = key.split('.')
        cobj = self
        for attr in parts:
            cobj = getattr(cobj, attr)
        return cobj