Back-port compiler for Python 3.6 f-string literals.
Project description
f2format
Write f-string in Python 3.6 flavour, and let
f2formatworry about back-port issues :beer:
Since PEP 498, Python introduced
f-string
literal in version 3.6. Though released ever since
December 23, 2016, Python
3.6 is still not widely used as expected. For those who are now used to f-string, f2format
provides an intelligent, yet imperfect, solution of a backport compiler by converting
f-strings to str.format literals, which guarantees you to always write f-string in Python
3.6 flavour then compile for compatibility later.
f2format is inspired and assisted by my mate @gousaiyang.
It functions by tokenising and parsing Python code into multiple abstract syntax trees (AST),
through which it shall synthesise and extract expressions from f-string literals, and then
reassemble the original string using str.format method. Besides
conversion and
format specification, f2format
also considered and resolved
string concatenation.
Also, it always tries to maintain the original layout of source code, and accuracy of syntax.
Installation
Note that
f2formatonly supports Python versions since 3.3
For macOS users, f2format is now available through Homebrew:
brew tap jarryshaw/tap
brew install f2format
# or simply, a one-liner
brew install jarryshaw/tap/f2format
Simply run the following to install the current version from PyPI:
pip install f2format
Or install the latest version from the git repository:
git clone https://github.com/JarryShaw/f2format.git
cd f2format
pip install -e .
# and to update at any time
git pull
Usage
CLI
It is fairly straightforward to use f2format:
context in
${...}changes dynamically according to runtime environment
usage: f2format [options] <python source files and folders...>
Convert f-string to str.format for Python 3 compatibility.
positional arguments:
SOURCE python source files and folders to be converted
(default is '${CWD}')
optional arguments:
-h, --help show this help message and exit
-V, --version show program's version number and exit
archive options:
duplicate original files in case there's any issue
-n, --no-archive do not archive original files
-p PATH, --archive-path PATH
path to archive original files (default is '${CWD}/archive')
convert options:
compatibility configuration for none-unicode files
-c CODING, --encoding CODING
encoding to open source files (default is '${ENCODING}')
f2format will read then convert all f-string literals in every Python file under this
path. In case there might be some problems with the conversion, f2format will duplicate all
original files it is to modify into archive directory ahead of the process, if -n not set.
For instance, the code will be converted as follows.
# the original code
var = f'foo{(1+2)*3:>5}bar{"a", "b"!r}boo'
# after `f2format`
var = 'foo{:>5}bar{!r}boo'.format((1+2)*3, ("a", "b"))
Automator
make-demo.sh provides a
demo script, which may help integrate f2format in your development and distribution circle.
NB:
make-demo.shis not an integrated automation script. It should be revised by design.
It assumes
- all source files in
/srcdirectory - using GitHub for repository management
- having release branch under
/releasedirectory - already installed
f2formatandtwine - permission to these files and folders granted
And it will
- copy
setup.pyandsrctoreleasedirectory - run
f2formatfor Python files underrelease - distribute to PyPI and TestPyPI using
twine - upload to release branch on GitHub
- upload original files to GitHub
APIs
f2format.f2format(filename)
Wrapper works for conversion.
Args:
filename--str, file to be converted
f2format.convert(string, lineno)
The main conversion process.
Args:
string--str, context to be convertedlineno--dict<int: int>, line number to actual offset mapping
Returns:
str-- converted string
Codec
f2format-codec registers a codec in Python
interpreter, which grants you the compatibility to write directly in Python 3.6 f-string syntax
even through running with a previous version of Python.
Test
The current test samples are under /test
folder. test_driver.py is the main entry point for tests.
Contribution
Contributions are very welcome, especially fixing bugs and providing test cases, which @gousaiyang is to help with, so to speak. Note that code must remain valid and reasonable.
Project details
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file f2format-0.3.1.tar.gz.
File metadata
- Download URL: f2format-0.3.1.tar.gz
- Upload date:
- Size: 21.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0b06bb9f631a6c26ea0ba4747ba244fdf8098a9c0ca97ccf538a6fed89181562
|
|
| MD5 |
80389aa8449535e5732dac653d38be8b
|
|
| BLAKE2b-256 |
49dda0667274c48f89579a9107836c6be4a8c6d6bed38ef5108f342184335c1f
|
File details
Details for the file f2format-0.3.1-py3.7.egg.
File metadata
- Download URL: f2format-0.3.1-py3.7.egg
- Upload date:
- Size: 37.3 kB
- Tags: Egg
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b4bb9c01fac858c018f3648c03f1bad188b0abd5804ff6b42894e7030e875f01
|
|
| MD5 |
e59852ae6bd0fbbee468b3d43426dc9b
|
|
| BLAKE2b-256 |
34f93a8e8bc19183135f3ebf523c666b65089a1abd1c3e29a4cdab09e638fd97
|
File details
Details for the file f2format-0.3.1-py3.6.egg.
File metadata
- Download URL: f2format-0.3.1-py3.6.egg
- Upload date:
- Size: 37.3 kB
- Tags: Egg
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e8f5def687b73e815b62251bb572b6100eef60fc3e9812204809b5c8a75c024b
|
|
| MD5 |
b5c4564e48ccf2592ed9fc325b39defd
|
|
| BLAKE2b-256 |
228e8a7ae6ff53f7ef893c1df57764389646f63265aaeac72689abf32d024a11
|
File details
Details for the file f2format-0.3.1-py3.5.egg.
File metadata
- Download URL: f2format-0.3.1-py3.5.egg
- Upload date:
- Size: 37.9 kB
- Tags: Egg
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
223cdac88cd3a09e4bd6f93f78352ac5685988a936b975844821b32f4c287da9
|
|
| MD5 |
7fa3baf1f2280a654e0c18136cc9d2bd
|
|
| BLAKE2b-256 |
1c634a83f5a9d1a84c4b86faae34c05548d418db7b4af174f3c9a95f9e2bdf2a
|
File details
Details for the file f2format-0.3.1-py3.4.egg.
File metadata
- Download URL: f2format-0.3.1-py3.4.egg
- Upload date:
- Size: 38.0 kB
- Tags: Egg
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9010fe032b0a1f2f961b8c4ba1a82f92578f50decd0ff586be2802760cc03fcb
|
|
| MD5 |
4ae3e84b210ddb8319ee438b1e0715ae
|
|
| BLAKE2b-256 |
c4e81e4d3030367301ca66180a71715759b0ef3f7086bc85c0ddd22b1dedef52
|
File details
Details for the file f2format-0.3.1-pp35-none-macosx_10_14_x86_64.whl.
File metadata
- Download URL: f2format-0.3.1-pp35-none-macosx_10_14_x86_64.whl
- Upload date:
- Size: 20.6 kB
- Tags: PyPy, macOS 10.14+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
37ce5fa159fbbf36682d335599b7025abf36b1719e1c52088bbb3b282236615c
|
|
| MD5 |
c6b11f018e0e0d6562d43d18e5cadbbd
|
|
| BLAKE2b-256 |
8ffa6937bf1a12889f19f97ea0f3bb1948615d2734e94835bf2290e3a9dbc6d7
|
File details
Details for the file f2format-0.3.1-cp37-none-macosx_10_14_x86_64.whl.
File metadata
- Download URL: f2format-0.3.1-cp37-none-macosx_10_14_x86_64.whl
- Upload date:
- Size: 20.6 kB
- Tags: CPython 3.7, macOS 10.14+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5d25f77bed3712529d07053b04d4e457d150e84da9eb9a9c48e83a55a148b030
|
|
| MD5 |
41113e4df8a7933c757bbcf8c7e28cc5
|
|
| BLAKE2b-256 |
ead83a35d321cc39e617a9659a376b4a4b6e77416dc577f915a64ad57847306d
|
File details
Details for the file f2format-0.3.1-cp36-none-macosx_10_14_x86_64.whl.
File metadata
- Download URL: f2format-0.3.1-cp36-none-macosx_10_14_x86_64.whl
- Upload date:
- Size: 20.6 kB
- Tags: CPython 3.6, macOS 10.14+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dee633e05f1849424d05509944c60c0822283a37bccb3312882b1fcd2f4fb61b
|
|
| MD5 |
e62f50b65a634f8f41bc90a07d2b5c7e
|
|
| BLAKE2b-256 |
ae695ae32444ca183aa35accde348fc623f0e8616a45309c49472381f3fa5987
|
File details
Details for the file f2format-0.3.1-cp35-none-macosx_10_14_x86_64.whl.
File metadata
- Download URL: f2format-0.3.1-cp35-none-macosx_10_14_x86_64.whl
- Upload date:
- Size: 20.6 kB
- Tags: CPython 3.5, macOS 10.14+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ab930f651ce888160f5e835d28ae71885bb1f4391a9c381de503fdc2bab73b2c
|
|
| MD5 |
3707aceade9e76d50c468025dc968322
|
|
| BLAKE2b-256 |
081d9404f0fb45a036c5c2d7ccf3d16737fd587cab421f925ea06d3ead516336
|
File details
Details for the file f2format-0.3.1-cp34-none-macosx_10_14_x86_64.whl.
File metadata
- Download URL: f2format-0.3.1-cp34-none-macosx_10_14_x86_64.whl
- Upload date:
- Size: 20.6 kB
- Tags: CPython 3.4, macOS 10.14+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
416b8a0a68821bbf0900318743f04dcad8bf15b19cc5ab9c070b7fc639d263e2
|
|
| MD5 |
6ce9f53e362fb9de9d3731b04a9f8f31
|
|
| BLAKE2b-256 |
0bc716d9d5e75897219e2096cb0239d731c96095a68c6799611464f7e2e2e693
|