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.3.tar.gz (24.9 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.3-py3.7.egg (37.4 kB view details)

Uploaded Egg

f2format-0.4.3-py3.6.egg (37.4 kB view details)

Uploaded Egg

f2format-0.4.3-py3.5.egg (38.1 kB view details)

Uploaded Egg

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

Uploaded Egg

f2format-0.4.3-pp35-none-manylinux1_x86_64.whl (27.5 kB view details)

Uploaded PyPy

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

Uploaded PyPymacOS 10.14+ x86-64

f2format-0.4.3-cp37-none-manylinux1_x86_64.whl (27.5 kB view details)

Uploaded CPython 3.7

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

Uploaded CPython 3.7macOS 10.14+ x86-64

f2format-0.4.3-cp36-none-manylinux1_x86_64.whl (27.5 kB view details)

Uploaded CPython 3.6

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

Uploaded CPython 3.6macOS 10.14+ x86-64

f2format-0.4.3-cp35-none-manylinux1_x86_64.whl (27.5 kB view details)

Uploaded CPython 3.5

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

Uploaded CPython 3.5macOS 10.14+ x86-64

f2format-0.4.3-cp34-none-manylinux1_x86_64.whl (27.5 kB view details)

Uploaded CPython 3.4

f2format-0.4.3-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.3.tar.gz.

File metadata

  • Download URL: f2format-0.4.3.tar.gz
  • Upload date:
  • Size: 24.9 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.3.tar.gz
Algorithm Hash digest
SHA256 ec05c3499d0948c9692d875c9e19e58ccde61ed1138b6b3e5743e193bf8bbc82
MD5 3e4d707d9eb94532c7b9a251c9a08f7c
BLAKE2b-256 5293bb5984cc8cb711d49eefada6405ad4044da50b3c347056997453d52a86e0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.4.3-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.2

File hashes

Hashes for f2format-0.4.3-py3.7.egg
Algorithm Hash digest
SHA256 6073c448d8de1b0b5bd90fbc4978fae94390ddd28d79551e5a1bd4c745bd2e59
MD5 1f56a7cdc44c8c11534611183fa7820f
BLAKE2b-256 f2d18a6e0f2b0a11e66dcf691ce309c7133d26563839bd8ca4ee5c72d5348f92

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.4.3-py3.6.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.2

File hashes

Hashes for f2format-0.4.3-py3.6.egg
Algorithm Hash digest
SHA256 672d537a507037abd86171de2709d9d948cf70369a78621f6aeecfdb87da03e7
MD5 7c84a76f2a1218cdc18dfa8e2ad63f33
BLAKE2b-256 c12b06f917852a1d10c5a15c3fe7271ea9213a1b0d72f3a832781dc79a2f5254

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.4.3-py3.5.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.2

File hashes

Hashes for f2format-0.4.3-py3.5.egg
Algorithm Hash digest
SHA256 78d913ef9203f9344140534eadcc8f5e26a53cc020568b33f64aaf3e6672d9b1
MD5 4aef3bd9abb931f63342961e6a03f718
BLAKE2b-256 1f50d080932c7ef7ade89d9d298b11c0aa5a1b2d10ab6a83e8a2e44daab3e338

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.4.3-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.2

File hashes

Hashes for f2format-0.4.3-py3.4.egg
Algorithm Hash digest
SHA256 db3bb1a6e9166e9f064ea3f24106027cb2db48179726095b478220e9d6c3a7f7
MD5 45f70250d04c1b151a273d48b671aec3
BLAKE2b-256 aa16b2d0d16f3099486686b7cace8eafb6ff26b4ecd816a33dbed18d46d1e982

See more details on using hashes here.

File details

Details for the file f2format-0.4.3-pp35-none-manylinux1_x86_64.whl.

File metadata

  • Download URL: f2format-0.4.3-pp35-none-manylinux1_x86_64.whl
  • Upload date:
  • Size: 27.5 kB
  • Tags: PyPy
  • 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.3-pp35-none-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 57bde9505492f7490eac2b08df510b1818e9594516b831f02af58512a02aa978
MD5 fe93803db14cae52338a67fe42e68c9c
BLAKE2b-256 4ce20a1d81ce50bf9261eab486a71411d7e219a22c2b1324eb46a7a3bcfee439

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.4.3-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.2

File hashes

Hashes for f2format-0.4.3-pp35-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 7dbbbb0238d0d15be2ca907692cc81e6c270944cd4a1123f307b94ddfc6f1b7c
MD5 4bdb07c7e21ea07f89f3aa75838e6337
BLAKE2b-256 64d971fceb595c12d638c20679851f21855d0fccc9593b7a45f01016fd35cd35

See more details on using hashes here.

File details

Details for the file f2format-0.4.3-cp37-none-manylinux1_x86_64.whl.

File metadata

  • Download URL: f2format-0.4.3-cp37-none-manylinux1_x86_64.whl
  • Upload date:
  • Size: 27.5 kB
  • Tags: CPython 3.7
  • 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.3-cp37-none-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 151b93cbe32619eb5073d4d61b14dbf26a7449b4173ce47ce6948d142fd4b635
MD5 e359011bccb9b0dabd671c937e0e768f
BLAKE2b-256 0c7290cad8b0538a55ef23a54a3388856973afe1e4bc5bb51f6334bc78ddcf6c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.4.3-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.2

File hashes

Hashes for f2format-0.4.3-cp37-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 462115427216192c5ea0e7727bb6de2389ea0f879cdd12210a6f612eff0b86c2
MD5 23bd5c4c82f2586282ce2d7320169009
BLAKE2b-256 60f30e484a0217eac97bb21975971b3479bd6a5f0d025375bd9c3a053a7c1cfc

See more details on using hashes here.

File details

Details for the file f2format-0.4.3-cp36-none-manylinux1_x86_64.whl.

File metadata

  • Download URL: f2format-0.4.3-cp36-none-manylinux1_x86_64.whl
  • Upload date:
  • Size: 27.5 kB
  • Tags: CPython 3.6
  • 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.3-cp36-none-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 19601ff57b10dd4415764599cfdb74f33c4282800ee70e0005024713492b7d2a
MD5 3669945149e288859ffa12e35bea8747
BLAKE2b-256 8e20918660dffd28730b1c248ddf88d227e625f8796cc25def886a9fc8892e4f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.4.3-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.2

File hashes

Hashes for f2format-0.4.3-cp36-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 45268e1af8e33166cea7beed3d80b123acea09db2f68c92fd9d7a3a3f2b2cf07
MD5 29d4800c92083c69eda31ccafba0e005
BLAKE2b-256 4b12475364ed5dbba2f2e2e5f80545ed2e21ea298652f2ea782b1a0c0d47222a

See more details on using hashes here.

File details

Details for the file f2format-0.4.3-cp35-none-manylinux1_x86_64.whl.

File metadata

  • Download URL: f2format-0.4.3-cp35-none-manylinux1_x86_64.whl
  • Upload date:
  • Size: 27.5 kB
  • Tags: CPython 3.5
  • 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.3-cp35-none-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 5b7ebad3a67b5e34013b217e0c44b3ef28c9d3a9817692b9b9df1e88e2e0a89c
MD5 91aa87d44bd5200cd990fbec4a244437
BLAKE2b-256 9e3c42ff420ff77109bdf6d3c023d88c4eb977c7849f97d4a2d920d2cb177922

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.4.3-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.2

File hashes

Hashes for f2format-0.4.3-cp35-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 b255d5db8ff40eb765e3a9a260d6d2aa5af22fa1053e1933b1ef4e04d205bca6
MD5 69ced3c87eecbc45d082e7cdd3365cc7
BLAKE2b-256 52edba5c5e7be87a2d84fd3429f0b8646fe5795a50473bd05557b20f077a944c

See more details on using hashes here.

File details

Details for the file f2format-0.4.3-cp34-none-manylinux1_x86_64.whl.

File metadata

  • Download URL: f2format-0.4.3-cp34-none-manylinux1_x86_64.whl
  • Upload date:
  • Size: 27.5 kB
  • Tags: CPython 3.4
  • 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.3-cp34-none-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 dc4c6f0084ec1068c4fb9902bf48659f23ba0e1e3b28946d7d36878992373673
MD5 003a1fe9ff1367432fc597430ffae809
BLAKE2b-256 09fcd675613bf3d883daae9f8037e46bed7f5163463a901b6fd9a466ceda6c44

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.4.3-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.2

File hashes

Hashes for f2format-0.4.3-cp34-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 16dc32d2459a24ab040e8a355165b24f57651406532981fad86bad5c2245234a
MD5 973a35562d66172f32b2757c44b27d67
BLAKE2b-256 8b09ff10c6b2b03b8bee81584909eb139df79050dd9d80f46982d11e7c8437e7

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