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}')

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.0.post2.tar.gz (24.8 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.0.post2-py3.7.egg (37.4 kB view details)

Uploaded Egg

f2format-0.4.0.post2-py3.6.egg (37.3 kB view details)

Uploaded Egg

f2format-0.4.0.post2-py3.5.egg (38.0 kB view details)

Uploaded Egg

f2format-0.4.0.post2-py3.4.egg (38.1 kB view details)

Uploaded Egg

f2format-0.4.0.post2-pp35-none-macosx_10_14_x86_64.whl (24.0 kB view details)

Uploaded PyPymacOS 10.14+ x86-64

f2format-0.4.0.post2-cp37-none-macosx_10_14_x86_64.whl (24.0 kB view details)

Uploaded CPython 3.7macOS 10.14+ x86-64

f2format-0.4.0.post2-cp36-none-macosx_10_14_x86_64.whl (24.0 kB view details)

Uploaded CPython 3.6macOS 10.14+ x86-64

f2format-0.4.0.post2-cp35-none-macosx_10_14_x86_64.whl (24.0 kB view details)

Uploaded CPython 3.5macOS 10.14+ x86-64

f2format-0.4.0.post2-cp34-none-macosx_10_14_x86_64.whl (24.0 kB view details)

Uploaded CPython 3.4macOS 10.14+ x86-64

File details

Details for the file f2format-0.4.0.post2.tar.gz.

File metadata

  • Download URL: f2format-0.4.0.post2.tar.gz
  • Upload date:
  • Size: 24.8 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.4.0.post2.tar.gz
Algorithm Hash digest
SHA256 96f06428b29e415a162ced5e446b72bd5910842f6f02055f4b02578f644e2250
MD5 88faeebf429da1f755c76f6e4ee6e46e
BLAKE2b-256 445d46ad0cc7d327dfbdc3f6aeb82b242e09dd19606585c0196ebc7ac01a53a3

See more details on using hashes here.

File details

Details for the file f2format-0.4.0.post2-py3.7.egg.

File metadata

  • Download URL: f2format-0.4.0.post2-py3.7.egg
  • Upload date:
  • Size: 37.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.1

File hashes

Hashes for f2format-0.4.0.post2-py3.7.egg
Algorithm Hash digest
SHA256 3622e377be3c619f54eeb0546789bfded9673b8bc2f9cd8a93a58e4a6a534c3b
MD5 7d32f6c18595c9f16b56ee5ad1b11bf1
BLAKE2b-256 be7bfbfb5406d454dc32400a41954e19d9b12fc8d5f5d9391c01293dbdf7234b

See more details on using hashes here.

File details

Details for the file f2format-0.4.0.post2-py3.6.egg.

File metadata

  • Download URL: f2format-0.4.0.post2-py3.6.egg
  • Upload date:
  • Size: 37.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.4.0.post2-py3.6.egg
Algorithm Hash digest
SHA256 68fc1ad04d52afb1c61f55fe1341f23f6609583c95cae9e1547c4c8576850045
MD5 46dc471265928e878ccf45044b525bb7
BLAKE2b-256 2345477466f2dd6aeea22d6650d1a16c16baaf3dcdbab5f5a2bb710f99f27283

See more details on using hashes here.

File details

Details for the file f2format-0.4.0.post2-py3.5.egg.

File metadata

  • Download URL: f2format-0.4.0.post2-py3.5.egg
  • Upload date:
  • Size: 38.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.4.0.post2-py3.5.egg
Algorithm Hash digest
SHA256 6cf397e38ff0c9829223ed48446cfd445619b34ce127a0b383e38255489d0659
MD5 c03858c8b77222d4af93f5b2bf6a2fd3
BLAKE2b-256 9f994b3981a103ee2f3d55617ae27e2af0917a18a01831a6b552ee2533054884

See more details on using hashes here.

File details

Details for the file f2format-0.4.0.post2-py3.4.egg.

File metadata

  • Download URL: f2format-0.4.0.post2-py3.4.egg
  • Upload date:
  • Size: 38.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.4.0.post2-py3.4.egg
Algorithm Hash digest
SHA256 966f5f1f158ba907d336722b27a0254fa1a05b045b8b0bf4f090d10b7472c37b
MD5 1631863a18421eb0dbcc4b1cd5d90e28
BLAKE2b-256 f87f0e619ac33c9f65185a9a9b96d1ab708087870b36cd61ee57718337427c2b

See more details on using hashes here.

File details

Details for the file f2format-0.4.0.post2-pp35-none-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: f2format-0.4.0.post2-pp35-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 24.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.4.0.post2-pp35-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 87e790c442c55ab903e3b460ad3dd885136546640ecab8b95ea749e59ebc01ed
MD5 ac16582537fa378d83562d62e46b550a
BLAKE2b-256 898f704429d48521f0e611b383c00403ec6bb82c37a73452002c05cd52cc116c

See more details on using hashes here.

File details

Details for the file f2format-0.4.0.post2-cp37-none-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: f2format-0.4.0.post2-cp37-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 24.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.4.0.post2-cp37-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 0ec6d9223a3a63790eb998a3176adc60f8d59e4ca2811eba6b7c11d360bb0ba2
MD5 7fbedfd567fe966237fc96564030b370
BLAKE2b-256 9e2da67c1f845b42efb1b4a44b98d59dc96941d708bb0676a9b678ee91c78efe

See more details on using hashes here.

File details

Details for the file f2format-0.4.0.post2-cp36-none-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: f2format-0.4.0.post2-cp36-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 24.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.4.0.post2-cp36-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 d97e28feb06393ee4c15c867754215e59b38e91952d5d58ebc733478d63c7269
MD5 8b1e7111ae07a9a714216d976c532ecd
BLAKE2b-256 43bc065eb0e89a844400dcd2ff0f6deb84fa9ffcbd5935b587fa91cff8904310

See more details on using hashes here.

File details

Details for the file f2format-0.4.0.post2-cp35-none-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: f2format-0.4.0.post2-cp35-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 24.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.4.0.post2-cp35-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 d4e8a22d8686718c640cf5588c6fa59e1425606022919685be5ba006eec83b91
MD5 6b75f74d40bc3ace23aa3b433b713e95
BLAKE2b-256 d0388ea5846dbf9c771205930f20af80ef362179b7bbeb559f7da573bc9053c1

See more details on using hashes here.

File details

Details for the file f2format-0.4.0.post2-cp34-none-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: f2format-0.4.0.post2-cp34-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 24.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.4.0.post2-cp34-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 a4623f75df29fab701eaf9663912755280a8e3dd324b64aa0835d91e5f47a227
MD5 00940984d7b3d7666b6265e1e312057e
BLAKE2b-256 43d0c5b3afd64d19915cc7128e38a2e9991124976a65382be48bbf5c75e7c3e3

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