How to use the dpath.segments.leaf function in dpath

To help you get started, we’ve selected a few dpath examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github akesterson / dpath-python / tests / test_segments.py View on Github external
def test_has(node):
    '''
    Given a node, has should return True for all paths, False otherwise.
    '''
    for k, v in api.walk(node):
        assert api.has(node, k) is True

        # If we are at a leaf, then we can create a value that isn't
        # present easily.
        if api.leaf(v):
            assert api.has(node, k + (0,)) is False
github akesterson / dpath-python / tests / test_segments.py View on Github external
def test_set_create_missing(walkable, kstr, kint, value, extension):
    '''
    Given a walkable non-leaf, set should be able to create missing
    nodes and set a new value.
    '''
    (node, (segments, found)) = walkable
    assume(api.leaf(found))

    parent_segments = segments[:-1]
    parent = api.get(node, parent_segments)

    if isinstance(parent, list):
        assume(len(parent) < kint)
        destination = parent_segments + (kint,) + tuple(extension)
    elif isinstance(parent, dict):
        assume(kstr not in parent)
        destination = parent_segments + (kstr,) + tuple(extension)
    else:
        raise Exception('mad mad world')

    api.set(node, destination, value)
    assert api.get(node, destination) is value
github akesterson / dpath-python / dpath / util.py View on Github external
# Path not present in destination, create it.
            if not dpath.segments.has(dst, segments):
                dpath.segments.set(dst, segments, found)
                continue

            # Retrieve the value in the destination.
            target = dpath.segments.get(dst, segments)

            # If the types don't match, replace it.
            if type(found) != type(target):
                dpath.segments.set(dst, segments, found)
                continue

            # If target is a leaf, the replace it.
            if dpath.segments.leaf(target):
                dpath.segments.set(dst, segments, found)
                continue

            # At this point we know:
            #
            # * The target exists.
            # * The types match.
            # * The target isn't a leaf.
            #
            # Pretend we have a sequence and account for the flags.
            try:
                if flags & MERGE_ADDITIVE:
                    target += found
                    continue

                if flags & MERGE_REPLACE:
github akesterson / dpath-python / dpath / util.py View on Github external
def f(obj, pair, counter):
        (segments, value) = pair

        # Skip segments if they no longer exist in obj.
        if not dpath.segments.has(obj, segments):
            return

        matched = dpath.segments.match(segments, globlist)
        selected = afilter and dpath.segments.leaf(value) and afilter(value)

        if (matched and not afilter) or selected:
            key = segments[-1]
            parent = dpath.segments.get(obj, segments[:-1])

            try:
                # Attempt to treat parent like a sequence.
                parent[0]

                if len(parent) - 1 == key:
                    # Removing the last element of a sequence. It can be
                    # truly removed without affecting the ordering of
                    # remaining items.
                    #
                    # Note: In order to achieve proper behavior we are
                    # relying on the reverse iteration of