Skip to main content

Reusable f-strings and t-strings

Project description

fyeah - reusable f-strings and t-strings

PyPI version License

Reusable f-strings and t-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 or t function instead of the f or t 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.6.0.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

f_yeah-0.6.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: f_yeah-0.6.0.tar.gz
  • Upload date:
  • Size: 9.3 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.6.0.tar.gz
Algorithm Hash digest
SHA256 041e1049b58d189d6630601a176a65ec6fc14b76f88a2bd054fd182bed3229f3
MD5 0c9de1e5969e2a34d79d684e37677010
BLAKE2b-256 529b4005f5fce503c42019dbbf36e21e0ca322776b168abd3cd132fe62b19338

See more details on using hashes here.

Provenance

The following attestation bundles were made for f_yeah-0.6.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.6.0-py3-none-any.whl.

File metadata

  • Download URL: f_yeah-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 7.1 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.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d9d906ed552324e4737123eab97fa3723ddafad5f6dccc5ab691e182d0f95ef7
MD5 ecb8ec5053f5b3f0c79cfd2625757c88
BLAKE2b-256 d82b46894dad3799807d07a1e58754cdfde6e78453deef240ca4fbb34286bfad

See more details on using hashes here.

Provenance

The following attestation bundles were made for f_yeah-0.6.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 Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page