Skip to main content

A backport of fstrings to python<3.6

Project description

Build Status Azure DevOps coverage

future-fstrings

A backport of fstrings to python<3.6.

Installation

pip install future-fstrings

Usage

Include the following encoding cookie at the top of your file (this replaces the utf-8 cookie if you already have it):

# -*- coding: future_fstrings -*-

And then write python3.6 fstring code as usual!

# -*- coding: future_fstrings -*-
thing = 'world'
print(f'hello {thing}')
$ python2.7 main.py
hello world

Showing transformed source

future-fstrings also includes a cli to show transformed source.

$ future-fstrings-show main.py
# -*- coding: future_fstrings -*-
thing = 'world'
print('hello {}'.format((thing)))

Transform source for micropython

The future-fstrings-show command can be used to transform source before distributing. This can allow you to write f-string code but target platforms which do not support f-strings, such as micropython.

To use this on modern versions of python, install using:

pip install future-fstrings[rewrite]

and then use future-fstrings-show as above.

For instance:

future-fstrings-show code.py > code_rewritten.py

How does this work?

future-fstrings has two parts:

  1. A utf-8 compatible codec which performs source manipulation
    • The codec first decodes the source bytes using the UTF-8 codec
    • The codec then leverages tokenize-rt to rewrite f-strings.
  2. A .pth file which registers a codec on interpreter startup.

you may also like

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

future_fstrings-1.2.0.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

future_fstrings-1.2.0-py2.py3-none-any.whl (6.1 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file future_fstrings-1.2.0.tar.gz.

File metadata

  • Download URL: future_fstrings-1.2.0.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.7

File hashes

Hashes for future_fstrings-1.2.0.tar.gz
Algorithm Hash digest
SHA256 6cf41cbe97c398ab5a81168ce0dbb8ad95862d3caf23c21e4430627b90844089
MD5 0221a15b77b426aa6b5049b2b05664e5
BLAKE2b-256 5de23874574cce18a2e3608abfe5b4b5b3c9765653c464f5da18df8971cf501d

See more details on using hashes here.

File details

Details for the file future_fstrings-1.2.0-py2.py3-none-any.whl.

File metadata

  • Download URL: future_fstrings-1.2.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.1 CPython/3.6.7

File hashes

Hashes for future_fstrings-1.2.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 90e49598b553d8746c4dc7d9442e0359d038c3039d802c91c0a55505da318c63
MD5 9b6ebc3b489e1b68ea4a79985e0c6e96
BLAKE2b-256 ab6dea1d52e9038558dd37f5d30647eb9f07888c164960a5d4daa5f970c6da25

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page