Dec-24-2019, 06:10 AM
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.
>>> 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.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 usingstring.format()
. But I am sure you thought about that already.
(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
(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~~~
(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. 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.
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'