Skip to main content

Reusable f-strings

Project description

fyeah - reusable f-strings

PyPI version License Code style: ruff

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), f'Expected value to be an integer, got {type(value)} instead'
    return value * value

def pow(value):
    assert isinstance(value, int), f'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.4.0.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

f_yeah-0.4.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file f-yeah-0.4.0.tar.gz.

File metadata

  • Download URL: f-yeah-0.4.0.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.0

File hashes

Hashes for f-yeah-0.4.0.tar.gz
Algorithm Hash digest
SHA256 a854c5f743637de14f6a9f57a9214655033656eaa80b09d5af3cb2afd69d5f04
MD5 cd4909f172742a259e391b0f7b0237e2
BLAKE2b-256 92c074c0149e8d18a6a339d9ed62b99895687ad98b5104e9e0f02f5ea85575f8

See more details on using hashes here.

File details

Details for the file f_yeah-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: f_yeah-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.0

File hashes

Hashes for f_yeah-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 551a03f0b06727454945a77f28a365ad402a68fac7df994a236b972d4f311c8f
MD5 7cf0fb10f89103cab20fdbbfa4c8995a
BLAKE2b-256 15cb3458d0adbf411e6158fd3e5bdd2d97e803a6911bbf981d870eae86d6ba72

See more details on using hashes here.

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