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.tar.gz (25.0 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-py3.7.egg (40.8 kB view details)

Uploaded Egg

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

Uploaded Egg

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

Uploaded Egg

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

Uploaded Egg

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

Uploaded PyPymacOS 10.14+ x86-64

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

Uploaded CPython 3.7macOS 10.14+ x86-64

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

Uploaded CPython 3.6macOS 10.14+ x86-64

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

Uploaded CPython 3.5macOS 10.14+ x86-64

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

Uploaded CPython 3.4macOS 10.14+ x86-64

File details

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

File metadata

  • Download URL: f2format-0.4.0.tar.gz
  • Upload date:
  • Size: 25.0 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.tar.gz
Algorithm Hash digest
SHA256 d716b084e5d2fa74485c5f65313d91bb81bf747a57d7c18131736520e8fb806f
MD5 24dbb06d1e1053c516d696e8fe87bd28
BLAKE2b-256 0753b78fecc6e95be48c3e0c577ca80414d5e541ddb64c9c2bac23886ca39235

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.4.0-py3.7.egg
  • Upload date:
  • Size: 40.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.4.0-py3.7.egg
Algorithm Hash digest
SHA256 c057d72caaacf5790d2e1d635433f5cfbf23a28d6eb17718f938aa5bd7b98437
MD5 d1d7a7570d3511064947a67aa208e160
BLAKE2b-256 1c185e54665f6263d1b6cc66a1f5a4b7ca5bbfe7ac9ee2e95eb59661e93011e8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.4.0-py3.6.egg
  • Upload date:
  • Size: 40.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.4.0-py3.6.egg
Algorithm Hash digest
SHA256 b5c0bea4b0633bb6682d4381251eca4ea861369e09ceeb782279c3fa5ed4af7a
MD5 9ab4d805ef3bab60ea0ababfaddc4a41
BLAKE2b-256 d8fff2a69a85734f430f9676096724ab61d384c63eee9c2e4104d6a8fce9e942

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.4.0-py3.5.egg
  • Upload date:
  • Size: 41.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-py3.5.egg
Algorithm Hash digest
SHA256 18b7bee4d527d6584049376e2db19ce068fb304ebd68a546667b2ec71efeac6b
MD5 64329d46427df36972a21d0f761d82a3
BLAKE2b-256 573134cc13155af60e1fa64846d90a4a875d133d80448d49bea522ef5cb3c4b4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.4.0-py3.4.egg
  • Upload date:
  • Size: 41.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.4.0-py3.4.egg
Algorithm Hash digest
SHA256 7d9d6a3f4f0551977882dae32908c580191fae75cdf2cf37a5ec000946e59c2f
MD5 5bf49148958b844fb85f1d38cfdbc57e
BLAKE2b-256 a3b15a1e09fca67a7d6d75d400546358b0f1ef3c86e2bf8fe46bb95761ece3f1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.4.0-pp35-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 27.6 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-pp35-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 5ed10d6380647140803c0daff346f80e1296cc43151431a5bdc1dd34e3d3f711
MD5 3f1b64c4e692a359b3f55f60cb828963
BLAKE2b-256 4354551030bf5d77ffc4a1171f7bde55a76c18138f9cb4305b7be6ecd4cc79e5

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.4.0-cp37-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 27.6 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-cp37-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 157b3fea23f9f594d6de1b2cf5b9ce06a08cb25e82ad216c8f90888319fe7652
MD5 8ed57d791dde097a2ea905a747ece394
BLAKE2b-256 cd7ca64008244632332dd25b454e27965bac96f156825562d7bc645a754fc5c8

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.4.0-cp36-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 27.6 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-cp36-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 884c968a3e11d2c5a3dd795fea537a9b0349f8db6db9ba7d24b371d0f0d7ba2c
MD5 cd4c91abfd4457330e480af4ff1140c9
BLAKE2b-256 ef3c1f365d7659aabb3b025f579af910c2f242492427709cf9f068f4abe6a931

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.4.0-cp35-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 27.6 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-cp35-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 2a17154d445efb0adc57e8530cf6959d4d64bff831c6d5d40be863b202b503d0
MD5 1adadc113890b8b02181183bf918cb6d
BLAKE2b-256 44bc19bbedfe9c38bd7ddc58463e2f715725b6c7ebc488190741abd69ccbf436

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.4.0-cp34-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 27.6 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-cp34-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 1b7a881de4f651a2a788aa5916e564592e9e8def7e819a7102947163c552f470
MD5 be939210d17e5ada7fa13c4286a05415
BLAKE2b-256 e15536a24575227f64af1c160d99cf08249e2c51742ac8d7310da9ed402ece93

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