Skip to content

Commit f463f86

Browse files
authored
Merge pull request #95 from AWhetter/fix_52
Can supply a default return value to get()
2 parents c3dc037 + 0f744b8 commit f463f86

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

dpath/util.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
from dpath.exceptions import InvalidKeyName
55
import dpath.segments
66

7+
_DEFAULT_SENTINAL = object()
78
MERGE_REPLACE = (1 << 1)
89
MERGE_ADDITIVE = (1 << 2)
910
MERGE_TYPESAFE = (1 << 3)
1011

11-
1212
def __safe_path__(path, separator):
1313
'''
1414
Given a path and separator, return a tuple of segments. If path is
@@ -145,13 +145,15 @@ def f(obj, pair, counter):
145145
return changed
146146

147147

148-
def get(obj, glob, separator='/'):
148+
def get(obj, glob, separator='/', default=_DEFAULT_SENTINAL):
149149
'''
150150
Given an object which contains only one possible match for the given glob,
151151
return the value for the leaf matching the given glob.
152+
If the glob is not found and a default is provided,
153+
the default is returned.
152154
153155
If more than one leaf matches the glob, ValueError is raised. If the glob is
154-
not found, KeyError is raised.
156+
not found and a default is not provided, KeyError is raised.
155157
'''
156158
if glob == '/':
157159
return obj
@@ -169,6 +171,9 @@ def f(obj, pair, results):
169171
results = dpath.segments.fold(obj, f, [])
170172

171173
if len(results) == 0:
174+
if default is not _DEFAULT_SENTINAL:
175+
return default
176+
172177
raise KeyError(glob)
173178
elif len(results) > 1:
174179
raise ValueError("dpath.util.get() globs must match only one leaf : %s" % glob)

tests/test_util_get_values.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ def test_get_explicit_single():
2828

2929
assert(dpath.util.get(ehash, '/a/b/c/f') == 2)
3030
assert(dpath.util.get(ehash, ['a', 'b', 'c', 'f']) == 2)
31+
assert(dpath.util.get(ehash, ['a', 'b', 'c', 'f'], default=5) == 2)
32+
assert(dpath.util.get(ehash, ['does', 'not', 'exist'], default=None) is None)
33+
assert(dpath.util.get(ehash, ['doesnt', 'exist'], default=5) == 5)
3134

3235

3336
def test_get_glob_single():
@@ -45,6 +48,8 @@ def test_get_glob_single():
4548

4649
assert(dpath.util.get(ehash, '/a/b/*/f') == 2)
4750
assert(dpath.util.get(ehash, ['a', 'b', '*', 'f']) == 2)
51+
assert(dpath.util.get(ehash, ['a', 'b', '*', 'f'], default=5) == 2)
52+
assert(dpath.util.get(ehash, ['doesnt', '*', 'exist'], default=6) == 6)
4853

4954

5055
def test_get_glob_multiple():
@@ -63,6 +68,7 @@ def test_get_glob_multiple():
6368

6469
assert_raises(ValueError, dpath.util.get, ehash, '/a/b/*/d')
6570
assert_raises(ValueError, dpath.util.get, ehash, ['a', 'b', '*', 'd'])
71+
assert_raises(ValueError, dpath.util.get, ehash, ['a', 'b', '*', 'd'], default=3)
6672

6773

6874
def test_get_absent():

0 commit comments

Comments
 (0)