A backport of fstrings to python<3.6
Project description
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:
- 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.
- The
- A
.pth
file which registers a codec on interpreter startup.
you may also like
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for future_fstrings-1.2.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 90e49598b553d8746c4dc7d9442e0359d038c3039d802c91c0a55505da318c63 |
|
MD5 | 9b6ebc3b489e1b68ea4a79985e0c6e96 |
|
BLAKE2b-256 | ab6dea1d52e9038558dd37f5d30647eb9f07888c164960a5d4daa5f970c6da25 |