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.3
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.3.tar.gz (9.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.2.3-py3.7.egg (12.5 kB view details)

Uploaded Egg

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

Uploaded Egg

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

Uploaded Egg

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

Uploaded Egg

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

Uploaded PyPymacOS 10.14+ x86-64

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

Uploaded CPython 3.7macOS 10.14+ x86-64

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

Uploaded CPython 3.6macOS 10.14+ x86-64

File details

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

File metadata

  • Download URL: f2format-0.2.3.tar.gz
  • Upload date:
  • Size: 9.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.2.3.tar.gz
Algorithm Hash digest
SHA256 ea1515be17572a831db6fa1b999533340988504a36c0f36b1f0b4bdc2e167fe0
MD5 dc9dcfc0caac8f1c9ce3dd6d7cf9684f
BLAKE2b-256 e7df82f0dd6fd59d115bd432875c6041c3b9c42ab412b51d95247698412bbd19

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.2.3-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.3-py3.7.egg
Algorithm Hash digest
SHA256 9da0b469ed64c960ca0c7b475fbe80b2d216614ea9fe9804e3a4355148230f87
MD5 d326750d6344289d0718bab9b9c4b1ea
BLAKE2b-256 504923d79d6ec9d0e57477d9aa745f8b2c5a8faf2c2aeec7f462fad1ebc1356b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.2.3-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.3-py3.6.egg
Algorithm Hash digest
SHA256 acd608d6113fd6776626413a17addb0be30565fed81f0f6ed5cfef2aab11cd88
MD5 18872587a1f040524f97937fb8b6b023
BLAKE2b-256 f6bf18b5623759d2337e897db2777af8c13bce9f193ca9f47defafe0a6d52572

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.2.3-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.3-py3.5.egg
Algorithm Hash digest
SHA256 4cf980afcc5ac2b2dbe05731d4fb6ed13bced1eff0310c00da4e5de672b2d495
MD5 d04a2d00b63f1a46a39d01cdee24ce1e
BLAKE2b-256 ecd71479a631c92b882418291b4885830e71d7d09ecd76c02230716036a3f78f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.2.3-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.3-py3.4.egg
Algorithm Hash digest
SHA256 fde3fca6a413985ee9fcbc747eb7f36e546923434e37c79b03e59f80fa2bc545
MD5 2d69e69bc390f78841438f2d8cbe468e
BLAKE2b-256 b1721d9a004d324a3dbb6fefa45df42eb8cbe97b7b45f61ae43343c7009bde43

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.2.3-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.3-pp35-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 0e25e629fa04ed9889ed88d7120e7a3901680c114e664ea0232c708fb8898694
MD5 8ebc4bf2fc61164b079c809bda1f826d
BLAKE2b-256 28e5a6e4ac6837372a94a38265ec6161b571f496066218457652a1732205c56a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.2.3-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.3-cp37-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 31d20e50978b732f0f6407079ed4aca4bb395cfbdb3f374c5d216f2094edfc91
MD5 8df52717cfa8437d2abd5b4e2870d83c
BLAKE2b-256 7ba7a16ba79d29622d584da1b014cad91cb1f0cb7085a4635eaaef1963efaffb

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.2.3-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.3-cp36-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 c478655f438c46f33f46765aba80ad45adbbac9c71cd856ede642cc134651fd2
MD5 4bb5183bb5cc2df39ac377946b2c266c
BLAKE2b-256 34d8df208206a42ad9be5c0497773e98508ce635acbd1eed5df9319794eba892

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