I have a web form that accepts user input for string modifications to be applied to information being passed via a given column in a csv. Because I am attempting to use existing architecture I am applying these modifications to lambda functions. For most of the mods it is fairly straight forward, but for my replace modification I would like to do this dynamically, for example a string I might receive from my form might be:
('you', 'me' / 'this', 'that' / 'do', "don't")
the lambda equivalent I would like to create for passing with this data would be:
func = lambda v: str(v).replace('you', 'me').replace('this', 'that').replace('do', "don't")
I can easily do this by limiting the amount of replaces that can be done and then counting the delimiters ('/' in this case) and creating them individually using if statements and such.
I hate this idea for 2 reasons:
- I am effectively limiting my users modification capabilities by putting limits on how many modifications they are allowed to do on a given string
- it does not feel pythonic.
I would prefer an approach like this:
func = lambda v: str(v)
for mod in modstring.split(' / '):
func = func + .replace(mod.split(', ')[0], mod.split(', ')[1])
but I highly doubt such functionality exists... I figure this question is a long-shot but it was worth a try.