Python Forum
i'm back to f-strings, again - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: i'm back to f-strings, again (/thread-38239.html)



i'm back to f-strings, again - Skaperen - Sep-20-2022

i have a list of file name patterns. where each parameter is expected in the file name varies between names. but, this list has it all. the list was read in from a configuration file. it is stored in global space so all the functions that need it can access it. these names are in f-string format in the configuration file. there are 4, 6, or 8 names. there are 2 or 3 parameters which varies by which file it is.

next, i need to make functions that take arguments for each parameter, such which directory/folder they are in for each call or which file extension it has or the file's creator (a name that varies in location in the file name through the list). one of these functions will check that each file in the given directory/folder does exist.

the parameters will be passed as arguments. but these probably should be keyword arguments so the functions can get them as a directory.

so, how would the f-strings be read in from the configuration file?

how would a function, such as the one that tests existence, get the format parameters filled in?


RE: i'm back to f-strings, again - rob101 - Sep-20-2022

(Sep-20-2022, 06:10 PM)Skaperen Wrote: these names are in f-string format in the configuration file

What does this mean? You have replacement fields, predefined?

Maybe an example would help.


RE: i'm back to f-strings, again - Skaperen - Sep-21-2022

yeah, the file will have the same kind of thing as you see between quotes of an f-string literal but it would not be a literal. i cannot remember where i read this but i read that there was a way to pass such a string to some method called format that would format it in the specified/given context. it have looked at the description of the builtin format() but it does not seem to have a way to execute a snippet (stuff between {}) that is executable code. so i can put a var name in there but not an expression.

i can do what i want in Bash using the eval command. i just need to put '$' in front of the { for each snippet and make it run in a way that it captures the evaluation into a string. i have done the bash thing before.

but i now want to do this in Python.


RE: i'm back to f-strings, again - bowlofred - Sep-21-2022

You can eval in python as well, but I wouldn't do things that way. Too easy to make a mistake and do the wrong thing.

But these are just strings. If you want to make an fstring out of it, stick an f and some quotes around it.

>>> var = "chair"
>>> s = "my {var}"
>>> f_string = "f'" + s + "'"
>>> f_string
"f'my {var}'"
>>> eval(f_string)
'my chair'



RE: i'm back to f-strings, again - Gribouillis - Sep-21-2022

Instead of @bolowfred 's f_string = "f'" + s + "'" I suggest f_string = f"f{repr(s)}" in case s contains quote characters, for example
>>> var = 'chair'
>>> s = "'spam' {var}"
>>> 
>>> f1 = f"f{repr(s)}"
>>> eval(f1)
"'spam' chair"
>>> 
>>> f2 = "f'" + s + "'"
>>> eval(f2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1
    f''spam' {var}'
       ^^^^
SyntaxError: invalid syntax
>>>