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 (${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 (${CWD}/archive)

convert options:
  compatibility configuration for none-unicode files

  -c CODING, --encoding CODING
                        encoding to open source files (${LOCALE_ENCODING})
  -v VERSION, --python VERSION
                        convert against Python version (${LATEST_VERSION})

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

Environments

f2format currently supports two environment arguments:

  • F2FORMAT_VERSION -- convert against Python version (same as --python option in CLI)
  • F2FORMAT_ENCODING -- encoding to open source files (same as --encoding option in CLI)

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.5.2.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.5.2-py3.7.egg (15.3 kB view details)

Uploaded Egg

f2format-0.5.2-py3.6.egg (15.3 kB view details)

Uploaded Egg

f2format-0.5.2-py3.5.egg (15.5 kB view details)

Uploaded Egg

f2format-0.5.2-py3.4.egg (15.5 kB view details)

Uploaded Egg

f2format-0.5.2-pp35-none-manylinux1_x86_64.whl (17.3 kB view details)

Uploaded PyPy

f2format-0.5.2-pp35-none-macosx_10_14_x86_64.whl (13.7 kB view details)

Uploaded PyPymacOS 10.14+ x86-64

f2format-0.5.2-cp37-none-manylinux1_x86_64.whl (17.3 kB view details)

Uploaded CPython 3.7

f2format-0.5.2-cp37-none-macosx_10_14_x86_64.whl (13.6 kB view details)

Uploaded CPython 3.7macOS 10.14+ x86-64

f2format-0.5.2-cp36-none-manylinux1_x86_64.whl (17.3 kB view details)

Uploaded CPython 3.6

f2format-0.5.2-cp36-none-macosx_10_14_x86_64.whl (13.6 kB view details)

Uploaded CPython 3.6macOS 10.14+ x86-64

f2format-0.5.2-cp35-none-manylinux1_x86_64.whl (17.3 kB view details)

Uploaded CPython 3.5

f2format-0.5.2-cp35-none-macosx_10_14_x86_64.whl (13.6 kB view details)

Uploaded CPython 3.5macOS 10.14+ x86-64

f2format-0.5.2-cp34-none-manylinux1_x86_64.whl (17.3 kB view details)

Uploaded CPython 3.4

f2format-0.5.2-cp34-none-macosx_10_14_x86_64.whl (13.6 kB view details)

Uploaded CPython 3.4macOS 10.14+ x86-64

File details

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

File metadata

  • Download URL: f2format-0.5.2.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.3

File hashes

Hashes for f2format-0.5.2.tar.gz
Algorithm Hash digest
SHA256 19d729bdd1d833134ac051caa0f6789a09d97d9f5c1742dae6e97fd74d501672
MD5 fd74d1b6de80bbc2ecbc4c36525a031d
BLAKE2b-256 03ef2295711be12391f3988cb8d339ba9726691b7d4fae29c8fe19c434e7c314

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.5.2-py3.7.egg
  • Upload date:
  • Size: 15.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.3

File hashes

Hashes for f2format-0.5.2-py3.7.egg
Algorithm Hash digest
SHA256 8ee300a911ebb415d952d14b3dc6215a5a4946770f886f57e8717e949b4d84ff
MD5 89e323147ec1026208f3509f05459b29
BLAKE2b-256 fe030e2aeafcfcc90034eea0a04f5b5de2f9df03ac0d41a3b9dee65928830fa0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.5.2-py3.6.egg
  • Upload date:
  • Size: 15.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.3

File hashes

Hashes for f2format-0.5.2-py3.6.egg
Algorithm Hash digest
SHA256 b11f280f9d6961695bc14f4f89c383c91da6dc718bcc919d6832d05e0a0a5ec1
MD5 00cffdcf08af4788db2cb5a681f6463f
BLAKE2b-256 3eef7bb527bb8436644a9da23828c860970dab752eb55dee4ae3f01942bfca2a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.5.2-py3.5.egg
  • Upload date:
  • Size: 15.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.3

File hashes

Hashes for f2format-0.5.2-py3.5.egg
Algorithm Hash digest
SHA256 cb4337eeab824849eb7a25a27e0bc8cf5f71ccee5410ebb2ae41706ff3ca76d3
MD5 112410900e272d9e8a9cc601b5ad4d3a
BLAKE2b-256 f1c53c98bc46f8cbfd8976a17437aec13ce304b0bf0c95a30843d4eee87d9a36

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.5.2-py3.4.egg
  • Upload date:
  • Size: 15.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.3

File hashes

Hashes for f2format-0.5.2-py3.4.egg
Algorithm Hash digest
SHA256 e528ca1bf5df4d0453279bcdb908540ecb6c6b3d93754bf12fdaed2b5b68e3c2
MD5 9ec7a08b0eddc3b2db2962c258c746fe
BLAKE2b-256 89d7b586a88fdde3fb9897bb1e03c0d25ca1e8f1f3801912cc3a2cfc2bbcf044

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.5.2-pp35-none-manylinux1_x86_64.whl
  • Upload date:
  • Size: 17.3 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.3

File hashes

Hashes for f2format-0.5.2-pp35-none-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a8d14f6cfd1d0ed3431a42601daaa20bf046539501473c8ac46d5ef5767b292c
MD5 4fcb48e1b2e0baf655ccae532f5af114
BLAKE2b-256 2ddfff9fdb769618977f60e7140384f75c1befb83316b8d322e1a23792f4a362

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.5.2-pp35-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 13.7 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.3

File hashes

Hashes for f2format-0.5.2-pp35-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 d59f3497c01cd6d494ca63da36f52493d80e08148abf37562bebac75e85089ae
MD5 130bd15eab988559561d57002baa1093
BLAKE2b-256 f8fe278770c729f010333b64edff34bbfc0fa2b757c887e7ca0a48d9b5b79369

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.5.2-cp37-none-manylinux1_x86_64.whl
  • Upload date:
  • Size: 17.3 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.3

File hashes

Hashes for f2format-0.5.2-cp37-none-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 f68102341e3ed71bff2ee22a0be0a3a1bc48b2530a1672fa58db7ede6e9d52e4
MD5 58b8b4fceba887725b4a92cbd59d2937
BLAKE2b-256 1c132aa457d406569c4b1c70613e72a8b943e9af48640aa626e60c56fcbc90d4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.5.2-cp37-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 13.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.3

File hashes

Hashes for f2format-0.5.2-cp37-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 16d06fb1f072ce560c43a9ab2f58013ccaacda5a3f2de1bdea1fafa2e958dab7
MD5 b2e7f9aff197f47a01a7b379a48a6cd6
BLAKE2b-256 a1fb466afef024adae62d23d650aa6c0759a34e498f01b1f764ca42df15385b9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.5.2-cp36-none-manylinux1_x86_64.whl
  • Upload date:
  • Size: 17.3 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.3

File hashes

Hashes for f2format-0.5.2-cp36-none-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 000027704b15f88a01cd183126e698efdb3b257d80ea5e75359e95e0f7e386f5
MD5 869460fd4a1d8d5d501f07c971d89f5f
BLAKE2b-256 df9fc553e8cc28c0e9d236220573f5aa153168e7552ef72b10b36c1c1e6b93b2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.5.2-cp36-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 13.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.3

File hashes

Hashes for f2format-0.5.2-cp36-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 543a9690541a81a4b81049600f059d4e48b41cfbbc328a0a7a6ff8e9b267c7d9
MD5 4a0327afa71261e634916063c12bee9e
BLAKE2b-256 19140196f3a8660aabf867ec30f9770f4bb8a94fc7782b1a6284ff7e983778d3

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.5.2-cp35-none-manylinux1_x86_64.whl
  • Upload date:
  • Size: 17.3 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.3

File hashes

Hashes for f2format-0.5.2-cp35-none-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 c2ed5f1e42b651d679e6f73d7fa3270ab4c266a110df9c14d4c504c46ac33948
MD5 187c7d6b97fab9c691dc65a982a4cf1c
BLAKE2b-256 fd55f6e40b06c92a827f9dd4cfce9c94e156b1bccd9f31bee0ca04aa07a56804

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.5.2-cp35-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 13.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.3

File hashes

Hashes for f2format-0.5.2-cp35-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 9acc5a4aab507a2c8035770bd47a42544a5f1c4afdcc96273e22cf2854b0d055
MD5 8d3e50aa46cf9fbbee2d2d8379336c7f
BLAKE2b-256 7c819e55f32182c538192952777636c12bd1de622e50a8af0a0f272e15cabc39

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.5.2-cp34-none-manylinux1_x86_64.whl
  • Upload date:
  • Size: 17.3 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.3

File hashes

Hashes for f2format-0.5.2-cp34-none-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 db9b9c55f38079e931212388368f8ffa1b595dd88e3ab68b3a8956eedf5d2a3f
MD5 7d0f849212e4da319093fa5935e03317
BLAKE2b-256 62c7935e60c9395f38eb89c4876a480549be7b9cf6e35d4ecafc42021a6bf7e2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.5.2-cp34-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 13.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.3

File hashes

Hashes for f2format-0.5.2-cp34-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 b0b360d888e7a62f3ea6e9b10d9712b8831ffe5775de4a9babbd11d44525d3aa
MD5 5b685c101375fb4cdd3ef60dccbb563b
BLAKE2b-256 59b42b3bcc4ea148323054e824f1260c9ee835fdda2b0bf7639df207687ab047

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