Back-port compiler for Python 3.6 f-string literals.
Project description
f2format
Write f-string in Python 3.6 flavour, then compile and distribute for older versions :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 guarentees 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.6
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:
f2format 0.1.2
usage: f2format [-h] [-n] <python source files and folders..>
Convert f-string to str.format for Python 3 compatibility.
options:
-h show this help message and exit
-n do not archive original files
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
release.sh provides a demo script, which may help integrate f2format in your development and distribution circle.
NB:
release.shis not an integrated automation script.
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
Wrapper works for conversion.
f2format.f2format(filename)
Args:
filename--str, file to be converted
The main conversion process.
f2format.convert(string, lineno)
Args:
string--str, context to be convertedlineo--dict<int: int>, line number to actual offset mapping
Returns:
str-- converted string
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 Distribution
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.1.4.tar.gz.
File metadata
- Download URL: f2format-0.1.4.tar.gz
- Upload date:
- Size: 7.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.3.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5cd871f7f2e78b3e00b3c1469ff230db285d2e52bbe465cc83169db1a367c9b6
|
|
| MD5 |
6093e793ff7dd066ef365a29018e233a
|
|
| BLAKE2b-256 |
fc26243794f9a4fcf2a5488730fe4a1280c1d8645b5d8b4ed429f7ad88f57683
|
File details
Details for the file f2format-0.1.4-py2.py3-none-any.whl.
File metadata
- Download URL: f2format-0.1.4-py2.py3-none-any.whl
- Upload date:
- Size: 8.2 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.3.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
28d5d2e6e53beb47f74ca65df8ef9b1a5931d54fdd70e31f1348e1e0088849ca
|
|
| MD5 |
50f03f63060322976e5e19c04c602f0d
|
|
| BLAKE2b-256 |
c5813b081eb597ab56c0389ed8303d501cbd17b09cfe9c488676ece5f219177b
|