allow missing field access keys in format strings (#136)
This commit is contained in:
parent
0c9762f00e
commit
c5d4f558c9
@ -382,11 +382,11 @@ class Formatter():
|
|||||||
|
|
||||||
def _apply(self, key, funcs, fmt):
|
def _apply(self, key, funcs, fmt):
|
||||||
def wrap(obj):
|
def wrap(obj):
|
||||||
if key in obj:
|
try:
|
||||||
obj = obj[key]
|
obj = obj[key]
|
||||||
for func in funcs:
|
for func in funcs:
|
||||||
obj = func(obj)
|
obj = func(obj)
|
||||||
else:
|
except Exception:
|
||||||
obj = self.default
|
obj = self.default
|
||||||
return fmt(obj)
|
return fmt(obj)
|
||||||
return wrap
|
return wrap
|
||||||
|
@ -188,11 +188,17 @@ class TestFormatter(unittest.TestCase):
|
|||||||
|
|
||||||
def test_missing(self):
|
def test_missing(self):
|
||||||
replacement = "None"
|
replacement = "None"
|
||||||
|
|
||||||
self._run_test("{missing}", replacement)
|
self._run_test("{missing}", replacement)
|
||||||
self._run_test("{missing.attr}", replacement)
|
self._run_test("{missing.attr}", replacement)
|
||||||
self._run_test("{missing[key]}", replacement)
|
self._run_test("{missing[key]}", replacement)
|
||||||
self._run_test("{missing:?a//}", "")
|
self._run_test("{missing:?a//}", "")
|
||||||
|
|
||||||
|
self._run_test("{name[missing]}", replacement)
|
||||||
|
self._run_test("{name[missing].attr}", replacement)
|
||||||
|
self._run_test("{name[missing][key]}", replacement)
|
||||||
|
self._run_test("{name[missing]:?a//}", "")
|
||||||
|
|
||||||
def test_missing_custom_default(self):
|
def test_missing_custom_default(self):
|
||||||
replacement = default = "foobar"
|
replacement = default = "foobar"
|
||||||
self._run_test("{missing}" , replacement, default)
|
self._run_test("{missing}" , replacement, default)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user