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

  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.0
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

release.sh provides a demo script, which may help integrate f2format in your development and distribution circle.

NB: release.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.

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.2.tar.gz (10.3 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.2-py3.7.egg (12.3 kB view details)

Uploaded Egg

f2format-0.2.2-py3.6.egg (12.3 kB view details)

Uploaded Egg

f2format-0.2.2-py3.5.egg (12.4 kB view details)

Uploaded Egg

f2format-0.2.2-py3.4.egg (12.4 kB view details)

Uploaded Egg

f2format-0.2.2-py2.7.egg (7.9 kB view details)

Uploaded Egg

f2format-0.2.2-pp35-none-macosx_10_14_x86_64.whl (8.9 kB view details)

Uploaded PyPymacOS 10.14+ x86-64

f2format-0.2.2-pp27-none-macosx_10_14_x86_64.whl (8.9 kB view details)

Uploaded PyPymacOS 10.14+ x86-64

f2format-0.2.2-cp37-none-macosx_10_14_x86_64.whl (8.9 kB view details)

Uploaded CPython 3.7macOS 10.14+ x86-64

f2format-0.2.2-cp36-none-macosx_10_14_x86_64.whl (8.9 kB view details)

Uploaded CPython 3.6macOS 10.14+ x86-64

f2format-0.2.2-cp27-none-macosx_10_14_x86_64.whl (8.9 kB view details)

Uploaded CPython 2.7macOS 10.14+ x86-64

File details

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

File metadata

  • Download URL: f2format-0.2.2.tar.gz
  • Upload date:
  • Size: 10.3 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.2.tar.gz
Algorithm Hash digest
SHA256 9119371535ecac83a2d3d5a0536d55d2f51663cd9bfb9a6073095ebd0dc79e99
MD5 859125cbc50c22ab3b9f1afc8b96f77f
BLAKE2b-256 f8667c7fbc079284988d64eeefe03f1548f92b5d787d33a3efef5913c7d5d8f7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.2.2-py3.7.egg
  • Upload date:
  • Size: 12.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.2.2-py3.7.egg
Algorithm Hash digest
SHA256 c67b8c8338b403dae967d73b3f3b69a7457974e4fc4540973a1bc131a5ada750
MD5 db1ce49b04797d8db767a9a228eba564
BLAKE2b-256 b436d4803fa3f589d6790ce2ecceefd19e7634b60791fe3d13da001cc45fb8ee

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.2.2-py3.6.egg
  • Upload date:
  • Size: 12.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.2.2-py3.6.egg
Algorithm Hash digest
SHA256 da5bacfd2dbd66785bc60ce68f0377f2429e6107d03e3164f93f2773c4517266
MD5 2cee905620f321b0444d0897a595c95a
BLAKE2b-256 20be9309cac185063a35e6930918b06f2fa942fff6bdc5274dcd0207dd38001b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.2.2-py3.5.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.2-py3.5.egg
Algorithm Hash digest
SHA256 b0c93f6d607fdb029484b1b83e02edc4e20910c1655a4436e735fa6a131643fc
MD5 a177d0b2ea845c77357460bef6e21bd1
BLAKE2b-256 783b6f86ae05abb21771a155b132be308742d6c5c391eb623b37a7e88c410d07

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.2.2-py3.4.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.2-py3.4.egg
Algorithm Hash digest
SHA256 68c34ed5043cc1a9c8dbc1b7b1df7337b55baf0ec429b18ff22c83a4c6cb1ea3
MD5 8b588e3474dde2e29a5a3c70344f806c
BLAKE2b-256 e13789bb0d8dd57f44bd18745ae74d3d1ef9960ce148898567a1bbe7e3b0f4a2

See more details on using hashes here.

File details

Details for the file f2format-0.2.2-py2.7.egg.

File metadata

  • Download URL: f2format-0.2.2-py2.7.egg
  • Upload date:
  • Size: 7.9 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.2-py2.7.egg
Algorithm Hash digest
SHA256 1f56399bd9574d4394907bc4c1e07e529885ba85297af31f0fcc655c28863c2f
MD5 9cdd0817dbb874dfe500538f571f6143
BLAKE2b-256 9a10cc85781bb1cd51071b3a195871e8fcd27f5b1ca21214ee0ce3581554ba67

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.2.2-pp35-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 8.9 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.2-pp35-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 ccaad0a8897f3ca6246ec339f5febf40cf1f37fb85a5fef31a1cb781749a32d5
MD5 af65f3e215afb4c3385cda0522e11330
BLAKE2b-256 e3d8870eeeafe5d1cc2c6b8d8ffd04b8120ecfad5244148fafe9c4bf0eb696f3

See more details on using hashes here.

File details

Details for the file f2format-0.2.2-pp27-none-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: f2format-0.2.2-pp27-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 8.9 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.2-pp27-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 9ff49c516f08caaa5db936ff78d9b281d745daa36d06137dacdfa1a56ff7f5a6
MD5 501dc2b3ed45c4a56a384379870a03b0
BLAKE2b-256 50f662aacbfddf43c0cda6db55b412a77a8f1fd4c1537f4faf05ce45a2875bcf

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.2.2-cp37-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 8.9 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.2-cp37-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 849ed06ee61f4ab2bc3b38a2b9b4219d59ccb2ac398e118b00e17e520329e9ac
MD5 78ca18c9ff68d629da43d497f762b7c8
BLAKE2b-256 c86f33d6572214f8df56a22e56b05d92a893bd25a61102f033499228e839d46f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.2.2-cp36-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 8.9 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.2-cp36-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 0081bf42864b4acfb6a6bde76e8823b9bdb3f051ae21f2f06eeabe062e9ce17a
MD5 7da1d362def0ac60e32827dda5c77ed5
BLAKE2b-256 8a04fac83f6f0e94e9d5b41286dcd24cbabba1c62ec290f0c6b3174e22d770c5

See more details on using hashes here.

File details

Details for the file f2format-0.2.2-cp27-none-macosx_10_14_x86_64.whl.

File metadata

  • Download URL: f2format-0.2.2-cp27-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: CPython 2.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.2-cp27-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 6915758f703ffe8d13837f66f5e63e1529a469e7d20f1350d2e01ad69b0ebe3e
MD5 bcd3255765947102737da9dbb88a5b50
BLAKE2b-256 e20566d4afdf01a53a80a5656e6b845cb7ec4d96328e346280583335e69c2dae

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