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

convert options:
  compatibility configuration for none-unicode files

  -c CODING, --encoding CODING
                        encoding to open source files (default is '${ENCODING}')
  -v VERSION, --python-version VERSION
                        Python version of lexical scanner to analyse source
                        files (default is Python ${PYTHONVERSION})

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.4.2.tar.gz (34.3 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.4.2-py3.7.egg (61.0 kB view details)

Uploaded Egg

f2format-0.4.2-py3.6.egg (60.9 kB view details)

Uploaded Egg

f2format-0.4.2-py3.5.egg (62.2 kB view details)

Uploaded Egg

f2format-0.4.2-py3.4.egg (62.3 kB view details)

Uploaded Egg

f2format-0.4.2-pp35-none-macosx_10_14_x86_64.whl (34.8 kB view details)

Uploaded PyPymacOS 10.14+ x86-64

f2format-0.4.2-cp37-none-macosx_10_14_x86_64.whl (34.8 kB view details)

Uploaded CPython 3.7macOS 10.14+ x86-64

f2format-0.4.2-cp36-none-macosx_10_14_x86_64.whl (34.8 kB view details)

Uploaded CPython 3.6macOS 10.14+ x86-64

f2format-0.4.2-cp35-none-macosx_10_14_x86_64.whl (34.8 kB view details)

Uploaded CPython 3.5macOS 10.14+ x86-64

f2format-0.4.2-cp34-none-macosx_10_14_x86_64.whl (34.8 kB view details)

Uploaded CPython 3.4macOS 10.14+ x86-64

File details

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

File metadata

  • Download URL: f2format-0.4.2.tar.gz
  • Upload date:
  • Size: 34.3 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.2

File hashes

Hashes for f2format-0.4.2.tar.gz
Algorithm Hash digest
SHA256 56571acb42ac072969648c190fc820b01d0164272b305c6a64e0803de81210e3
MD5 120e0a41eb1b339dba2679318c9b53a2
BLAKE2b-256 3ea968646c00871407f5d57e6c8e4e88c34168644695b28537c604466109f04a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.4.2-py3.7.egg
  • Upload date:
  • Size: 61.0 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.2

File hashes

Hashes for f2format-0.4.2-py3.7.egg
Algorithm Hash digest
SHA256 a0893c65d1feda67aca666f070d7a30df571297ab00a1bdee93e007466f4a655
MD5 974d718050d720a113e61ca1961ba53e
BLAKE2b-256 235519e7039f82c5be98cea41e8088e8dcb6bf1f670015b78cec81cf197212a2

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for f2format-0.4.2-py3.6.egg
Algorithm Hash digest
SHA256 b5e72b0fc475c534e21791de4393d3f6256a632de77d6bde64a56f6280d62156
MD5 898a073ff3950b68ac84af0a4220d954
BLAKE2b-256 9884b6f689be3e0d1bbce189e3e0fa5aa1db18a53daba4bb29eede489bc67d9c

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for f2format-0.4.2-py3.5.egg
Algorithm Hash digest
SHA256 417c6290b346e395b709fd5aa8bc5e62a626b6362e47f01ba9d89b8fdb181aea
MD5 f3a60640e5f1da8f4579ce13f37a3fa8
BLAKE2b-256 f5688f4de152fdf02b6fcec0bc0665367896a955f808bd379389c7a559e94645

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for f2format-0.4.2-py3.4.egg
Algorithm Hash digest
SHA256 f660343b8287e1a65f2fac26f1582111b6d988305c0183ba8c342662496d109a
MD5 c5bd1f93e903f6d50a8cf2eac14210df
BLAKE2b-256 f0f217901ef8dc4393244d583e5641c68b0a36c0373f041745180cb37946bf41

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for f2format-0.4.2-pp35-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 4e5d9e238ae1d644a359aba25693c959df0305a815828410ce915a96a32818a5
MD5 53b4dd36be041d3fcd5dd364d7b05b78
BLAKE2b-256 8ef6d8468a9e94a45bba27ec5a93cea4d147efb0ab021175febdfca71ef4cb09

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for f2format-0.4.2-cp37-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 0b7606461238fc0f0e3b7402a49c7d3ce7bb841d07f9a7c6f602e7011bce8bb9
MD5 4d295ef5dd5649587d5dfcbd1c6a376e
BLAKE2b-256 57098727a6b4d9fea3c79e89efe37aa26e293564b4bc1acf01527a2f71d722c4

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for f2format-0.4.2-cp36-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 4e67538ea930ab86a9a7d23428497ee5c9bfc33734da834eeac99b550cd6b799
MD5 656928976dea6eabe306c44d71ac1d80
BLAKE2b-256 b3499f96924449e7ddb1dcf21f59a398e70785d042447e743ca7699f4c3983e7

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for f2format-0.4.2-cp35-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 7d4ee8549ebd2f51fc9502aac3e1b70e40d2dc4e9012f982a43b9ccbb5819cf3
MD5 c12273e3702e0489538d6b619f5944d1
BLAKE2b-256 e67e97083f8621f334aac72ede1bb7212dee381b65e0a5ac6fdd0a4f5f15e668

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for f2format-0.4.2-cp34-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 c9541269dfc0d8b256718b754ba17d5e45484f3d20f300bb014983f372a0a85f
MD5 5f2f039c5663b61dbb46e2fa227e63f6
BLAKE2b-256 3c0e88899d7f52e6eb3b2167db74da3fd821b684ad7551fcc51da707ee900182

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