0

Running out of ideas where to look.

I am trying to set up a string that can be edited outside my code and stored by the user and have that string feed into an f-string inside my code.

Example: Take the following value that is stored in database CharField: Hello {user}! How are you? I want to grab that and feed it into Python to treat as the content of an f-string such that:

>>> user = 'Bob'
>>> stringFromOutside = 'Hello {user}! How are you?'
>>> print(f'{stringFromOutside}')
Hello {user}! How are you?

prints "Hello Bob! How are you?"

I oversimplified for the example, because that's the core of where I'm lost.

  • I have tried nesting the {stringFromOutside} in a second f-string, but that doesn't work.
  • I have also tried double {{user}} inside the template file, but that did not work.

Use Case in case I'm just being obtuse and doing the whole thing wrong
I have an application that I want to expose to the administrator the ability to write templates and save them in the database. The templates need to be able to pull from several different objects and all the properties of those objects, so it would be very cumbersome to try to add the substitutions manually for each possible variable that the template can include.

3 Answers 3

2

I don't think you can use the f-string here, but, I would rather choose str.format(...)

In [3]: user = 'Bob'

In [4]: str_template = 'Hello {user}! How are you?'

In [5]: variables = {"user":user}

In [6]: str_generated = str_template.format(**variables)

In [7]: str_generated
Out[7]: 'Hello Bob! How are you?'

Sign up to request clarification or add additional context in comments.

2 Comments

I like this idea since I've been told to avoid eval (which is the other approach), but I need this to be able to handle just about any object and it's attributes throughout my project. Is there a way to make that variables dict just look at my object models?
In case someone else is looking, as long as the things that need to be accessed are available in locals() (or perhaps even globals() - but someone might be able to say that would be too dangerous), then the following works for line 5 in this answer: variables=locals(). In the end, I could just do str_generated = str_template.format(**locals())
1

You can use nested fstring and eval it:

user = 'Bob'
stringFromOutside = 'Hello {user}! How are you?'
print(eval(f"f'{stringFromOutside}'"))

Output:

Hello Bob! How are you?

1 Comment

I've been led to believe that eval is dangerous and should pretty much never be used. Since this field/template can only be written (theoretically) from inside the admin UI of my product, maybe I can trust it, but I want to make sure from other more experienced people that I'm not missing a huge security risk.
0

You can try this:

user = 'Bob'
stringFromOutside = "f'Hello {user}! How are you?'"
print(eval(stringFromOutside))

1 Comment

I've been led to believe that eval is dangerous and should pretty much never be used. Since this field/template can only be written (theoretically) from inside the admin UI of my product, maybe I can trust it, but I want to make sure from other more experienced people that I'm not missing a huge security risk.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.