Cyclical depenency detection for templating
This commit is contained in:
parent
14a9e1c504
commit
239b3f1a84
@ -27,12 +27,13 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"execution_count": 3,
|
||||
"id": "207d2510",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import os\n",
|
||||
"import re\n",
|
||||
"import shutil\n",
|
||||
"import markdown\n",
|
||||
"import yaml\n",
|
||||
@ -88,11 +89,96 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 14,
|
||||
"id": "9abb76b2",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def find_cyclical_placeholders(s, _parents = None, _leaves = None, **kwargs):\n",
|
||||
" if _parents is None:\n",
|
||||
" _parents = tuple()\n",
|
||||
" if _leaves is None:\n",
|
||||
" _leaves = {}\n",
|
||||
" placeholders = extract_placeholders(s)\n",
|
||||
"\n",
|
||||
" if placeholders is None or len(placeholders) == 0:\n",
|
||||
" _leaves[_parents] = False\n",
|
||||
"\n",
|
||||
" for p in placeholders:\n",
|
||||
" if p in _parents:\n",
|
||||
" _leaves[_parents + (p,)] = True\n",
|
||||
" else:\n",
|
||||
" find_cyclical_placeholders(\n",
|
||||
" ('{'+p+'}').format(**kwargs),\n",
|
||||
" _parents = _parents+(p,),\n",
|
||||
" _leaves = _leaves,\n",
|
||||
" **kwargs\n",
|
||||
" )\n",
|
||||
"\n",
|
||||
" return _leaves\n",
|
||||
" \n",
|
||||
" \n",
|
||||
"\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"id": "8699f542",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"{('a',): False, ('b',): False, ('c', 'd'): False, ('c', 'e', 'c'): True}"
|
||||
]
|
||||
},
|
||||
"execution_count": 16,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"kwargs = {'a': '1', 'b': '2', 'c': '{d}+{e}', 'd': '3', 'e': '{c}'}\n",
|
||||
"s = '{a} + {b} = {c}'\n",
|
||||
"find_cyclical_placeholders(s, **kwargs)\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"id": "92310172",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
"source": [
|
||||
"def extract_placeholders(s):\n",
|
||||
" # Regex pattern to match placeholders\n",
|
||||
" placeholder_pattern = r'\\{(\\w+)\\}'\n",
|
||||
" \n",
|
||||
" # Find all matches in the string\n",
|
||||
" matches = re.findall(placeholder_pattern, s)\n",
|
||||
" \n",
|
||||
" return matches\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"id": "e6884a45",
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"\n",
|
||||
"FOR FORMAT_HTML_TEMPLATE:\n",
|
||||
"unformatted = ...\n",
|
||||
"formatted = None\n",
|
||||
"filled_placeholders = set()\n",
|
||||
"while unformatted != formatted:\n",
|
||||
" placeholders = extract_paceholders(unformatted)\n",
|
||||
" formatted = unformatted.format(...)\n",
|
||||
" filled_placeholders.add(set(placeholders))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user