From e2beaa239fe604b2f7574dc1f84c373178a0bfb3 Mon Sep 17 00:00:00 2001 From: Chuck McCallum Date: Tue, 19 Nov 2024 17:42:35 -0500 Subject: [PATCH] check for expression and value slots --- dp_wizard/utils/code_generators/_template.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/dp_wizard/utils/code_generators/_template.py b/dp_wizard/utils/code_generators/_template.py index a64ba0e..0c42b8a 100644 --- a/dp_wizard/utils/code_generators/_template.py +++ b/dp_wizard/utils/code_generators/_template.py @@ -13,6 +13,8 @@ def __init__(self, path, template=None): raise Exception('"path" and "template" are mutually exclusive') self._path = "template-instead-of-path" self._template = template + # We want a list of the initial slots, because substitutions + # can produce sequences of upper case letters that could be mistaken for slots. self._initial_slots = self._find_slots() def _find_slots(self): @@ -26,13 +28,17 @@ def _find_slots(self): def fill_expressions(self, **kwargs): for k, v in kwargs.items(): k_re = re.escape(k) - self._template = re.sub(rf"\b{k_re}\b", str(v), self._template) + self._template, count = re.subn(rf"\b{k_re}\b", str(v), self._template) + if count == 0: + raise Exception(f"No slot for '{k}' in {self._path}") return self def fill_values(self, **kwargs): for k, v in kwargs.items(): k_re = re.escape(k) - self._template = re.sub(rf"\b{k_re}\b", repr(v), self._template) + self._template, count = re.subn(rf"\b{k_re}\b", repr(v), self._template) + if count == 0: + raise Exception(f"No slot for '{k}' in {self._path}") return self def fill_blocks(self, **kwargs):