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.dev0
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.dev0.tar.gz (21.1 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.dev0-py3.7.egg (36.1 kB view details)

Uploaded Egg

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

Uploaded Egg

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

Uploaded Egg

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

Uploaded Egg

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

Uploaded PyPymacOS 10.14+ x86-64

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

Uploaded CPython 3.7macOS 10.14+ x86-64

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

Uploaded CPython 3.6macOS 10.14+ x86-64

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

Uploaded CPython 3.5macOS 10.14+ x86-64

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

Uploaded CPython 3.4macOS 10.14+ x86-64

File details

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

File metadata

  • Download URL: f2format-0.3.0.dev0.tar.gz
  • Upload date:
  • Size: 21.1 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.dev0.tar.gz
Algorithm Hash digest
SHA256 1cbe65eaef0665da8cdb10a22fdb94383d1c2b1e91c92505793fdbdd469de869
MD5 50ead8f1dd134724ddd290c2418c459b
BLAKE2b-256 ae7eb0579b95c6150958db851d0e3901d1c16079618f213f166506d2efb79de0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.3.0.dev0-py3.7.egg
  • Upload date:
  • Size: 36.1 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.dev0-py3.7.egg
Algorithm Hash digest
SHA256 4e1a51fa3a5be48961dc35c370bd8fd6b5a3148b673d4b12e6537f8fff4ea8a0
MD5 041f80f322b1ae33a1d37962168eadf3
BLAKE2b-256 e8dd535a5f6e8b3639f38d79ace148be04cc64846a2747618adec27e83ab25a8

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for f2format-0.3.0.dev0-py3.6.egg
Algorithm Hash digest
SHA256 89aaa007fe4f18638931df4e5613eda2aaa7bdaf97ab490391c251e9639a8992
MD5 23095deb83a9ad17ea020fa4c2473f0a
BLAKE2b-256 fd76138ae89f698b9b2a9893a1dc00374d4ad642c6a2ef318d95d104becf97d3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.3.0.dev0-py3.5.egg
  • Upload date:
  • Size: 36.7 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.dev0-py3.5.egg
Algorithm Hash digest
SHA256 114525e44f58a96b5a0c9af0f9e701da5c64f5b791cf9c960a32722b85cff9bb
MD5 df8304e40f0a96bec959ccb3ef02fcdb
BLAKE2b-256 bb1cb66a1b846593ed97c24a97b4698073d281c62d7ac40a0bc84f0022876bcc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.3.0.dev0-py3.4.egg
  • Upload date:
  • Size: 36.8 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.dev0-py3.4.egg
Algorithm Hash digest
SHA256 02835473b361964e396dce999fa428602425650ef49ace0ac7bde0a931e15921
MD5 16e205bf621a32ae481f0a951361c57c
BLAKE2b-256 7bf0091da2fc92e439cd13369c991154b53f65d114357f00665ad2438e175c51

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.3.0.dev0-pp35-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 20.0 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.dev0-pp35-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 ea1fbfa7ed9c403e64f3f8be5993f079b082d33741747e1d92709877da2014ab
MD5 d497eef00bb8db573174435a1a18ae85
BLAKE2b-256 e4ab7b30d8c36fbd39c8c1ffe7508711177c5e352792a2bd517fd3daa9df0678

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.3.0.dev0-cp37-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 20.0 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.dev0-cp37-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 42d1accb10c5d1c28b8e4886407b02a9dcc1370433042d4670386a8db10dc407
MD5 2c0fd93b63d66b1109d536c8bf547b5c
BLAKE2b-256 54f71fbfc6ef4fa7f594e2de021659497f631d5d76e4499d2cf8e29b65e60643

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.3.0.dev0-cp36-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 20.0 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.dev0-cp36-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 058d05e1b1223e2dee1f7a46edadcae90202b17f953296b44a12c69f5295b46c
MD5 c0986cfbdeefa7b413c75a3204e38a2a
BLAKE2b-256 bca5e0a3958cfdffd90352740156cfb50e127f53fd8e4afe969512f19ccd8837

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.3.0.dev0-cp35-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 20.0 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.dev0-cp35-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 8385773046b9107079a2489a2a6981f3b3040c8fe3c074ed20528895392e7b01
MD5 341234895ab0327d3b2f61ae487a3128
BLAKE2b-256 0b78292c8052082e8bd57a931932482a932a52a1c8ad27738412a22c1d70cc67

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.3.0.dev0-cp34-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 20.0 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.dev0-cp34-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 668caa8e66bc2ef59f845fc9c077236dcc48423ffcb99e36c8f8f46c7c3f6e4e
MD5 23b017e510128207675bcce2f2f9c4c2
BLAKE2b-256 e3c68b476f592b945247671c3e0fc261a3293ce6e1538912e516fe0ac4e1474f

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