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.dev1
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.dev1.tar.gz (20.7 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.dev1-py3.7.egg (36.0 kB view details)

Uploaded Egg

f2format-0.3.0.dev1-py3.6.egg (35.9 kB view details)

Uploaded Egg

f2format-0.3.0.dev1-py3.5.egg (36.6 kB view details)

Uploaded Egg

f2format-0.3.0.dev1-py3.4.egg (36.6 kB view details)

Uploaded Egg

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

Uploaded PyPymacOS 10.14+ x86-64

f2format-0.3.0.dev1-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.dev1-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.dev1-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.dev1-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.dev1.tar.gz.

File metadata

  • Download URL: f2format-0.3.0.dev1.tar.gz
  • Upload date:
  • Size: 20.7 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.dev1.tar.gz
Algorithm Hash digest
SHA256 f7f04eb2778fcbb3640b06ef0d7af6a1558a20257036d10845521f9c76b553a2
MD5 e9c31ca569f1f9b5b712b615e26f90d3
BLAKE2b-256 6601ead2b4f7188c85b59cde57a1f89d8e8deb679ce3a6676285436645cce7ec

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.3.0.dev1-py3.7.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.dev1-py3.7.egg
Algorithm Hash digest
SHA256 9c2ebf4b6dc07fc8c7624925e8070ad9c2adb2d202c5f8e1588f6334030baf73
MD5 9af5e3bbbdcfa06b45c73e1db188f270
BLAKE2b-256 25f9f23ee11812abb1c8f11932e11973df3037da2f7ce9a5de5b41bd2bf7c247

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.3.0.dev1-py3.6.egg
  • Upload date:
  • Size: 35.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.3.0.dev1-py3.6.egg
Algorithm Hash digest
SHA256 911b281ad3899cf5a7c45fc619a249e7025c619a4e802ff2f27680a9c13ea468
MD5 a15341110455c07636c41f2fdf14232e
BLAKE2b-256 4e6fa37ca004e26fc75c49bcab9dbfdf623f6b7795c6c32098e3b75898589227

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.3.0.dev1-py3.5.egg
  • Upload date:
  • Size: 36.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.3.0.dev1-py3.5.egg
Algorithm Hash digest
SHA256 3e879f6a6ffadc23ccf6e69426a6fc39491a8cab63ed92b4c1cef1f80f4f852e
MD5 0cf4e0dc6b41029fb22aaf562b3542ef
BLAKE2b-256 e84f01f47f4f1b07e92985ee6584c8446385c0902200a83537437da291fa1a28

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.3.0.dev1-py3.4.egg
  • Upload date:
  • Size: 36.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.3.0.dev1-py3.4.egg
Algorithm Hash digest
SHA256 31377f3340beecff400a76226576591943da4895236d0cd47ad866240a7fda9f
MD5 8a7cfb2ff7ff4666b44b5bc9b52c3da8
BLAKE2b-256 f0c36edfddd635909b5e838d6543e1c877338af47cde708351f7141a66e88810

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.3.0.dev1-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.dev1-pp35-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 7af0ca776343d63e6c9422b35892992f104c234f2580b7ae4065b04931582c8f
MD5 32f283a92a804375e75a817d91bb5b38
BLAKE2b-256 c2281af67cb1c6c4e363fe91963e8184e967961c96bce9b952fa24f2ae0f32e1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.3.0.dev1-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.dev1-cp37-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 a9fc9d92c845a96dc697ac5cc050146e3bdcb8e65ab52bc6fa92508cb562ff15
MD5 798f8bafa4cb6c50fbe6f45c34e9be3b
BLAKE2b-256 b9477e8039f87a1e9783508e00a013163e0405e2b811897ca8e3c7e0a630c262

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.3.0.dev1-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.dev1-cp36-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 a5da41dfe2ebbb0853e91d4660d80b428b6b0852f946c96d419cc27bcc0cd5ad
MD5 b11701b2ae12a1076e8ccbe1df814a17
BLAKE2b-256 7d9c25bf98baeded9733ee5d761d6eead76809943e940b54ae4d1546054d7eb9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.3.0.dev1-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.dev1-cp35-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 2e8663cde11b2bf52f877bd9119b291f884bf81b862c49c6790385ffc1216549
MD5 cbefb48916513b5c417cbb08f5d3933a
BLAKE2b-256 4c3043d99194332beebe74d5e0ff4ec86e7d5ebfedaf01fe681e28bee3753141

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.3.0.dev1-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.dev1-cp34-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 de611e2d03791db5af16a08063d8743947295ed5b57e9e85728ab59d82ad2b7b
MD5 20d7a51ae2cc10ad5adf2ece1eb6d47c
BLAKE2b-256 a865e2def1f07462d40ec4f3b2a415acb024e769ad8b22f2a17308073d561fab

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