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.2.4
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.2.4.tar.gz (8.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.2.4-py3.7.egg (12.5 kB view details)

Uploaded Egg

f2format-0.2.4-py3.6.egg (12.4 kB view details)

Uploaded Egg

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

Uploaded Egg

f2format-0.2.4-py3.4.egg (12.6 kB view details)

Uploaded Egg

f2format-0.2.4-pp35-none-macosx_10_14_x86_64.whl (9.0 kB view details)

Uploaded PyPymacOS 10.14+ x86-64

f2format-0.2.4-cp37-none-macosx_10_14_x86_64.whl (9.0 kB view details)

Uploaded CPython 3.7macOS 10.14+ x86-64

f2format-0.2.4-cp36-none-macosx_10_14_x86_64.whl (9.0 kB view details)

Uploaded CPython 3.6macOS 10.14+ x86-64

f2format-0.2.4-cp35-none-macosx_10_14_x86_64.whl (9.0 kB view details)

Uploaded CPython 3.5macOS 10.14+ x86-64

f2format-0.2.4-cp34-none-macosx_10_14_x86_64.whl (9.0 kB view details)

Uploaded CPython 3.4macOS 10.14+ x86-64

File details

Details for the file f2format-0.2.4.tar.gz.

File metadata

  • Download URL: f2format-0.2.4.tar.gz
  • Upload date:
  • Size: 8.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.1

File hashes

Hashes for f2format-0.2.4.tar.gz
Algorithm Hash digest
SHA256 327f526616965d20cae1c8d2c4cdce67487260832256e31387b48a195a2a9521
MD5 fef2caef182e68655d9772ee5a1ca4ce
BLAKE2b-256 c59e588103ed519affc31980a6ddf18c7fb72958f06ff2a938952271636ce2ea

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.2.4-py3.7.egg
  • Upload date:
  • Size: 12.5 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.4-py3.7.egg
Algorithm Hash digest
SHA256 901b5e55e2c46f1f1975ee96b9cbfefa345279a58aeefe39496f75901a1319ba
MD5 508975ad792883f15ea39bee852e7ef6
BLAKE2b-256 de0b7fe75033a3ff87c13d45542e3e2bc794b3aab6a0531d0e098fa3339ccd66

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.2.4-py3.6.egg
  • Upload date:
  • Size: 12.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.2.4-py3.6.egg
Algorithm Hash digest
SHA256 e4ea125dee5da3e109b71d46e479914a773ed9c1c237e43b2063b5f0ec549df7
MD5 07f1892d1e47ce48c053ebbb9ed16c91
BLAKE2b-256 1d68d60f6e0880403f936bc771d0868fdfbc201aee32c7d2151ea328314b048f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.2.4-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.4-py3.5.egg
Algorithm Hash digest
SHA256 cb7b225a3937b9f8fb901dfd36519bb3b37de20d5e40f273def85eb9bf5a35d6
MD5 6a2e665efa28c4912e6e69e9191b9844
BLAKE2b-256 0ef224ada6baa3c1d61f335d6120200d581a246371bf1d19beada74e5a87f2ee

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.2.4-py3.4.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.4-py3.4.egg
Algorithm Hash digest
SHA256 574ef6fb0fc18877daa217ba1f6963831891f031c8e979da2e6d5ced054eba30
MD5 18e232138ef9fa467eb2bc838d31286b
BLAKE2b-256 589cd916423f3907a2fe9852417fd0c70e75b1f9c2f69ebdc65edb8e1cf40487

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.2.4-pp35-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 9.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.2.4-pp35-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 8cfdb256a347896fd007b09c67ec2f1b0f44312fd92b015ad2057b5bc53f41d0
MD5 eaf45a906a6fb03bfcceae71dbdc04c9
BLAKE2b-256 45d6d05dddaf0b8c1520e0c593ac0258128f588fe2a8d0ccea5d99633cdadffc

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.2.4-cp37-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 9.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.2.4-cp37-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 6830f29916dc5a2eb363819af9e8e0b0fb04f7c1d1cbdc694b2466752e58799f
MD5 b5de4b36617ef2691ade837912a80597
BLAKE2b-256 9c50249fbafffbe27dd201441ecebb39bf8f6a7253e7284752959a14beae2a39

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.2.4-cp36-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 9.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.2.4-cp36-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 4f1235bd4e77eec3b75f9417b5d84487acab2a1eac56d4c2c44fc0690e1446c5
MD5 dfa7b5df609553f3a7905cac8a584c6a
BLAKE2b-256 bdf10cf92dada76f55dcb60a70eddeb0e8d41ee54a1ac19b319f586c7e7b358e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.2.4-cp35-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 9.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.2.4-cp35-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 292330b75c5a14d7733eb21b3adc6256c925e766f3758952322e722f9a433f27
MD5 5a79ef31095279c0488c3d0ffeff8efa
BLAKE2b-256 3fa45a2e66ba9ac041bd36b2186ad812a2c4643326204dc157d2433e4b90ea5a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.2.4-cp34-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 9.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.2.4-cp34-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 eecafd9e10829dcdc6e6aa81157594e064bb46eb53e9ae6d66974f12b3307db6
MD5 5c3ebdc0a5c6ff559308cb987af7aaf9
BLAKE2b-256 a59bf26522868d859df8fdd2ca9bfbdda1ea5e6b3976d27e0f37478ae43fc428

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