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.1.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.1-py3.7.egg (61.0 kB view details)

Uploaded Egg

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

Uploaded Egg

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

Uploaded Egg

f2format-0.4.1-py3.4.egg (62.4 kB view details)

Uploaded Egg

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

Uploaded PyPymacOS 10.14+ x86-64

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

Uploaded CPython 3.7macOS 10.14+ x86-64

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

Uploaded CPython 3.6macOS 10.14+ x86-64

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

Uploaded CPython 3.5macOS 10.14+ x86-64

f2format-0.4.1-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.1.tar.gz.

File metadata

  • Download URL: f2format-0.4.1.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.1.tar.gz
Algorithm Hash digest
SHA256 99bbfe031d5e4b5f3733ea94586fc6baea96d3b1823c58c88ab6d01b6e96a49e
MD5 cf8c9e0c6b00c5521e04784a31bc1e9d
BLAKE2b-256 a1e01a07cbc387dc4af5307b829696d06e0308d1f42935ec40c71a7f3978c852

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.4.1-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.1-py3.7.egg
Algorithm Hash digest
SHA256 eb861769f9089bbe9a816c9dc261c27ca3d3be5117118a992042e72536bd2f4c
MD5 9131044ceb7dbf60577ace5d5bfee0a4
BLAKE2b-256 9585d060bf8cfa52d87adddeac85e9986de59ccab4a9f1ff88cb336620becc7b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.4.1-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.1-py3.6.egg
Algorithm Hash digest
SHA256 95dc2cd30d7e6ced4b7be23b3d1e9ed32b0fa0c78579d2749b07a03d7db40436
MD5 601c29e9c160d094799ffaa22081feaf
BLAKE2b-256 04cbdf51780b4f1841a5bf4410da56378ad41175a5a06b157c823cac52715b95

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.4.1-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.1-py3.5.egg
Algorithm Hash digest
SHA256 6f4b8cb27e875f9717e6219f24794771e62cee54d92e504338b3a346dc71d93a
MD5 438a4551bb0d72bfdbac0c7afe8b5060
BLAKE2b-256 7ac03859255ac45f3cac9bd8bfa4b3c3e07c082c425c3e67b49107d568ae6722

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.4.1-py3.4.egg
  • Upload date:
  • Size: 62.4 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.1-py3.4.egg
Algorithm Hash digest
SHA256 65a4a01c66cccfe6acd1c2d3d4773ecb15b8e9211f352f729db275dda15883b3
MD5 d5a0ef27a5de6c0f8d93e735d6eada7c
BLAKE2b-256 e847912fb0fbc651dc51ea85e60d417ce6352fcb05f8e996f3c810cf26a3bd46

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.4.1-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.1-pp35-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 3e2557878885488869b25e6fa5f2f88e7c5f9ed4c8e9d5c2200d63a0bdc5f8f6
MD5 f82de24af30b0f98463d4cb3b30abf5a
BLAKE2b-256 778aba5a3f6329ff60377e0401b877e36b8b1bef27a8bfb6a478fd93dc48835a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.4.1-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.1-cp37-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 27c3fe8b9bfce6d690ead443d5e408d250b4e27e000e08743834f3d3cff87d0c
MD5 c2aa5d89641203befb4018e730866400
BLAKE2b-256 c02b129a78cb046c552815972d8ddd23c537bced8c5902483b99c370b10c623b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.4.1-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.1-cp36-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 6b855ad96c187e8cbc83efcf9eadf96bf446ac0e18045f4e74d207dfdcb654c7
MD5 aff29e158a7814a35a8eb4f0cbfb5c67
BLAKE2b-256 fd5c555c941adb00291beedc4c1d96d5d719630a462eebc2a43dedbbc75cae62

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.4.1-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.1-cp35-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 2a8fbe731c93e1435859af185899e9022e24c2193ee44cf586638b95c359f33a
MD5 5f12d80e7bc09c14db4f77b19bb2ddf6
BLAKE2b-256 4c78eb9ad6996b5e0d73f95e859434f0cfd31db556d301c6153b4ad9302a51b6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.4.1-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.1-cp34-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 4b2be2b937560553ea8cb56fdfcaae2b92505bf265c183f60e7db9bf17ca8560
MD5 42ffa58fdc378841ec098e92a4cb931c
BLAKE2b-256 cd7e24b82cbcd73c57180b4b65d405af1c75e4254a091aa04ed6a9b4b2f30830

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