Skip to main content

Python 3.6 f-string sympathy for Python 2.7. Now with printf()

Project description

Python 3.6 f-string sympathy (partial compatibility) module for Python 2.7 See https://www.python.org/dev/peps/pep-0498/ for the specification for Literal String Interpolation.

Example from PEP-0498

>>> import datetime
>>> name = 'Fred'
>>> age = 50
>>> anniversary = datetime.date(1991, 10, 12)
>>> f'My name is {name}, my age next year is {age+1}, my anniversary is {anniversary:%A, %B %d, %Y}.'
'My name is Fred, my age next year is 51, my anniversary is Saturday, October 12, 1991.'
>>> f'He said his name is {name!r}.'
"He said his name is 'Fred'."

Sympathetic output from fstring427

>>> from fstring427.fstring import Fmt as f
>>> import datetime
>>> name = 'Fred'
>>> age = 50
>>> anniversary = datetime.date(1991,10,12)
>>> str(f('My name is {name}, my age next year is {age+1}, my anniversary is {anniversary:%A, %B %d, %Y}.'))
'My name is Fred, my age next year is 51, my anniversary is Saturday, October 12, 1991.'
>>>  f('He said his name is {name!r}')()
"He said his name is 'Fred'"

Note the major differences:

  • f is a class, not a string type

  • f() evaluates the string

  • str() of a instance of f also evaluates the string

The underlying implementation is a subclass of the Python 2.7 Format class, and depends on internals. Obviously fragile and probably non-portable, but still serves my purpose.

## printf(), a convenience function

>>> printf('He said his name is {name!r}') He said his name is 'Fred'

which has the additional convenience of a temporary scope for kwargs

 >>> printf('He said his name is {name!r}', name="Sam")
He said his name is 'Sam'

Major incompatibilities

Python 3.6 f-strings were carefully designed, and cover edge cases that .format() does not, see https://mail.python.org/pipermail/python-ideas/2015-July/034726.html

fstring427 was implemented on top of .format() and shares the underlying implementation of lookups. If .format() can’t handle a {field}, fstring427 will evaluate field as a Python expression in the proper scope. In practice this means that:

a = 10
d = {'a': 'string', 10: 'int'}
printf("{d[a]")

prints string (Python 2.7 .format() behavior) instead of int (Python 3.6 f-string behavior). I’ve found this a small price to pay in my 2.7 code to get cleaner printing and string formatting.

Roadmap

  • Add printf() style logging module

  • Contemplate 2-3 port for printf() utility function (dealing with the kwargs scope)

Copyright 2017, Smartvid.io

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

fstring427-0.9.9.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fstring427-0.9.9-py2-none-any.whl (6.7 kB view details)

Uploaded Python 2

File details

Details for the file fstring427-0.9.9.tar.gz.

File metadata

  • Download URL: fstring427-0.9.9.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for fstring427-0.9.9.tar.gz
Algorithm Hash digest
SHA256 fdea61a7c43742bec01189de1e739ccd4b4982b092f5ffdc79eb0d2c281885b6
MD5 50601c5fda79e78f4ba0f3c1d06fc89e
BLAKE2b-256 3af44a32385a4c8c3d379adfc98de81d1132094d007e412f89c31a16699e9b7e

See more details on using hashes here.

File details

Details for the file fstring427-0.9.9-py2-none-any.whl.

File metadata

File hashes

Hashes for fstring427-0.9.9-py2-none-any.whl
Algorithm Hash digest
SHA256 5fd65ddfb1fffa52932c65741f0523ac781aa1086ac988cbdea45eed8b8442bd
MD5 1d9596edfb5686e749428aed2f4fb312
BLAKE2b-256 c486dbafa4ce325e154d1f713b4336315055ec3ffd8f5ab0791d0782ef286f7c

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