Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
f-string-ify
#1
i ran into yet another case where it would be nice to be able to express the literal for the f-string in just on place and be able to "run" that string in several places. this is where a simple function to do the f-string evaluation in multiple places from one given string would be useful. since this can't be done i end up with the same f-string in many places.
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
Care to share an example of the code?
I am trying to help you, really, even if it doesn't always seem that way
Reply
#3
i can't share the real code, but i can make an example. don't make other assumptions about this example. don't try to "solve" it some other way (the "solution" is probably not applicable to other cases)
if case1:
    ...
    foo,bar = ...
    ...
    print(f'foo = {foo!r}  and bar = {bar!r}')
    ...
if case2:
    ...
    foo,bar = ...
    ...
    print(f'foo = {foo!r}  and bar = {bar!r}')
    ...
if case3:
    ...
    foo,bar = ...
    ...
    print(f'foo = {foo!r}  and bar = {bar!r}')
    ...
what i want is something more like:
...
pattern = 'foo = {foo!r}  and bar = {bar!r}'
...
if case1:
    ...
    foo,bar = ...
    ...
    print(fy(pattern))
    ...
if case2:
    ...
    foo,bar = ...
    ...
    print(fy(pattern))
    ...
if case3:
    ...
    foo,bar = ...
    ...
    print(fy(pattern))
    ...
where fy() in this example does the f-string-ify work. there will likely be a suggestion to have a function with the f-string coded once in the function and to call that function in each case, passing the variables to it. but that assumes a trivial set of variables. maybe there are too many. maybe they are unknown. maybe i'd like to read the pattern from a config file.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
Not tried it, but I wonder if you could use string Template, from string import Template to handle the patterns.

Just tried a crude example, apologies if missing the plot here:

from string import Template

def fy(pattern):
    return pattern.substitute(foo=f'{foo!r}', bar=f'{bar!r}')
    
pattern = Template('foo = {$foo}  and bar = {$bar}')

for example in 1, 2, 3:
    if example == 1:
        foo,bar = 'one', '1'
        print(fy(pattern))
    if example == 2:
        foo,bar = 'two', '2'
        print(fy(pattern))
    if example == 3:
        foo,bar = 'three', '3'
        print(fy(pattern))
which gives me:
Output:
foo = {'one'} and bar = {'1'} foo = {'two'} and bar = {'2'} foo = {'three'} and bar = {'3'}
I am trying to help you, really, even if it doesn't always seem that way
Reply
#5
it looks like your code is assuming which variables are in the pattern.
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
Look again.
I am trying to help you, really, even if it doesn't always seem that way
Reply
#7
it codes foo and bar directly in function fy(). so what if there is another variable that the pattern uses? the pattern is a string that can come from anywhere such as a line in a file.
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
Yes, but just to illustrate. I am sure you recognise the foo and bar references in the template definition are not related to the foo and bar used later. The substitution could have been in-situ and localised in-line or parameterised in the function.

The Template substitution does not care what parameters are assigned to the foo and bar placeholders in the pattern. The substitution can reference any objects convenient to you at the time of exploitation. You do have to do substitution somewhere.

If this is not suitable, so be it. Your requirements are beyond my comprehension.
I am trying to help you, really, even if it doesn't always seem that way
Reply
#9
my requirement is very simple. i need to just do what an f-string does, but getting the contents between the quotes from somewhere else besides the source code literal. one example is the contents comes from a disk file such as might be done with internationalization.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#10
(Jan-25-2020, 07:28 PM)Skaperen Wrote: my requirement is very simple. i need to just do what an f-string does, but getting the contents between the quotes from somewhere else besides the source code literal. one example is the contents comes from a disk file such as might be done with internationalization.

I surrender as I cannot see what is stopping you from doing so. I am clearly not understanding.

Perhaps someone else can help.
I am trying to help you, really, even if it doesn't always seem that way
Reply


Forum Jump:

User Panel Messages

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