Skip to main content

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:

f2format 0.3.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.

  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 and twine
  • permission to these files and folders granted

  And it will

  • copy setup.py and src to release directory
  • run f2format for Python files under release
  • 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 converted
  • lineno -- 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


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.3.0.tar.gz (20.7 kB view details)

Uploaded Source

Built Distributions

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

f2format-0.3.0-py3.7.egg (35.9 kB view details)

Uploaded Egg

f2format-0.3.0-py3.6.egg (35.9 kB view details)

Uploaded Egg

f2format-0.3.0-py3.5.egg (36.5 kB view details)

Uploaded Egg

f2format-0.3.0-py3.4.egg (36.6 kB view details)

Uploaded Egg

f2format-0.3.0-pp35-none-macosx_10_14_x86_64.whl (19.9 kB view details)

Uploaded PyPymacOS 10.14+ x86-64

f2format-0.3.0-cp37-none-macosx_10_14_x86_64.whl (19.9 kB view details)

Uploaded CPython 3.7macOS 10.14+ x86-64

f2format-0.3.0-cp36-none-macosx_10_14_x86_64.whl (19.9 kB view details)

Uploaded CPython 3.6macOS 10.14+ x86-64

f2format-0.3.0-cp35-none-macosx_10_14_x86_64.whl (19.9 kB view details)

Uploaded CPython 3.5macOS 10.14+ x86-64

f2format-0.3.0-cp34-none-macosx_10_14_x86_64.whl (19.9 kB view details)

Uploaded CPython 3.4macOS 10.14+ x86-64

File details

Details for the file f2format-0.3.0.tar.gz.

File metadata

  • Download URL: f2format-0.3.0.tar.gz
  • Upload date:
  • Size: 20.7 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

Hashes for f2format-0.3.0.tar.gz
Algorithm Hash digest
SHA256 cc403a0d022e082766b376610828ad358a2afcc54e9f50ff050e2f5bfb30b77b
MD5 2d0e7d5cfb88a9e309de726035ad270e
BLAKE2b-256 25124d2ce5717aa488e3ef0bb66d137943fdbfd7d6c0c709266b0b7f4eadcec1

See more details on using hashes here.

File details

Details for the file f2format-0.3.0-py3.7.egg.

File metadata

  • Download URL: f2format-0.3.0-py3.7.egg
  • Upload date:
  • Size: 35.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

Hashes for f2format-0.3.0-py3.7.egg
Algorithm Hash digest
SHA256 5bd60bb76779014644263e6d3ac29f71a848dd3e1316550212a8d71f2990839a
MD5 553ba712186972d79638ff0752c933a5
BLAKE2b-256 e9c6c664a43762236745dbcbefc78fa2a1451bd666544d2edc6078c1a3e1f3ee

See more details on using hashes here.

File details

Details for the file f2format-0.3.0-py3.6.egg.

File metadata

  • Download URL: f2format-0.3.0-py3.6.egg
  • Upload date:
  • Size: 35.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

Hashes for f2format-0.3.0-py3.6.egg
Algorithm Hash digest
SHA256 c2fe6346b7353c76ee4affd59a7c7ccf4d2105836b33ba83b778461155b0defe
MD5 25b50c9d982a634a733d356fd4e1a108
BLAKE2b-256 b08f9b2c49a578a59453096d9ba935e88579fab6ea2b12fbb9427b49f4448984

See more details on using hashes here.

File details

Details for the file f2format-0.3.0-py3.5.egg.

File metadata

  • Download URL: f2format-0.3.0-py3.5.egg
  • Upload date:
  • Size: 36.5 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

Hashes for f2format-0.3.0-py3.5.egg
Algorithm Hash digest
SHA256 4b19d3b1ae427bcbf188586b3e5628d65c60603f0f93cd67386e9e8b44033438
MD5 9a4e2ef73f4dff759ad47c6974b866e9
BLAKE2b-256 55ff76051831fbc72fb75e5d6f3c7c1470ec573cab0eed734e17742f81fcf164

See more details on using hashes here.

File details

Details for the file f2format-0.3.0-py3.4.egg.

File metadata

  • Download URL: f2format-0.3.0-py3.4.egg
  • Upload date:
  • Size: 36.6 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

Hashes for f2format-0.3.0-py3.4.egg
Algorithm Hash digest
SHA256 8043a6b9caf0e1bc19a968ffd753abc98e0e3e4fab2fd4177463853d0ae8f26b
MD5 7e8901dac8ffd01b9d03ed7340810e91
BLAKE2b-256 51dc697841096b5ccc76a36029d4e1fea12ff04c411b46f6704d987a240c94e8

See more details on using hashes here.

File details

Details for the file f2format-0.3.0-pp35-none-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: f2format-0.3.0-pp35-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 19.9 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

Hashes for f2format-0.3.0-pp35-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 023c177fe1630ca0da30910c962c55a53005e74cb66473ed6a3db17632513498
MD5 8cd72e9adb249fa15cb5cd71b3647416
BLAKE2b-256 cb21e315099e7005a8f0fdc754d479692b076e26b1fb6cbf221874440852843d

See more details on using hashes here.

File details

Details for the file f2format-0.3.0-cp37-none-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: f2format-0.3.0-cp37-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 19.9 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

Hashes for f2format-0.3.0-cp37-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 7393e6198b08928dc711957f3eb0fa6030d42648978623bafb8a1ed6fe257930
MD5 b2cfe38c6f7d8f0b4ef305e5a9469630
BLAKE2b-256 824ff5f3ed22d8ecd72c5b9a5313b777c37aaf6d3eb22c249922592207d276c6

See more details on using hashes here.

File details

Details for the file f2format-0.3.0-cp36-none-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: f2format-0.3.0-cp36-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 19.9 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

Hashes for f2format-0.3.0-cp36-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 ff86389afc185f3662e5a17aadcf3a9e81d964fd6bbdc4a7499a49b51b75cb15
MD5 893ac5e24d3428e5bd31c61f82433fbd
BLAKE2b-256 b5e576607c84bea9ba30ccf6cfc153bffb8e7629990d48146727923609f58d28

See more details on using hashes here.

File details

Details for the file f2format-0.3.0-cp35-none-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: f2format-0.3.0-cp35-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 19.9 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

Hashes for f2format-0.3.0-cp35-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 fd1251fbb0f66d999c535b8b41cbf72f13a4d65793f83d97f8fc01797a7a2125
MD5 4472403dd63d96d61580bb1aa7f69e86
BLAKE2b-256 1229affc7eccd810da63b21414672c067fb87bb3b3ddc2e07aed516b2e600e2c

See more details on using hashes here.

File details

Details for the file f2format-0.3.0-cp34-none-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: f2format-0.3.0-cp34-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 19.9 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

Hashes for f2format-0.3.0-cp34-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 39aecc568c8452f9320936ec6c53c751ceb4a719b33e1e8774c39f58e57a85c3
MD5 dc3d010bdc7ca1807388e870f61b551e
BLAKE2b-256 7d84885c6450d43191d59d7b3f045e0cf1e0cd694938f1145105be3e92571d0d

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