'calculation parameters in format_map()

I read a formatting message from database, and I want to fill message parameters from a dictionary.

The problem is where I got a mixed parameter that need calculation.

data = {"p1": 10, "p2": 8}
msg = "user point is {p1+p2}" # read from database
print(msg.format_map(data))

How can I handle this?



Solution 1:[1]

?? Do not rely on the following if you do not know what 1) “dynamic evaluation” exactly means and 2) what are the exact security risks (e.g. *) you are taking by doing so. ??

>>> eval(f'f"{msg}"', None, data)
'user point is 18'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 keepAlive