Bottom Page

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

BTW: Merry Christmas
snippsat likes this post
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Quote
#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~~~
Quote
#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.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#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.
Quote
#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.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#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'
Quote
#9
now i need a function that converts f-string format to j-string format.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  a function to look for dates in a string Skaperen 6 563 May-09-2020, 11:03 PM
Last Post: Skaperen
  function t split a string Skaperen 2 517 Apr-23-2019, 06:03 PM
Last Post: Skaperen

Forum Jump:


Users browsing this thread: 1 Guest(s)