f-string capability in a function - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: General (https://python-forum.io/forum-1.html) +--- Forum: News and Discussions (https://python-forum.io/forum-31.html) +--- Thread: f-string capability in a function (/thread-23350.html) |
f-string capability in a function - Skaperen - Dec-24-2019 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. RE: f-string capability in a function - ibreeden - Dec-24-2019 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.
RE: f-string capability in a function - DeaD_EyE - Dec-24-2019 Maybe you want to use some existing tools: https://palletsprojects.com/p/jinja/ BTW: Merry Christmas RE: f-string capability in a function - snippsat - Dec-24-2019 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})
RE: f-string capability in a function - Skaperen - Dec-24-2019 (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. 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/ 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. my goal had nothing to do with web templating. RE: f-string capability in a function - snippsat - Dec-24-2019 (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. RE: f-string capability in a function - Skaperen - Dec-25-2019 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. RE: f-string capability in a function - snippsat - Dec-25-2019 (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) 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())
>>> 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' RE: f-string capability in a function - Skaperen - Dec-26-2019 now i need a function that converts f-string format to j-string format. |