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.3.1.tar.gz (21.6 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.3.1-py3.7.egg (37.3 kB view details)

Uploaded Egg

f2format-0.3.1-py3.6.egg (37.3 kB view details)

Uploaded Egg

f2format-0.3.1-py3.5.egg (37.9 kB view details)

Uploaded Egg

f2format-0.3.1-py3.4.egg (38.0 kB view details)

Uploaded Egg

f2format-0.3.1-pp35-none-macosx_10_14_x86_64.whl (20.6 kB view details)

Uploaded PyPymacOS 10.14+ x86-64

f2format-0.3.1-cp37-none-macosx_10_14_x86_64.whl (20.6 kB view details)

Uploaded CPython 3.7macOS 10.14+ x86-64

f2format-0.3.1-cp36-none-macosx_10_14_x86_64.whl (20.6 kB view details)

Uploaded CPython 3.6macOS 10.14+ x86-64

f2format-0.3.1-cp35-none-macosx_10_14_x86_64.whl (20.6 kB view details)

Uploaded CPython 3.5macOS 10.14+ x86-64

f2format-0.3.1-cp34-none-macosx_10_14_x86_64.whl (20.6 kB view details)

Uploaded CPython 3.4macOS 10.14+ x86-64

File details

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

File metadata

  • Download URL: f2format-0.3.1.tar.gz
  • Upload date:
  • Size: 21.6 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.3.1.tar.gz
Algorithm Hash digest
SHA256 0b06bb9f631a6c26ea0ba4747ba244fdf8098a9c0ca97ccf538a6fed89181562
MD5 80389aa8449535e5732dac653d38be8b
BLAKE2b-256 49dda0667274c48f89579a9107836c6be4a8c6d6bed38ef5108f342184335c1f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.3.1-py3.7.egg
  • Upload date:
  • Size: 37.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.3.1-py3.7.egg
Algorithm Hash digest
SHA256 b4bb9c01fac858c018f3648c03f1bad188b0abd5804ff6b42894e7030e875f01
MD5 e59852ae6bd0fbbee468b3d43426dc9b
BLAKE2b-256 34f93a8e8bc19183135f3ebf523c666b65089a1abd1c3e29a4cdab09e638fd97

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.3.1-py3.6.egg
  • Upload date:
  • Size: 37.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.3.1-py3.6.egg
Algorithm Hash digest
SHA256 e8f5def687b73e815b62251bb572b6100eef60fc3e9812204809b5c8a75c024b
MD5 b5c4564e48ccf2592ed9fc325b39defd
BLAKE2b-256 228e8a7ae6ff53f7ef893c1df57764389646f63265aaeac72689abf32d024a11

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.3.1-py3.5.egg
  • Upload date:
  • Size: 37.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.3.1-py3.5.egg
Algorithm Hash digest
SHA256 223cdac88cd3a09e4bd6f93f78352ac5685988a936b975844821b32f4c287da9
MD5 7fa3baf1f2280a654e0c18136cc9d2bd
BLAKE2b-256 1c634a83f5a9d1a84c4b86faae34c05548d418db7b4af174f3c9a95f9e2bdf2a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.3.1-py3.4.egg
  • Upload date:
  • Size: 38.0 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.3.1-py3.4.egg
Algorithm Hash digest
SHA256 9010fe032b0a1f2f961b8c4ba1a82f92578f50decd0ff586be2802760cc03fcb
MD5 4ae3e84b210ddb8319ee438b1e0715ae
BLAKE2b-256 c4e81e4d3030367301ca66180a71715759b0ef3f7086bc85c0ddd22b1dedef52

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.3.1-pp35-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 20.6 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.3.1-pp35-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 37ce5fa159fbbf36682d335599b7025abf36b1719e1c52088bbb3b282236615c
MD5 c6b11f018e0e0d6562d43d18e5cadbbd
BLAKE2b-256 8ffa6937bf1a12889f19f97ea0f3bb1948615d2734e94835bf2290e3a9dbc6d7

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.3.1-cp37-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 20.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.1

File hashes

Hashes for f2format-0.3.1-cp37-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 5d25f77bed3712529d07053b04d4e457d150e84da9eb9a9c48e83a55a148b030
MD5 41113e4df8a7933c757bbcf8c7e28cc5
BLAKE2b-256 ead83a35d321cc39e617a9659a376b4a4b6e77416dc577f915a64ad57847306d

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.3.1-cp36-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 20.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.1

File hashes

Hashes for f2format-0.3.1-cp36-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 dee633e05f1849424d05509944c60c0822283a37bccb3312882b1fcd2f4fb61b
MD5 e62f50b65a634f8f41bc90a07d2b5c7e
BLAKE2b-256 ae695ae32444ca183aa35accde348fc623f0e8616a45309c49472381f3fa5987

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.3.1-cp35-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 20.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.1

File hashes

Hashes for f2format-0.3.1-cp35-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 ab930f651ce888160f5e835d28ae71885bb1f4391a9c381de503fdc2bab73b2c
MD5 3707aceade9e76d50c468025dc968322
BLAKE2b-256 081d9404f0fb45a036c5c2d7ccf3d16737fd587cab421f925ea06d3ead516336

See more details on using hashes here.

File details

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

File metadata

  • Download URL: f2format-0.3.1-cp34-none-macosx_10_14_x86_64.whl
  • Upload date:
  • Size: 20.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.1

File hashes

Hashes for f2format-0.3.1-cp34-none-macosx_10_14_x86_64.whl
Algorithm Hash digest
SHA256 416b8a0a68821bbf0900318743f04dcad8bf15b19cc5ab9c070b7fc639d263e2
MD5 6ce9f53e362fb9de9d3731b04a9f8f31
BLAKE2b-256 0bc716d9d5e75897219e2096cb0239d731c96095a68c6799611464f7e2e2e693

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