Skip to content

Returning default value #52

@anandka

Description

@anandka

Hey, can you please update the code to send in a default value if an object is not found using the get function.

The function "get" under dpath/util.py can be changed as mentioned below to achieve it

def get(obj, glob, separator="/", default = None):
"""
Given an object which contains only one possible match for the given glob,
return the value for the leaf matching the given glob.
If more than one leaf matches the glob, ValueError is raised. If the glob is
not found, KeyError is raised.
"""
ret = None
for item in search(obj, glob, yielded=True, separator=separator):
if ret is not None:
raise ValueError("dpath.util.get() globs must match only one leaf : %s" % glob)
ret = item[1]
if ret is None:
if (default != None):
ret = default
else:
raise KeyError(glob)
return ret

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions