def jpath_set(structure, jpath, value, overwrite = True, unique = False):
    """
    Set given JPath to given value within given structure.

    For performance reasons this method is intentionally not written as
    recursive.

    :param str structure: data structure to be searched
    :param str jpath: JPath to be evaluated
    :param any value: value of any type to be set at given path
    :param bool overwrite: enable/disable overwriting of already existing value
    :param bool unique: ensure uniqueness of value, works only for lists
    :return: numerical return code, one of the (:py:data:`RC_VALUE_SET`,
             :py:data:`RC_VALUE_EXISTS`, :py:data:`RC_VALUE_DUPLICATE`)
    :rtype: int
    """
    chunks = jpath_parse_c(jpath)
    size = len(chunks) - 1
    current = structure

    # Process chunks in order, enumeration is used for detection of the last JPath chunk.
    for i, chnk in enumerate(chunks):
        key = chnk['n']

        if not isinstance(current, dict) and not isinstance(current, collections.Mapping):
            raise JPathException("Expected dict-like structure to attach node '{}'".format(chnk['p']))

        # Process indexed nodes.
        if 'i' in chnk:
            idx = chnk['i']

            # Automatically create nodes for non-existent keys.
            if not key in current:
                current[key] = []
            if not isinstance(current[key], list) and not isinstance(current[key], collections.MutableSequence):
                raise JPathException("Expected list-like object under structure key '{}'".format(key))

            # Detection of the last JPath chunk - node somewhere in the middle.
            if i != size:
                # Attempt to access node at given index.
                try:
                    current = current[key][idx]
                # IndexError: list index out of range
                # Node at given index does not exist, append new one. Using insert()
                # does not work, item is appended to the end of the list anyway.
                # TypeError: list indices must be integers or slices, not str
                # In the case list index was '*', we are appending to the end of
                # list.
                except (IndexError, TypeError):
                    current[key].append({})
                    current = current[key][-1]

            # Detection of the last JPath chunk - node at the end.
            else:
                # Attempt to insert value at given index.
                try:
                    if overwrite or not current[key][idx]:
                        current[key][idx] = value
                    else:
                        return RC_VALUE_EXISTS
                # IndexError: list index out of range
                # Node at given index does not exist, append new one. Using insert()
                # does not work, item is appended to the end of the list anyway.
                # TypeError: list indices must be integers or slices, not str
                # In the case list index was '*', we are appending to the end of
                # list.
                except (IndexError, TypeError):
                    # At this point only deal with unique, overwrite does not make
                    # sense, because we would not be here otherwise.
                    if not unique or not value in current[key]:
                        current[key].append(value)
                    else:
                        return RC_VALUE_DUPLICATE

        # Process unindexed nodes.
        else:
            # Detection of the last JPath chunk - node somewhere in the middle.
            if i != size:
                # Automatically create nodes for non-existent keys.
                if not key in current:
                    current[key] = {}
                if not isinstance(current[key], dict) and not isinstance(current[key], collections.Mapping):
                    raise JPathException("Expected dict-like object under structure key '{}'".format(key))

                current = current[key]

            # Detection of the last JPath chunk - node at the end.
            else:
                if overwrite or not key in current:
                    current[key] = value
                else:
                    return RC_VALUE_EXISTS
    return RC_VALUE_SET
def jpath_set ( structure , jpath , value , overwrite = True , unique = False ) : chunks = jpath_parse_c ( jpath ) size = len ( chunks ) - 1 current = structure for i , chnk in enumerate ( chunks ) : key = chnk [ 'n' ] if not isinstance ( current , dict ) and not isinstance ( current , collections . Mapping ) : raise JPathException ( "Expected dict-like structure to attach node '{}'" . format ( chnk [ 'p' ] ) ) if 'i' in chnk : idx = chnk [ 'i' ] if not key in current : current [ key ] = [ ] if not isinstance ( current [ key ] , list ) and not isinstance ( current [ key ] , collections . MutableSequence ) : raise JPathException ( "Expected list-like object under structure key '{}'" . format ( key ) ) if i != size : try : current = current [ key ] [ idx ] except ( IndexError , TypeError ) : current [ key ] . append ( { } ) current = current [ key ] [ - 1 ] else : try : if overwrite or not current [ key ] [ idx ] : current [ key ] [ idx ] = value else : return RC_VALUE_EXISTS except ( IndexError , TypeError ) : if not unique or not value in current [ key ] : current [ key ] . append ( value ) else : return RC_VALUE_DUPLICATE else : if i != size : if not key in current : current [ key ] = { } if not isinstance ( current [ key ] , dict ) and not isinstance ( current [ key ] , collections . Mapping ) : raise JPathException ( "Expected dict-like object under structure key '{}'" . format ( key ) ) current = current [ key ] else : if overwrite or not key in current : current [ key ] = value else : return RC_VALUE_EXISTS return RC_VALUE_SET
Set given JPath to given value within given structure.

    For performance reasons this method is intentionally not written as
    recursive.

    :param str structure: data structure to be searched
    :param str jpath: JPath to be evaluated
    :param any value: value of any type to be set at given path
    :param bool overwrite: enable/disable overwriting of already existing value
    :param bool unique: ensure uniqueness of value, works only for lists
    :return: numerical return code, one of the (:py:data:`RC_VALUE_SET`,
             :py:data:`RC_VALUE_EXISTS`, :py:data:`RC_VALUE_DUPLICATE`)
    :rtype: int