Short version:
I want to crate function which replace all named groups in regular expression with coresponding data from datadict. For example:
Input: expr=r"/(?P<something>\w+)/whatever/(?P<something2>\w+)" data={"something":123, "something2": "thing"}
Output: "/123/whatever/thing"
But i have no idea how to do it.
Some addtional info:
I have code which iterate trough list of tuples containing name and pattern and trying to use re.search. In case that re.search match given string it returns name from current tuple and groupdict() (which is dict with data from re.search).
Here is the code
class UrlResolver():
def __init__(self):
self.urls = {}
def parse(self, app, url):
for pattern in self.urls[app]:
data = re.search(pattern[1], url)
if data:
return {"name": pattern[0], "data": data.groupdict()}
Now i would like to create function:
def compose(self, app, name, data):
for pattern in self.url[app]:
if pattern[0] == name:
return string composed from regex expression and data from data dict.
Above function should replace all named groups with coresponding data from datadict.