Convert f-string to str.format for Python 3 compatibility.
Project description
f2format
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.
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.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
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.
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
Hashes for f2format-0.1.2-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 583bc563722eded8896aaff63b4623513ea36596ee75c7178bd50d8bb1a8c3ee |
|
MD5 | bcfb1e5c7d1e33315d138a0befb09a47 |
|
BLAKE2b-256 | b328edde98a4a6e68bb2a937428b06ef20349d05de0f3fae104902a008d655cc |