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_format_failure(pattern, data):
'''Format incomplete data against pattern.'''
template = Template('test', pattern)
with pytest.raises(FormatError):
template.format(data)
def _format(match):
'''Return value from data for *match*.'''
placeholder = match.group(1)
parts = placeholder.split('.')
try:
value = data
for part in parts:
value = value[part]
except (TypeError, KeyError):
raise lucidity.error.FormatError(
'Could not format data {0!r} due to missing key {1!r}.'
.format(data, placeholder)
)
else:
return value