How to use the dpath.segments.fold 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_fold(thing):
    '''
    Given a thing, count paths with fold.
    '''
    def f(o, p, a):
        a[0] += 1
    [count] = api.fold(thing, f, [0])
    assert count == len(tuple(api.walk(thing)))
github akesterson / dpath-python / dpath / util.py View on Github external
return (matched and not afilter) or (matched and selected)

    if yielded:
        def yielder():
            for segments, found in dpath.segments.walk(obj):
                if keeper(segments, found):
                    yield (separator.join(map(dpath.segments.int_str, segments)), found)
        return yielder()
    else:
        def f(obj, pair, result):
            (segments, found) = pair

            if keeper(segments, found):
                dpath.segments.set(result, segments, found, hints=dpath.segments.types(obj, segments))

        return dpath.segments.fold(obj, f, {})
github akesterson / dpath-python / dpath / util.py View on Github external
not found, KeyError is raised.
    '''
    if glob == '/':
        return obj

    globlist = __safe_path__(glob, separator)

    def f(obj, pair, results):
        (segments, found) = pair

        if dpath.segments.match(segments, globlist):
            results.append(found)
        if len(results) > 1:
            return False

    results = dpath.segments.fold(obj, f, [])

    if len(results) == 0:
        raise KeyError(glob)
    elif len(results) > 1:
        raise ValueError("dpath.util.get() globs must match only one leaf : %s" % glob)

    return results[0]