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

Uploaded Egg

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

Uploaded Egg

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

Uploaded Egg

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

Uploaded Egg

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

Uploaded PyPymacOS 10.14+ x86-64

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

Uploaded CPython 3.7macOS 10.14+ x86-64

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

Uploaded CPython 3.6macOS 10.14+ x86-64

File details

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

File metadata

  • Download URL: f2format-0.2.3.post1.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.post1.tar.gz
Algorithm Hash digest
SHA256 6eaf026f83bffce71df2183ec9eae18de9dd33f52ed45f58e3f290487ea8e5ef
MD5 83991a586269faa115ae546c27e67441
BLAKE2b-256 be7d470b33fb8d28054c594d570da5ce33a00cad4f9d2a8a40418b441d133401

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.2.3.post1-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.post1-py3.7.egg
Algorithm Hash digest
SHA256 7f341db983b8b56534e711165b33eaaf83945fa78f4540382ed4f968439806f3
MD5 f273cbd7eb4abdae37e26e1325bcf88e
BLAKE2b-256 21ce6d8af1144919483342e31533652bd267e44eec18c9bae9366ce9b2fde6b6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.2.3.post1-py3.6.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.post1-py3.6.egg
Algorithm Hash digest
SHA256 e02f011cc4c0e111c77b85ae2f82bf7826405c563cc583c47a9bf4a8995de415
MD5 9800584132322e50f65cf1a8e9b333ba
BLAKE2b-256 315039e824e6858920166c24912125a411060047ac94561c7b7ac90d499aecd7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.2.3.post1-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.post1-py3.5.egg
Algorithm Hash digest
SHA256 c5060449e691d4cd9bae55addcd2da0310a6b41958cb8ddb8f6555d1812a75c5
MD5 42e2b2f268bf49d87fe0dc0cd4fca9b8
BLAKE2b-256 f382d1a8c3afaa08624af38a3e901a54395a74e7d94ac78322b13325e40ad8ed

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.2.3.post1-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.post1-py3.4.egg
Algorithm Hash digest
SHA256 929d5c132ed9c80a67b05c32f1623e6b752d7a6289509c8e40971cb4678d8193
MD5 9abfc59abec99f9e3c91a3462a0551a2
BLAKE2b-256 fa7afe57fefc50308630ad1ba79a2a8469ee677a3086ceac93a56a4e1bcdebd0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.2.3.post1-pp35-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 9.1 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.post1-pp35-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 d88e9ac934322ee2993d61e0d14a7f355b707180e8887c06a4f62c7eb9f4f93f
MD5 d0f66bde3b9619f86f640f41dcb31c0d
BLAKE2b-256 4afb6622162e4e7663877f5c4bafacb54cb99fa3f81410fc0e658034a7edab69

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.2.3.post1-cp37-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 9.1 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.post1-cp37-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 38028b2dfcdb8c9b06e4c5e8b3b1a017f506483366fb9072193dfc796b7eb7f6
MD5 d12583611e0e3c656e373a3791fdbd4e
BLAKE2b-256 68b28238a24eaea564d5a88671607b92ee1633b9eea68f4d0f612f46dc4758ac

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.2.3.post1-cp36-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 9.1 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.post1-cp36-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 94e19b055356d9638c52d98d5b98a4feb33ced87ff51d97619d38fb2283c9bcb
MD5 386af2f76a85e1120b227654c57bc4dc
BLAKE2b-256 280e05e2babf0d17f92d7931beede2f4bf25c85770064daff0ef338024f7c619

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