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.3.0-cp38-cp38-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6b934ac9b01f15f5a9cf3ed30c3b03030c8305afb2c4f04f39d9b8f4eadc8d65 |
|
MD5 | de729b2e43f69821a0bab4d340e5cc7b |
|
BLAKE2b-256 | ceba809408cbb0415eff48b23935835fb8c725539a3200b3d4afc33c02bfb7ed |
Hashes for f_yeah-0.3.0-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6a0b7d073f9b3e2e7d88beae9eb40b87404e1ee2c4c58b123149b1ce5f62b728 |
|
MD5 | e79e1f1693f908197a78cbbc640e4774 |
|
BLAKE2b-256 | 57034cf4cc0edf4100d50a46ade7f8640de1aba3dfcc88e90e3390d68e13ab69 |
Hashes for f_yeah-0.3.0-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 99f53611b60ac5e1f7f305126cd22e0980886c9791ff4366f5facdd689101e46 |
|
MD5 | 1fb638c83b13d04151eefc7faa988a7e |
|
BLAKE2b-256 | 309b31af2fdc29ad7570fcd83f400262e90384323af6cce67f3823701f3ee2c7 |
Hashes for f_yeah-0.3.0-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b46b813df5b743ed3d07877b065fa3c7a00bb1316bdca79e82691a3d7de9fc92 |
|
MD5 | c4a738160719d8fabe682862ae4cf369 |
|
BLAKE2b-256 | 33382f50f807005eb16b66a834799b964ab65863c3cca432f2cc086804e4a070 |
Hashes for f_yeah-0.3.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f424d84d76b4ad54e695ad3b6455215368aafe7adb18b2fec06a12e6ba1539d9 |
|
MD5 | 78ed5ef1f58d3e4edc901cd47d5e9af0 |
|
BLAKE2b-256 | df5baee5c26141d860f7fe64952197fda872e695dfbf354da0c7418e0469337f |
Hashes for f_yeah-0.3.0-cp36-cp36m-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 38d3f6d627544b951971aa131cde4bf042c9eeb7d22758b7b3763854fd00ab16 |
|
MD5 | ea874daeda77c6b9c13ae1f808d32ea3 |
|
BLAKE2b-256 | 13acab0d9c0ecc53e5ac7da8104b6df227913a305cb5f51678d8c01ae7498afc |