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

Uploaded Egg

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

Uploaded Egg

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

Uploaded Egg

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

Uploaded Egg

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

Uploaded PyPymacOS 10.14+ x86-64

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

Uploaded CPython 3.7macOS 10.14+ x86-64

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

Uploaded CPython 3.6macOS 10.14+ x86-64

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

Uploaded CPython 3.5macOS 10.14+ x86-64

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

Uploaded CPython 3.4macOS 10.14+ x86-64

File details

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

File metadata

  • Download URL: f2format-0.4.0.post1.tar.gz
  • Upload date:
  • Size: 25.1 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.post1.tar.gz
Algorithm Hash digest
SHA256 5b4e760bc6ff08e0843efaafa7ec5ea73fbd76113eb1aef99ffb17c097158485
MD5 949d4786f8226524e8f08b678d6bbfd8
BLAKE2b-256 de5b4be9f10c0c0683a0e2cc306869d2dc7d7766fc548e27d590cd40a0b81105

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.4.0.post1-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.post1-py3.7.egg
Algorithm Hash digest
SHA256 1004268ce3c07a7b1ae66488d2109e895e66c85b8ee9ef3623d05f95d4d7bb35
MD5 633fbebf9cce1de2308a195085b18f92
BLAKE2b-256 de1b18357fa8912f3c98aced773736dda0340595d8b7e4934171ddd2cf1fe451

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.4.0.post1-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.post1-py3.6.egg
Algorithm Hash digest
SHA256 614e73490aac323e8a3389358834371c3d6342a0196c5e98bf45cbbf9c1a322c
MD5 64b09794543c6bdf9407538f73d43676
BLAKE2b-256 ca09857f4f04885ef22bb0b24d2994b9d3359378010751aa135b30be7072f666

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.4.0.post1-py3.5.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.post1-py3.5.egg
Algorithm Hash digest
SHA256 e1f06dcb8f316ecb5801a75c18c406a7f32e092a7cc91c3a3847d80e81910d91
MD5 1124e1180b92f10e5a5e8d161b83b7c7
BLAKE2b-256 fdf11cf3b436592894a6300da242627f811843b2c2819282611ba037192696d9

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.4.0.post1-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.post1-py3.4.egg
Algorithm Hash digest
SHA256 cbe88e71f6b1e2e0a536d1db1ee65e70d766a4fc3cf7b9abf44435d773c5b791
MD5 28e962751c8bdf12a1a53b57711b66e9
BLAKE2b-256 d2450b5989d05aba0d94f29abaf068dab3cda7983956db319d9d1e996a100fa7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.4.0.post1-pp35-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 27.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.1

File hashes

Hashes for f2format-0.4.0.post1-pp35-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 b42244fc4c4f9df82ccbbb04da1f7617d945f439278927c6352db04a23ff6397
MD5 c21670bf6d284b91c56ec28d4d884f1a
BLAKE2b-256 21d6d401a69e55dae0fc8ceda5f60f6970ba3dca2ceb03054a8175ecae663a77

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.4.0.post1-cp37-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 27.7 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.post1-cp37-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 9848a4f6d75284a167647390838c23c8f66815a9d6101f4b46ee4053c0e2fa5d
MD5 2bd2271ab805e6f633b48f248559b53c
BLAKE2b-256 b1e75da89612771124313d9e24eeee11c368b1e77215a9f32d50b2d1a1217bb7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.4.0.post1-cp36-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 27.7 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.post1-cp36-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 b9b21bbeb3a85c6f793212c7019509d136c2b97731b5a0566649f93c78acb024
MD5 e72075b472c26904a69e33fa80538266
BLAKE2b-256 24db23ed3e389fdf54570686fd0a5e8e0e9e625ce3c27c5952626edf465ced2d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.4.0.post1-cp35-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 27.7 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.post1-cp35-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 87132107685b23d5d7b6808e9061760c8358489a6561660bef35fb4a8fd58611
MD5 6e79de2107c84215a2303862b6d5348f
BLAKE2b-256 82b999ddbb71991e3d867cbcdec07d091c1462e06c02c94b3db632cf60ca86c6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.4.0.post1-cp34-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 27.7 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.post1-cp34-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 d5a3446733b3c73c9746b9ceb6cdcdc331077409233f66ae6629e6973164c1b8
MD5 46b8dab395b5dcaa7e5eb07905468589
BLAKE2b-256 a99d10515128de94e1bed7839d6270ad8fa4fe2a79d1f139ad99ff56efa4e045

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