How to use the dpath.options.ALLOW_EMPTY_STRING_KEYS 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_path_paths.py View on Github external
def test_path_paths_empty_key_allowed():
    tdict = {
        "Empty": {
            "": {
                "Key": ""
            }
        }
    }
    parts=[]
    dpath.options.ALLOW_EMPTY_STRING_KEYS=True
    for x in dpath.path.paths(tdict, dirs=False, leaves=True):
        path = x
    for x in path[:-1]:
        parts.append(x[0])
    dpath.options.ALLOW_EMPTY_STRING_KEYS=False
    assert("/".join(parts) == "Empty//Key")
github akesterson / dpath-python / tests / test_path_paths.py View on Github external
def test_path_paths_empty_key_allowed():
    tdict = {
        "Empty": {
            "": {
                "Key": ""
            }
        }
    }
    parts=[]
    dpath.options.ALLOW_EMPTY_STRING_KEYS=True
    for x in dpath.path.paths(tdict, dirs=False, leaves=True):
        path = x
    for x in path[:-1]:
        parts.append(x[0])
    dpath.options.ALLOW_EMPTY_STRING_KEYS=False
    assert("/".join(parts) == "Empty//Key")
github akesterson / dpath-python / tests / test_segments.py View on Github external
def setup():
    # Allow empty strings in segments.
    options.ALLOW_EMPTY_STRING_KEYS = True
github akesterson / dpath-python / tests / test_segments.py View on Github external
def teardown():
    # Revert back to default.
    options.ALLOW_EMPTY_STRING_KEYS = False
github akesterson / dpath-python / dpath / path.py View on Github external
path -- A list of keys representing the path.
    skip -- Skip special keys beginning with '+'.

    """
    if isinstance(obj, MutableMapping):
        # Python 3 support
        if PY3:
            iteritems = obj.items()
            string_class = str
        else: # Default to PY2
            iteritems = obj.iteritems()
            string_class = basestring

        for (k, v) in iteritems:
            if issubclass(k.__class__, (string_class)):
                if (not k) and (not dpath.options.ALLOW_EMPTY_STRING_KEYS):
                    raise dpath.exceptions.InvalidKeyName("Empty string keys not allowed without "
                                                          "dpath.options.ALLOW_EMPTY_STRING_KEYS=True")
                elif (skip and k and k[0] == '+'):
                    continue
            newpath = path + [[k, v.__class__]]
            validate(newpath)
            if dirs:
                yield newpath
            for child in paths(v, dirs, leaves, newpath, skip):
                yield child
    elif isinstance(obj, MutableSequence):
        for (i, v) in enumerate(obj):
            newpath = path + [[i, v.__class__]]
            if dirs:
                yield newpath
            for child in paths(obj[i], dirs, leaves, newpath, skip):
github akesterson / dpath-python / dpath / segments.py View on Github external
'''
    Yield all valid (segments, value) pairs (from a breadth-first
    search, right-to-left on sequences).

    walk(obj) -> (generator -> (segments, value))
    '''
    if not leaf(obj):
        for k, v in kvs(obj):
            length = None

            try:
                length = len(k)
            except:
                pass

            if length is not None and length == 0 and not options.ALLOW_EMPTY_STRING_KEYS:
                raise InvalidKeyName("Empty string keys not allowed without "
                                     "dpath.options.ALLOW_EMPTY_STRING_KEYS=True: "
                                     "{}".format(location + (k,)))
            yield ((location + (k,)), v)
        for k, v in kvs(obj):
            for found in walk(v, location + (k,)):
                yield found
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)
                        raise TypeError("Cannot merge objects of type"
                                        "{0} and {1} at {2}"
                                        "".format(tt, ft, path))