Skip to main content

Reusable f-strings

Project description

fyeah - reusable f-strings

Reusable f-strings

Unify on one format style. 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 literal

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 diverged 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 acquire():
    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.

Source Distribution

f-yeah-0.2.0.tar.gz (6.2 kB view hashes)

Uploaded Source

Built Distributions

f_yeah-0.2.0-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl (134.0 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.5+ x86-64

f_yeah-0.2.0-cp38-cp38-macosx_10_15_x86_64.whl (39.2 kB view hashes)

Uploaded CPython 3.8 macOS 10.15+ x86-64

f_yeah-0.2.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl (128.9 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.5+ x86-64

f_yeah-0.2.0-cp37-cp37m-macosx_10_15_x86_64.whl (34.5 kB view hashes)

Uploaded CPython 3.7m macOS 10.15+ x86-64

f_yeah-0.2.0-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl (127.6 kB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.5+ x86-64

f_yeah-0.2.0-cp36-cp36m-macosx_10_15_x86_64.whl (34.6 kB view hashes)

Uploaded CPython 3.6m macOS 10.15+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page