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

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

release.sh provides a demo script, which may help integrate f2format in your development and distribution circle.

NB: release.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.

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.2.1.post1.tar.gz (10.2 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.2.1.post1-py3.7.egg (12.3 kB view details)

Uploaded Egg

f2format-0.2.1.post1-py3.5.egg (12.6 kB view details)

Uploaded Egg

f2format-0.2.1.post1-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

f2format-0.2.1.post1-py2.7.egg (7.9 kB view details)

Uploaded Egg

f2format-0.2.1.post1-py2-none-any.whl (8.8 kB view details)

Uploaded Python 2

File details

Details for the file f2format-0.2.1.post1.tar.gz.

File metadata

  • Download URL: f2format-0.2.1.post1.tar.gz
  • Upload date:
  • Size: 10.2 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.2.1.post1.tar.gz
Algorithm Hash digest
SHA256 3def47353786780d178f7a4f21903b6657af4f5815d241c7d0d81f4bcc5d6edf
MD5 2761b160e0b8255821020435ec0787f4
BLAKE2b-256 2156269903745356bb123009984950276fcaee860749d191cb3dff1cee078007

See more details on using hashes here.

File details

Details for the file f2format-0.2.1.post1-py3.7.egg.

File metadata

  • Download URL: f2format-0.2.1.post1-py3.7.egg
  • Upload date:
  • Size: 12.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.1

File hashes

Hashes for f2format-0.2.1.post1-py3.7.egg
Algorithm Hash digest
SHA256 cdb9bb1a0cbe0fdee60d3a3298140b241c5aecadd63ca1b08bcc5e94fddb8487
MD5 022f15f4d1f6a8c8b06cc4f30856543f
BLAKE2b-256 43c38d02e2b0c5dfd452f54d4fbeb784c255f045f0a4d6331cb808018eb26377

See more details on using hashes here.

File details

Details for the file f2format-0.2.1.post1-py3.5.egg.

File metadata

  • Download URL: f2format-0.2.1.post1-py3.5.egg
  • Upload date:
  • Size: 12.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.2.1.post1-py3.5.egg
Algorithm Hash digest
SHA256 4bce2fb1cd362489edeb28bd609bad87ac27b2a5f58e974fc1773b96b8257464
MD5 7de9135687eea10bdc283da98d19099d
BLAKE2b-256 36c87db92b61785afce730db33ec7fe4d077e46c5f8680b4fa6e9d3dfe06095b

See more details on using hashes here.

File details

Details for the file f2format-0.2.1.post1-py3-none-any.whl.

File metadata

  • Download URL: f2format-0.2.1.post1-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • 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.2.1.post1-py3-none-any.whl
Algorithm Hash digest
SHA256 1cc2af75cfad0b1e5d88056eb797708811dee3085e6aec62599694b7997002b2
MD5 568c5e340ac58a2f5b30959cabffb5b8
BLAKE2b-256 67ec108ba3a8cc9e4956a1e73e94775709d1faa279deeec764f55dd1eb4e05ff

See more details on using hashes here.

File details

Details for the file f2format-0.2.1.post1-py2.7.egg.

File metadata

  • Download URL: f2format-0.2.1.post1-py2.7.egg
  • Upload date:
  • Size: 7.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.2.1.post1-py2.7.egg
Algorithm Hash digest
SHA256 e0d6758abca0fb83acd0c24acc8c41522280736fdfc9ad7bc4694db662d11a58
MD5 3c809e81ea135d28a7811426d9fabf61
BLAKE2b-256 224ed3b6092a878b4bd9a42d4c5682d836f32e3fa7b99472cf016f6b18371e78

See more details on using hashes here.

File details

Details for the file f2format-0.2.1.post1-py2-none-any.whl.

File metadata

  • Download URL: f2format-0.2.1.post1-py2-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 2
  • 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.2.1.post1-py2-none-any.whl
Algorithm Hash digest
SHA256 2796fea0d06845c3b035e31c44b520262059feb498353549f25ebb1c5484a723
MD5 e32448a27189fd86589db13d70d6db72
BLAKE2b-256 bd2bc72e9c6ef2df7980936aae847f390454cf04c61e88e284f3a5e56e4b492b

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