Bottom Page

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.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#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
Quote
#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.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#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
Quote
#5
it looks like your code is assuming which variables are in the pattern.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#6
Look again.
I am trying to help you, really, even if it doesn't always seem that way
Quote
#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.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#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
Quote
#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.
What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Quote
#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.
perfringo likes this post
I am trying to help you, really, even if it doesn't always seem that way
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)