How to use the dpath.segments 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 mutate(draw, segment):
    # Convert number segments.
    segment = api.int_str(segment)

    # Infer the type constructor for the result.
    kind = type(segment)

    # Produce a valid kind conversion for our wildcards.
    if isinstance(segment, bytes):
        def to_kind(v):
            try:
                return bytes(v, 'utf-8')
            except:
                return kind(v)
    else:
        def to_kind(v):
            return kind(v)

    # Convert to an list of single values.
github akesterson / dpath-python / tests / test_segments.py View on Github external
def test_kvs(node):
    '''
    Given a node, kvs should produce a key that when used to extract
    from the node renders the exact same value given.
    '''
    for k, v in api.kvs(node):
        assert node[k] is v
github akesterson / dpath-python / dpath / util.py View on Github external
def __safe_path__(path, separator):
    '''
    Given a path and separator, return a tuple of segments. If path is
    already a non-leaf thing, return it.

    Note that a string path with the separator at index[0] will have the
    separator stripped off. If you pass a list path, the separator is
    ignored, and is assumed to be part of each key glob. It will not be
    stripped.
    '''
    if not dpath.segments.leaf(path):
        segments = path
    else:
        segments = path.lstrip(separator).split(separator)

        # FIXME: This check was in the old internal library, but I can't
        # see a way it could fail...
        for i, segment in enumerate(segments):
            if (separator and (separator in segment)):
                raise InvalidKeyName("{} at {}[{}] contains the separator '{}'"
                                     "".format(segment, segments, i, separator))

        # Attempt to convert integer segments into actual integers.
        final = []
        for segment in segments:
            try:
                final.append(int(segment))
github akesterson / dpath-python / dpath / util.py View on Github external
def keeper(segments, found):
        '''
        Generalized test for use in both yielded and folded cases.
        Returns True if we want this result. Otherwise returns False.
        '''
        if not dirs and not dpath.segments.leaf(found):
            return False

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

        return (matched and not afilter) or (matched and selected)
github akesterson / dpath-python / dpath / util.py View on Github external
def merger(dst, src, _segments=()):
        for key, found in dpath.segments.kvs(src):
            # Our current path in the source.
            segments = _segments + (key,)

            if len(key) == 0 and not options.ALLOW_EMPTY_STRING_KEYS:
                raise InvalidKeyName("Empty string keys not allowed without "
                                     "dpath.options.ALLOW_EMPTY_STRING_KEYS=True: "
                                     "{}".format(segments))

            # Validate src and dst types match.
            if flags & MERGE_TYPESAFE:
                if dpath.segments.has(dst, segments):
                    target = dpath.segments.get(dst, segments)
                    tt = type(target)
                    ft = type(found)
                    if tt != ft:
                        path = separator.join(segments)