Python Forum

Full Version: f-string capability in a function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i would like to have a script read a string from a file and call a function to act on it just as if that string had been coded like the contents of an f-string.
That is an interesting puzzle. After a lot of tries I succeeded in producing a working example.
>>> string_from_file = 'f"Printing {var1}"'
>>> string_from_file
'f"Printing {var1}"'
>>> var1 = "content of variable"
>>> exec('print(eval(string_from_file))')
Printing content of variable
>>> 
Although this works I would prefer using string.format(). But I am sure you thought about that already.
Maybe you want to use some existing tools: https://palletsprojects.com/p/jinja/

BTW: Merry Christmas
I agree look into jinja,sometime it easy to forget that is stand alone and very powerful,not only for web templating.
from jinja2 import Template

def multi_str(word, numb):
    return f' '.join([f'{word.upper():~^21}'] * numb)

from_file = "Hello {{ multi_str('Merry Christmas', 3) }}"
template = Template(from_file)
print(template.render(multi_str=multi_str))
# or
#print(template.render({'multi_str': multi_str}) 
Output:
Hello ~~~MERRY CHRISTMAS~~~ ~~~MERRY CHRISTMAS~~~ ~~~MERRY CHRISTMAS~~~
(Dec-24-2019, 12:46 PM)ibreeden Wrote: [ -> ]That is an interesting puzzle. After a lot of tries I succeeded in producing a working example.
>>> string_from_file = 'f"Printing {var1}"'
>>> string_from_file
'f"Printing {var1}"'
>>> var1 = "content of variable"
>>> exec('print(eval(string_from_file))')
Printing content of variable
>>> 
Although this works I would prefer using string.format(). But I am sure you thought about that already.

i don't necessarily want to print it. maybe i want to pass the result, and some there values, to a function or method. maybe the result is meant to be a file name.

(Dec-24-2019, 03:31 PM)DeaD_EyE Wrote: [ -> ]Maybe you want to use some existing tools: https://palletsprojects.com/p/jinja/

BTW: Merry Christmas

i don't see how Jinja, or any template engine, could achieve this.

(Dec-24-2019, 08:31 PM)snippsat Wrote: [ -> ]I agree look into jinja,sometime it easy to forget that is stand alone and very powerful,not only for web templating.
from jinja2 import Template

def multi_str(word, numb):
    return f' '.join([f'{word.upper():~^21}'] * numb)

from_file = "Hello {{ multi_str('Merry Christmas', 3) }}"
template = Template(from_file)
print(template.render(multi_str=multi_str))
# or
#print(template.render({'multi_str': multi_str}) 
Output:
Hello ~~~MERRY CHRISTMAS~~~ ~~~MERRY CHRISTMAS~~~ ~~~MERRY CHRISTMAS~~~

my goal had nothing to do with web templating.
(Dec-24-2019, 08:52 PM)Skaperen Wrote: [ -> ]my goal had nothing to do with web templating.
Do you see any web templating in my code,from_file variable could come from a file.
That's why i did write.
snippsat Wrote:not only for web templating

Quote:i don't necessarily want to print it. maybe i want to pass the result, and some there values, to a function or method. maybe the result is meant to be a file name.
The printing was just a example when did call function from that string,do not need to do that.
If jinja dos not fit your requirement then do not use it,i did think @DeaD_EyE advice was something that you could look into.
could you show a complete and simple example?

ultimately i want a function such that:

send_string(f'{foo} == {bar}')
could be done like this:

send_string(skaplib.fstring('{foo} == {bar}'))
it's a pointless choice if the string is a literal. but if the string is gotten from somewhere else the function could be of value:

send_string(skaplib.fstring(somewhere_else('xyzzy')))
it might get the string from a multi-language message library, as an example.
(Dec-25-2019, 02:33 AM)Skaperen Wrote: [ -> ]could you show a complete and simple example?
Here one with in comparison with f-string.
from jinja2 import Template

name = 'Kent'
age = 34
f_string = f"My name is {name} and I am {age}"
j_string = "My name is {{ name }} and I am {{ age }}"
tm = Template(j_string)
jinja_msg = tm.render(name=name, age=age)
print(f_string)
print(jinja_msg)
Output:
My name is Kent and I am 34 My name is Kent and I am 34
Here using {{ }} - expressions to print to the template output.

{% %} - statements,like if,elif,else,for...ect.
from jinja2 import Template

t = Template("Numbers are: {% for n in range(1,10) %}{{n}} " "{% endfor %}")
print(t.render())
Output:
Numbers are: 1 2 3 4 5 6 7 8
>>> from jinja2 import Template
>>> 
>>> Template("{{ 10 ** 3 }}").render()
'1000'
>>> from jinja2 import Template
>>>
>>> def foo():
...    return "foo() called"
...
>>>
>>> Template("{{ foo() }}").render(foo=foo)
'foo() called'
now i need a function that converts f-string format to j-string format.