f-strings(Python 3.6) style literal string interpolation.
Using f-strings(PEP 498) style literal string interpolation without Python 3.6.
Usages
Accessing the globals and locals.
import os import fmt as f g_foo = 'global-foo' g_bar = 'global-bar' g_num = 23 g_ls = [1, 2, 3] def scope(): l_foo = 'local-foo' l_bar = 'local-bar' print( f('{l_foo}, {l_bar}') ) # 'local-foo, local-bar' print( f('{g_foo}, {g_bar!r}') ) # "global-foo, 'global-bar'" scope() print( f('{{ }}') ) # '{ }' print( f('hex: {g_num:#x}') ) # '0x17' print( f('{os.EX_OK}') ) # '0' print( f('{g_ls[0]}, {g_ls[1]}, {g_ls[2]}') ) # '1, 2, 3'NOTE: Closure will be a little tricky, must pass the outside scope variables as arguments to f, which added a reference to inside the closure in order this can work.
import fmt as f def outer(x='xx'): y = 'yy' def inner(): print( f('{x}, {y}', x, y) ) # "xx, yy" return inner outer()()Expression evaluation.
from datetime import datetime import fmt as f class S(object): def __str__(self): return 'hello' def __repr__(self): return 'hi' def __format__(self, fmt): return 'abcdefg'[int(fmt)] print( f('{1234567890:,}') ) # '1,234,567,890' print( f('{1 + 2}') ) # '3' print( f('{str(1 + 2)!r}') ) # "'3'" print( f('{[i for i in range(5)]}') ) # '[0, 1, 2, 3, 4]' ls = range(5) print( f('{{i for i in ls}}') ) # 'set([0, 1, 2, 3, 4])' or '{0, 1, 2, 3, 4}' print( f('{{k:v for k,v in zip(range(3), range(3, 6))}}') ) # '{0: 3, 1: 4, 2: 5}' print( f('{datetime(1994, 11, 6):%Y-%m-%d}') ) # '1994-11-06' print( f('{list(map(lambda x: x+1, range(3)))}') ) # '[1, 2, 3]' print( f('{S()!s}, {S()!r}, {S():1}') ) # 'hello, hi, b'Also, you can register some namespaces for convenience.
import fmt as f f.mregister({'x': 1, 'y': 2}) # register multiple f.register('z', 3) # register only one def func(x, y): return x + y print( f('{func(x, y)}') ) # '3' print( f('{func(x, z)}') ) # '4' print( f('{func(y, z)}') ) # '5'NOTE: locals() maybe cover the globals(), globals() maybe cover the namespaces that you registered.
Installation
Install by pip:
[sudo] pip install fmt -U
LICENSE
Metadata
Release files for fmt 0.3.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| fmt-0.3.1.tar.gz | 5.9 kB | Without provenance | Details for fmt-0.3.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| fmt-0.3.1-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details for fmt-0.3.1-py2.py3-none-any.whl |
Total release size: 14.0 kB
Release files / fmt-0.3.1.tar.gz
| Download URL | fmt-0.3.1.tar.gz |
|---|---|
| Size | 5.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7665aa8cf3258d16557a20e4f657434d274d9d0e1755259a7a064c18875cbfa3
|
|
BLAKE2b-256 checksum How to use checksums |
45ab4f3a1184fe37a288df89bc609882256a47a663190683d70a477b35c90b2d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / fmt-0.3.1-py2.py3-none-any.whl
| Download URL | fmt-0.3.1-py2.py3-none-any.whl |
|---|---|
| Size | 8.1 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
ea6658900a51b12d8c9e0fe118279c6904daba6e77596e6aed7e8851461c8e33
|
|
BLAKE2b-256 checksum How to use checksums |
77056b4e068e35400b137a9d59fcf8d0f482693259f6e2c4f0ee967ee86f6e68
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for fmt 0.3.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.