Python Forum
i'm back to f-strings, again
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
i'm back to f-strings, again
#1
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?
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
(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.
Sig:
>>> import this

The UNIX philosophy: "Do one thing, and do it well."

"The danger of computers becoming like humans is not as great as the danger of humans becoming like computers." :~ Konrad Zuse

"Everything should be made as simple as possible, but not simpler." :~ Albert Einstein
Reply
#3
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.
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
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'
Reply
#5
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
>>> 
Skaperen likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to understand strings and lists of strings Konstantin23 2 795 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Splitting strings in list of strings jesse68 3 1,808 Mar-02-2022, 05:15 PM
Last Post: DeaD_EyE
  Back again on f-strings newbieAuggie2019 11 4,400 Oct-16-2019, 11:24 PM
Last Post: newbieAuggie2019
  Finding multiple strings between the two same strings Slither 1 2,537 Jun-05-2019, 09:02 PM
Last Post: Yoriz
  lists, strings, and byte strings Skaperen 2 4,258 Mar-02-2018, 02:12 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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