Skip to main content

Reusable f-strings

Project description

fyeah - reusable f-strings

PyPI version License Code style: black

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

You can 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.5.0.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

f_yeah-0.5.0-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file f_yeah-0.5.0.tar.gz.

File metadata

  • Download URL: f_yeah-0.5.0.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for f_yeah-0.5.0.tar.gz
Algorithm Hash digest
SHA256 bfd234d8c7adf74bc6f1f17d996f1615ac2f76587924ff8a4fed2df248332a29
MD5 68a785864302a0c294e0af12fda5fcca
BLAKE2b-256 fecdf749ff71e7cfb0d7b20a16f54b9e7908bdd244af898a6fb45702c51bdb0f

See more details on using hashes here.

Provenance

The following attestation bundles were made for f_yeah-0.5.0.tar.gz:

Publisher: python-publish.yml on ucodery/fyeah

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: f_yeah-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for f_yeah-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5648ffdc8b34c7982536da7e30bedf5a0c03f73eda31d2222e51b597c1a35493
MD5 1420ac243f1e18250b94b93a4271d1b9
BLAKE2b-256 ff2a976e5a8576c52b0d5d69e346003477318c5aeae8fd37ff4a30a7478c9008

See more details on using hashes here.

Provenance

The following attestation bundles were made for f_yeah-0.5.0-py3-none-any.whl:

Publisher: python-publish.yml on ucodery/fyeah

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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