Skip to main content
Help the Python Software Foundation raise $60,000 USD by December 31st!  Building the PSF Q4 Fundraiser

Reusable f-strings

Project description

fyeah - reusable f-strings

Reusable f-strings

Unify on one format sytle. With F-yeah Just add parentheses and be on your way.

Usage

Keep using f-string formatting, but when you need to re-use a template, use the f function instead of the f listeral

These two lines are equivalent

print(f'about to put {os.getpid()} to sleep')
print(f('about to put {os.getpid()} to sleep'))
# "about to put 421 to sleep"

No longer choose between copying around f-string literals or continuing to use old-style format() calls.

Instead of this

def mul(value):
    assert isinstance(value, int), 'Expected value to be an integer, got {type(value)} instead'
    return value * value

def pow(value):
    assert isinstance(value, int), 'Expected value to be an integer, got {type(value)} instead'
    return value ** value

Or this

bad_check = 'expected value to be an integer, got {type(value)} instead'

def mul(value):
    assert isinstance(value, int), bad_check.format(value=value)
    return value * value

def pow(value):
    assert isinstance(value, int), bad_check.format(value=value)
    return value ** value

Just write the template once to get consistent strings that stay in sync.

from fyeah import f
bad_check = 'Expected value to be an integer, got {type(value)} instead'

def mul(value):
    assert isinstance(value, int), f(bad_check)
    return value * value

def pow(value):
    assert isinstance(value, int), f(bad_check)
    return value ** value

Why would I use a function over the literal?

f-string literals are evaluated when they are created. This makes situations like the following impossible.

class BaseListRunner:
    command = ['ls']
    args = []
    notify_running = '{self.__class__.__name__} is executing {self.command} with "{" ".join(self.args)}"'

    def run(self):
        log.debug(f(self.notify_running))
        subprocess.call(self.command + args)

class AllListRunner:
    def __init__(self):
        self.args.append('-A')

AllListRunner().run()
# DEBUG: AllListRunnner is executing ls with "-A"

Why would I use F-yeah instead of the format() builtin?

Although the format mini-language and f-strings share a lot of syntax, they have divered somewhat. You could use only format() for all your strings, but format() is more verbose and less flexible as compared to f-strings; enough so that f-strings were adopted into the language. Using F-yeah makes the following possible.

G_COUNT = 0
count_tracker = '{G_COUNT=} at {datetime.datetime.utcnow():%H:%M:%S}'

def aquire():
    G_COUNT += 1
    log.debug(f(count_tracker))

def release():
    G_COUNT -= 1
    log.debug(f(count_tracker))

def check():
    log.debug(f(count_tracker))
    return G_COUNT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for f-yeah, version 0.1.6
Filename, size File type Python version Upload date Hashes
Filename, size f_yeah-0.1.6-cp36-cp36m-macosx_10_14_x86_64.whl (34.6 kB) File type Wheel Python version cp36 Upload date Hashes View
Filename, size f_yeah-0.1.6-cp36-cp36m-manylinux1_x86_64.whl (127.3 kB) File type Wheel Python version cp36 Upload date Hashes View
Filename, size f_yeah-0.1.6-cp37-cp37m-macosx_10_14_x86_64.whl (34.6 kB) File type Wheel Python version cp37 Upload date Hashes View
Filename, size f_yeah-0.1.6-cp37-cp37m-manylinux1_x86_64.whl (128.6 kB) File type Wheel Python version cp37 Upload date Hashes View
Filename, size f_yeah-0.1.6-cp38-cp38-macosx_10_14_x86_64.whl (39.3 kB) File type Wheel Python version cp38 Upload date Hashes View
Filename, size f_yeah-0.1.6-cp38-cp38-manylinux1_x86_64.whl (136.4 kB) File type Wheel Python version cp38 Upload date Hashes View
Filename, size f-yeah-0.1.6.tar.gz (5.3 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page