Reusable f-strings
Project description
Reusable f-strings
Unify on one format sytle. 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
listeral
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 divered 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 aquire():
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.1.6-cp38-cp38-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 613b40fbfc922f7e81d4b4f0ddf457aae6c852e6140abba3ed702ce5df73cffd |
|
MD5 | f77d603b673c9add7989acc7f21945af |
|
BLAKE2b-256 | f57a96fac39275023f54d5ca50666f9819f07a033069e2f71bde2f4c02013b50 |
Hashes for f_yeah-0.1.6-cp38-cp38-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 53684feff0b29af5e59d7b246358448befd95d538947a03f1430c83791deda73 |
|
MD5 | a242f169ecf47c5d8a66c3964d4d9b68 |
|
BLAKE2b-256 | f097be0e2ad131e64f5a254d95d30b67816064a3b4d43dc41028de7d1bdd3f70 |
Hashes for f_yeah-0.1.6-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1ff3372ae69df059096b006813d711c79554f2befe5134a7bb93019df8cfe3ac |
|
MD5 | cf228b817c71ed3550616e4f72100c3a |
|
BLAKE2b-256 | 6b6dee3c58a20f83263c0a122fcbf1cb2e4705832c1d9119d83f1fde9a073d70 |
Hashes for f_yeah-0.1.6-cp37-cp37m-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 244dccac3b6bb05a7ef8fdd36c657929464d92009dc02fb40f33a4834d71e8e8 |
|
MD5 | 02cd3c8b2268aed2e0ac8b800c4f8c5b |
|
BLAKE2b-256 | 8de6e398580f568c81c7afe280ea38c99ddbe059e5f2c48ef4d5a088665d1f01 |
Hashes for f_yeah-0.1.6-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | aae7684c3c2f5169654a6693fca94c5dcf491307463bac40d105f930a3f96172 |
|
MD5 | 523d0db3440b7e73fafffd08375079dd |
|
BLAKE2b-256 | 6a2150a2fceb3d543fb58358bbaae4282579ff72644b73a43e96e6046a31c49b |
Hashes for f_yeah-0.1.6-cp36-cp36m-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0946fb81ec5751711d5c7cba1d1c778f599dbb38873d587e844a386ebb4c6f90 |
|
MD5 | e778206d270fcbdc5d586969a82ea9b4 |
|
BLAKE2b-256 | bca19ac22a1d89ab533fb452af22be0838d7171099cef0b93af5beb35e1510a4 |