I have this function that replaces a named rule in the value of a dictionary, like #digit#, with its regular expression counterpart: [0-9)
So for example, calling this function:
def expand_re(pat_dict:{str:str}):
pat_list = list(pat_dict.items())
for key, rule in pat_dict.items():
expression = re.compile(r'#\w+#')
pat_dict[key] = re.sub(expression, f"(?:{pat_list[0][1]})", rule)
return pat_dict
on this dictionary:
pd = dict(digit = r'[0-9]', integer = r'[+-]?#digit##digit#*')
Would produce:
{'digit': '[0-9]', 'integer': '[+-]?(?:[0-9])(?:[0-9])*'}
And it works just fine for that. However, if a dictionary starts using the previous value's named rule, it does not work. So calling expand_re on the dictionary:
pd = dict(a='correct',b='#a#',c='#b#',d='#c#',e='#d#',f='#e#',g='#f#')
My function produces:
{'a': 'correct',
'b': '(?:correct)',
'c': '(?:correct)',
'd': '(?:correct)',
'e': '(?:correct)',
'f': '(?:correct)',
'g': '(?:correct)'
}
When I want it to produce:
{'a': 'correct',
'b': '(?:correct)',
'c': '(?:(?:correct))',
'd': '(?:(?:(?:correct)))',
'e': '(?:(?:(?:(?:correct))))',
'f': '(?:(?:(?:(?:(?:correct)))))',
'g': '(?:(?:(?:(?:(?:(?:correct))))))'
}
How would I be able to do this? I've tried using the dictionary's .update() method, but to no avail.