Python Forum
f-string capability in a function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
f-string capability in a function
#1
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
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.
Reply
#3
Maybe you want to use some existing tools: https://palletsprojects.com/p/jinja/

BTW: Merry Christmas
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
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~~~
Reply
#5
(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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
(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.
Reply
#7
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.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
(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'
Reply
#9
now i need a function that converts f-string format to j-string format.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  passing a string to a function to be a f-string Skaperen 3 3,991 Nov-06-2020, 06:14 AM
Last Post: Gribouillis
  a function to look for dates in a string Skaperen 6 3,962 May-09-2020, 11:03 PM
Last Post: Skaperen
  function t split a string Skaperen 2 1,821 Apr-23-2019, 06:03 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020