Python Forum
instructing f-string over format()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
instructing f-string over format()
#8
a lot of my scripts generate what is call "user data", which is usually a bash script to be run, for instances being launched in the AWS EC2 cloud. these have a lot of variable substitution in them. i have been using .format, but i think f-strings will do well in these. i used to code them as multi-line triple-quoted strings but that was difficult to use with .format() so i switched each line being a single call to a function named ud() with .format() in there. so instead of:
ud("#!/bin/bash")
ud("export JOB='{}'".format(job_id))
ud("mkdir '{}'".format(log_dir_name))
ud("exec &>'{}".format(log_file_name))
...
ud("aws s3 sync '{}' 's3://{}{}'".format(log_dir_name,bucket,s3_prefix))
i can do:
ud(f"""#!/bin/bash
export JOB='{job_id}'
mkdir '{log_dir_name}'
exec &>'log_file_name'
...
aws s3 sync '{log_dir_name}' 's3://{bucket}{s3_prefix}'
""")
this is one of the reasons i wanted to get 3.6 or 3.7 running system-wide on my old Ubuntu 16.04 system that had 3.5 system-wide. upgrading to a full fresh install of Xubuntu 18.04 got me several things i wanted, including f-strings for these scripts.

i do expect to still use .format() in some other situations. but i do expect to migrate to only using f-strings for locally run scripts. stuff i make for others will use .format() for a very long time.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Messages In This Thread
instructing f-string over format() - by metulburr - Jul-13-2019, 12:27 AM
RE: instructing f-string over format() - by Skaperen - Jul-13-2019, 11:00 PM

Forum Jump:

User Panel Messages

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