Back-port compiler for Python 3.6 f-string literals.
Project description
f2format
Write f-string in Python 3.6 flavour, and let
f2format
worry 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
f2format
only 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.sh
is not an integrated automation script. It should be revised by design.
It assumes
- all source files in
/src
directory - using GitHub for repository management
- having release branch under
/release
directory - already installed
f2format
andtwine
- permission to these files and folders granted
And it will
- copy
setup.py
andsrc
torelease
directory - run
f2format
for 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
Hashes for f2format-0.4.3-pp35-none-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 57bde9505492f7490eac2b08df510b1818e9594516b831f02af58512a02aa978 |
|
MD5 | fe93803db14cae52338a67fe42e68c9c |
|
BLAKE2b-256 | 4ce20a1d81ce50bf9261eab486a71411d7e219a22c2b1324eb46a7a3bcfee439 |
Hashes for f2format-0.4.3-pp35-none-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7dbbbb0238d0d15be2ca907692cc81e6c270944cd4a1123f307b94ddfc6f1b7c |
|
MD5 | 4bdb07c7e21ea07f89f3aa75838e6337 |
|
BLAKE2b-256 | 64d971fceb595c12d638c20679851f21855d0fccc9593b7a45f01016fd35cd35 |
Hashes for f2format-0.4.3-cp37-none-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 151b93cbe32619eb5073d4d61b14dbf26a7449b4173ce47ce6948d142fd4b635 |
|
MD5 | e359011bccb9b0dabd671c937e0e768f |
|
BLAKE2b-256 | 0c7290cad8b0538a55ef23a54a3388856973afe1e4bc5bb51f6334bc78ddcf6c |
Hashes for f2format-0.4.3-cp37-none-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 462115427216192c5ea0e7727bb6de2389ea0f879cdd12210a6f612eff0b86c2 |
|
MD5 | 23bd5c4c82f2586282ce2d7320169009 |
|
BLAKE2b-256 | 60f30e484a0217eac97bb21975971b3479bd6a5f0d025375bd9c3a053a7c1cfc |
Hashes for f2format-0.4.3-cp36-none-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 19601ff57b10dd4415764599cfdb74f33c4282800ee70e0005024713492b7d2a |
|
MD5 | 3669945149e288859ffa12e35bea8747 |
|
BLAKE2b-256 | 8e20918660dffd28730b1c248ddf88d227e625f8796cc25def886a9fc8892e4f |
Hashes for f2format-0.4.3-cp36-none-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 45268e1af8e33166cea7beed3d80b123acea09db2f68c92fd9d7a3a3f2b2cf07 |
|
MD5 | 29d4800c92083c69eda31ccafba0e005 |
|
BLAKE2b-256 | 4b12475364ed5dbba2f2e2e5f80545ed2e21ea298652f2ea782b1a0c0d47222a |
Hashes for f2format-0.4.3-cp35-none-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5b7ebad3a67b5e34013b217e0c44b3ef28c9d3a9817692b9b9df1e88e2e0a89c |
|
MD5 | 91aa87d44bd5200cd990fbec4a244437 |
|
BLAKE2b-256 | 9e3c42ff420ff77109bdf6d3c023d88c4eb977c7849f97d4a2d920d2cb177922 |
Hashes for f2format-0.4.3-cp35-none-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b255d5db8ff40eb765e3a9a260d6d2aa5af22fa1053e1933b1ef4e04d205bca6 |
|
MD5 | 69ced3c87eecbc45d082e7cdd3365cc7 |
|
BLAKE2b-256 | 52edba5c5e7be87a2d84fd3429f0b8646fe5795a50473bd05557b20f077a944c |
Hashes for f2format-0.4.3-cp34-none-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dc4c6f0084ec1068c4fb9902bf48659f23ba0e1e3b28946d7d36878992373673 |
|
MD5 | 003a1fe9ff1367432fc597430ffae809 |
|
BLAKE2b-256 | 09fcd675613bf3d883daae9f8037e46bed7f5163463a901b6fd9a466ceda6c44 |
Hashes for f2format-0.4.3-cp34-none-macosx_10_14_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 16dc32d2459a24ab040e8a355165b24f57651406532981fad86bad5c2245234a |
|
MD5 | 973a35562d66172f32b2757c44b27d67 |
|
BLAKE2b-256 | 8b09ff10c6b2b03b8bee81584909eb139df79050dd9d80f46982d11e7c8437e7 |