Python Forum
dynamic f-string example
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
dynamic f-string example
#6
In the case where you need to aggregate information, which should result in a formatted string, you could do this in an isolated function. The benefit is, that everything is very close. You see which names are used and you see the format str in the same block.

The 3-File example. The ExitStack is only to save lines of code..
def make_string(file1, file2, file3):
    with contextlib.ExitStack() as stack:
        files = [stack.enter_context(open(f)) for f in (file1, file2, file3)]
        header1 = files[0].readline().strip()
        header2 = files[1].readline().strip()
        header3 = files[2].readline().strip()
    return f"Header1: {header1}\nHeader2: {header2}\nHeader3: {header3}"
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
dynamic f-string example - by Skaperen - Feb-16-2020, 03:53 AM
RE: dynamic f-string example - by DeaD_EyE - Feb-16-2020, 12:09 PM
RE: dynamic f-string example - by Skaperen - Feb-17-2020, 12:09 AM
RE: dynamic f-string example - by DeaD_EyE - Feb-17-2020, 07:08 AM
RE: dynamic f-string example - by Skaperen - Feb-18-2020, 12:51 AM
RE: dynamic f-string example - by DeaD_EyE - Feb-18-2020, 09:31 AM
RE: dynamic f-string example - by Skaperen - Feb-18-2020, 06:49 PM

Forum Jump:

User Panel Messages

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