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:

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 (${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 (${CWD}/archive)

convert options:
  compatibility configuration for none-unicode files

  -c CODING, --encoding CODING
                        encoding to open source files (${LOCALE_ENCODING})
  -v VERSION, --python VERSION
                        convert against Python version (${LATEST_VERSION})

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

Environments

f2format currently supports two environment arguments:

  • F2FORMAT_VERSION -- convert against Python version (same as --python option in CLI)
  • F2FORMAT_ENCODING -- encoding to open source files (same as --encoding option in CLI)

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.5.1.tar.gz (24.9 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.5.1-py3.7.egg (15.3 kB view details)

Uploaded Egg

f2format-0.5.1-py3.6.egg (15.3 kB view details)

Uploaded Egg

f2format-0.5.1-py3.5.egg (15.5 kB view details)

Uploaded Egg

f2format-0.5.1-py3.4.egg (15.5 kB view details)

Uploaded Egg

f2format-0.5.1-pp35-none-manylinux1_x86_64.whl (17.3 kB view details)

Uploaded PyPy

f2format-0.5.1-pp35-none-macosx_10_14_x86_64.whl (13.7 kB view details)

Uploaded PyPymacOS 10.14+ x86-64

f2format-0.5.1-cp37-none-manylinux1_x86_64.whl (17.3 kB view details)

Uploaded CPython 3.7

f2format-0.5.1-cp37-none-macosx_10_14_x86_64.whl (13.7 kB view details)

Uploaded CPython 3.7macOS 10.14+ x86-64

f2format-0.5.1-cp36-none-manylinux1_x86_64.whl (17.3 kB view details)

Uploaded CPython 3.6

f2format-0.5.1-cp36-none-macosx_10_14_x86_64.whl (13.7 kB view details)

Uploaded CPython 3.6macOS 10.14+ x86-64

f2format-0.5.1-cp35-none-manylinux1_x86_64.whl (17.3 kB view details)

Uploaded CPython 3.5

f2format-0.5.1-cp35-none-macosx_10_14_x86_64.whl (13.7 kB view details)

Uploaded CPython 3.5macOS 10.14+ x86-64

f2format-0.5.1-cp34-none-manylinux1_x86_64.whl (17.3 kB view details)

Uploaded CPython 3.4

f2format-0.5.1-cp34-none-macosx_10_14_x86_64.whl (13.7 kB view details)

Uploaded CPython 3.4macOS 10.14+ x86-64

File details

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

File metadata

  • Download URL: f2format-0.5.1.tar.gz
  • Upload date:
  • Size: 24.9 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.3

File hashes

Hashes for f2format-0.5.1.tar.gz
Algorithm Hash digest
SHA256 db8cd77bac12460b5d346f1598ad0c02c8e027d72b2e2cb2d86754c99e4e80c9
MD5 7f6385eea5e1f27967160442acaea498
BLAKE2b-256 bd165771fbf6b441602b6b02cb0f4ee75570225c675d54a1afda444991919834

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.5.1-py3.7.egg
  • Upload date:
  • Size: 15.3 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.3

File hashes

Hashes for f2format-0.5.1-py3.7.egg
Algorithm Hash digest
SHA256 372459bbdc3fcb92ce291f869c444a1689a0ea6b6bf1a397ad675fcbccbbc869
MD5 ab5d3f1b2276fc90badf1118b1f4a095
BLAKE2b-256 8e86b7286d5dec7f1bab698814628ac04cb2adfc873fcbe1e21f9e0e501e63b6

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for f2format-0.5.1-py3.6.egg
Algorithm Hash digest
SHA256 3d079b4b1bd5929f55c9a0fe52c7132402f1fd86835ebe6831beddf2233fff54
MD5 dbbbf2add6c20f48b3b525a692d51296
BLAKE2b-256 514697a89638f7378c6c161c88e6a821cfd9fcc13cf4c58c51e1f0b5ef9716e1

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for f2format-0.5.1-py3.5.egg
Algorithm Hash digest
SHA256 f1d7229500ebc21e13f0482be338a8d818ddf73a27e86358eff198016a7a44e3
MD5 88ed568c2a53793be26eb75ded6f5859
BLAKE2b-256 e7819c1676f01832d0ec5e1e2085df524df628ea271d65abbc780510e1eba393

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for f2format-0.5.1-py3.4.egg
Algorithm Hash digest
SHA256 fa4298212605d0730d286604c28d44a3dd2b62c9e7ca6aac257c47f0b4e1ecea
MD5 a3f82ddf0433a598d99b401955841859
BLAKE2b-256 32b66c628e898f15b7be120f253f23f770134bd292977b68b04255ed965f24b0

See more details on using hashes here.

File details

Details for the file f2format-0.5.1-pp35-none-manylinux1_x86_64.whl.

File metadata

  • Download URL: f2format-0.5.1-pp35-none-manylinux1_x86_64.whl
  • Upload date:
  • Size: 17.3 kB
  • Tags: PyPy
  • 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.3

File hashes

Hashes for f2format-0.5.1-pp35-none-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ea3d1ae60e4d5c9ce1b1a03fe4e24f77a830ce416f7273bf4192f690fb5ea410
MD5 fddf7ecd6efa485f3994b58a93b753bd
BLAKE2b-256 08c77a320e5e18adb35bd6b51f4d5369d8b458d2f0eb6eb00e8cbc3eacbf918e

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for f2format-0.5.1-pp35-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 8d3c4fe70ae39ebf6136c5433fd2cadda9e019a93e702c579481d0ff01f37f26
MD5 3b0076a784dddb7958eaf296a9b0ea7c
BLAKE2b-256 53f18c93828c7dc8cafc32ce3992d3c9e44f43d2b7c61742090cb2f313747eac

See more details on using hashes here.

File details

Details for the file f2format-0.5.1-cp37-none-manylinux1_x86_64.whl.

File metadata

  • Download URL: f2format-0.5.1-cp37-none-manylinux1_x86_64.whl
  • Upload date:
  • Size: 17.3 kB
  • Tags: CPython 3.7
  • 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.3

File hashes

Hashes for f2format-0.5.1-cp37-none-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 32290a9ca743553911802e20d63835b4521a7e87401d0c51d799d63ed20dd348
MD5 d4e7cdab9a8c0d8419820ada38dccb04
BLAKE2b-256 1666e4dcb02397ebb10e98721cbbde4bfa3726115f3d3d8ad0308f64c2995d37

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for f2format-0.5.1-cp37-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 c27d4842339321673834d7e77a2d52ceefc03fece5ba0f77ee08d32bb4a883bb
MD5 783af376984b3fa742e197686f958d6c
BLAKE2b-256 dbfd203add6476abb533926fdccb069fa702f0e637d4cd5d60caae619bedf52c

See more details on using hashes here.

File details

Details for the file f2format-0.5.1-cp36-none-manylinux1_x86_64.whl.

File metadata

  • Download URL: f2format-0.5.1-cp36-none-manylinux1_x86_64.whl
  • Upload date:
  • Size: 17.3 kB
  • Tags: CPython 3.6
  • 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.3

File hashes

Hashes for f2format-0.5.1-cp36-none-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 21ea1b8b9473c9fae0f7ea2760a08b87da26ff0e8d13b58a684a26fee753bcbd
MD5 15ed048407537205694f080026d383f1
BLAKE2b-256 4f2085a586a29e25ee7e338e5f4c2e0c5d07bd52669e901b9804fccd1129a182

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for f2format-0.5.1-cp36-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 28a1123c0ba1b1933209a564a6bedf13912d7dfc69eca100e90426606711af8c
MD5 696aa3364c0a63f1296563744780f9e5
BLAKE2b-256 3c8b6148e6d765343946111c44aa136939608b3d371b2aaaa6e2749b2c72e322

See more details on using hashes here.

File details

Details for the file f2format-0.5.1-cp35-none-manylinux1_x86_64.whl.

File metadata

  • Download URL: f2format-0.5.1-cp35-none-manylinux1_x86_64.whl
  • Upload date:
  • Size: 17.3 kB
  • Tags: CPython 3.5
  • 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.3

File hashes

Hashes for f2format-0.5.1-cp35-none-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 dcc35d7f122d45b99197534b4a33180bd64f39fa9f5717039cddb9b2b8ec1a94
MD5 08fed1524096ec265f00332aa5734c4c
BLAKE2b-256 13d00938cc46cb612dcb0643c891f0a5ea573ea006ea1a540fd827f43b322c95

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for f2format-0.5.1-cp35-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 705dfc1299875d2ce8c5651d2ae5d5ecb88fa3ccde47925578e4566416c8390b
MD5 5c7855b069e54c698b040a13a8817b5b
BLAKE2b-256 d6623d05554e9bb77f4169f31c71bf056735236275566f141028cc76a1289604

See more details on using hashes here.

File details

Details for the file f2format-0.5.1-cp34-none-manylinux1_x86_64.whl.

File metadata

  • Download URL: f2format-0.5.1-cp34-none-manylinux1_x86_64.whl
  • Upload date:
  • Size: 17.3 kB
  • Tags: CPython 3.4
  • 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.3

File hashes

Hashes for f2format-0.5.1-cp34-none-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 cdccfe244b892ae6599b2b3057c695ab9c955f97fee021d3f2b00e0b797b5059
MD5 4c93456150e75396a862f3f7b94db6de
BLAKE2b-256 3d4e60b367d2e442ec7220cff8d981447ffe19761f91da109ef18f4e73bccf32

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for f2format-0.5.1-cp34-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 2af771aee28afe7a17461406c29be9a4ebc981311dc6c7891b6b1e5261708327
MD5 f6955314a872dba5ac2f590c94d782d6
BLAKE2b-256 1178d5edd765ea43fff2b2bf8344379f7997547f8302048c4c05e0b4c52831aa

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