f2format: Back-port compiler for Python 3.6 f-string literals.

f2format is an intelligent, yet imperfect, solution for f-string literal compatibility. You may write f-string literals in Python 3.6 flavour, and compile to a compatible version for end-users to run, just like Babel for JavaScript.

f2format shall replace f-string literals with str.format methods, whilst maintaining the original layout of source code. You can simply use

f2format /path/to/the/file_or_directory

which will rewrite all Python files in place. For instance,


original source 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")) ```

String concatenation, conversion, format specification, multi-lines and unicodes are all treated right. Also, f2format will archive original files in case there're any syntax breaches.

/r/Python Thread Link - github.com