Skip to main content

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 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.0
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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

f2format-0.1.0b2.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

f2format-0.1.0b2-py2.py3-none-any.whl (6.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file f2format-0.1.0b2.tar.gz.

File metadata

  • Download URL: f2format-0.1.0b2.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0

File hashes

Hashes for f2format-0.1.0b2.tar.gz
Algorithm Hash digest
SHA256 b8e5d0b513871d1c19ecf0b0f48ceb448e73a366794c0f3ce89073de6b603b53
MD5 b83773c3b1978808ac1c8fd3ae95e332
BLAKE2b-256 b1c867df1871a2a4ad44afe194edefdea8442f5bf8590fdf8e599eb50acb9863

See more details on using hashes here.

File details

Details for the file f2format-0.1.0b2-py2.py3-none-any.whl.

File metadata

  • Download URL: f2format-0.1.0b2-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.6 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.2.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0

File hashes

Hashes for f2format-0.1.0b2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 169f05da10deb12f0c489357807ce8dab095af1f1b62a3923300eeb7ab55ab59
MD5 c9d6b9d0bc33333b8e59443356681f3d
BLAKE2b-256 7d9f03382b14952d67ff9a27a949fc37cbea289838224697bf8442da1b042ab9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page