Reusable f-strings
Project description
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
Built Distributions
Hashes for f_yeah-0.2.0-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0b9c1cbcc93483129171a7f8b10628bde87dc98ef600afb074e2a31c2f99274d |
|
MD5 | 1667aa4078efdd0b667e4c69131c31ca |
|
BLAKE2b-256 | 66ca97be5f531f46f028c3ddb0b8568f7fb5920c078d027493e3ebc7843fe784 |
Hashes for f_yeah-0.2.0-cp38-cp38-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1268dff4d04d815ee61d41a0da4a731d68cb56bd7f829937242548ccd7a7c51c |
|
MD5 | c34d6c9ee764ec50c98242fe1c6a7480 |
|
BLAKE2b-256 | 7baeb3f08dc4c31108856e9bb93da9a0159ab98c4b3eba14e12a761072281bb9 |
Hashes for f_yeah-0.2.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | db0beaf1aa371b73373f64e8ac640272c1675707e45bcda4c1a45d8223f95d97 |
|
MD5 | 7a1b973e00ab810b700915884423c529 |
|
BLAKE2b-256 | 8b5a1805d572b86af456f2bb3dd21c9a60642d589bef87429634b8c62addc536 |
Hashes for f_yeah-0.2.0-cp37-cp37m-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 06910e9c546182f0a4c20ee439fb8ec656f5b3406daf2eac6ad1e132cd751ff0 |
|
MD5 | 608945329d1a45b42f967d44633decd4 |
|
BLAKE2b-256 | 4b9fe998a5be294d2387e9eb4c9dc6a08f72c8f71cffbb8b495a979229ca346d |
Hashes for f_yeah-0.2.0-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d7d987aab5dfc4a8b7dfc3ad839fa86e8e366306071d747b7bc2d45495a84fb4 |
|
MD5 | e1f25737634864ae2033e788224035b4 |
|
BLAKE2b-256 | c177e972762a3fb0524e2e462b85cced9b429dbd840d681989681ba5c696948d |
Hashes for f_yeah-0.2.0-cp36-cp36m-macosx_10_15_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e8c771027395672094c13ee2e9fbabe31e8814ef018f7a9ef8c5895ef447d3de |
|
MD5 | 328d87e11ee837f406d2069ef11c80bb |
|
BLAKE2b-256 | 9f43dfeccf29bbc6dbdaec8fe74294f762dd036b5b0c4fedf53a98727761fddc |