Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_types(node):
'''
Given a node, types should yield a tuple of key, type pairs and the
type indicated should equal the type of the value.
'''
for k, v in api.walk(node):
ts = api.types(node, k)
ta = ()
for tk, tt in ts:
ta += (tk,)
assert type(api.get(node, ta)) is tt
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
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
def test_view(walkable):
'''
Given a walkable location, view that location.
'''
(node, (segments, found)) = walkable
assume(found == found) # Hello, nan! We don't want you here.
view = api.view(node, segments)
assert api.get(view, segments) == api.get(node, segments)
def test_set_walkable(walkable, value):
'''
Given a walkable location, set should be able to update any value.
'''
(node, (segments, found)) = walkable
api.set(node, segments, value)
assert api.get(node, segments) is value
def test_get(node):
'''
Given a node, get should return the exact value given a key for all
key, value pairs in the node.
'''
for k, v in api.walk(node):
assert api.get(node, k) is v
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))
# 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)
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))
# 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.
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
# non-dictionaries from dpath.segments.kvs().
# Otherwise we'd be unable to delete all the tails
# of a list and end up with None values when we
# don't need them.