GithubHelp home page GithubHelp logo

fstring427's Introduction

fstring427

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)

Similar functionality

Copyright 2017, Smartvid.io

fstring427's People

Contributors

seantrue avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.