[Python-Dev] summary of transitioning from % to {} formatting (original) (raw)

Mark Dickinson dickinsm at gmail.com
Sat Oct 3 18:34:41 CEST 2009


On Sat, Oct 3, 2009 at 4:41 PM, Steven Bethard <steven.bethard at gmail.com> wrote:

I thought it might be useful for those who don't have time to read a million posts to have a summary of what's happened in the formatting discussion.

Definitely useful. Thanks for the summary!

[...]

* Add a parameter which declares the type of format string::  logging.Formatter(fmt="{asctime} - {name}", format=BRACES)  The API code would then switch between %-format and {}-format  based on the value of that parameter. If %-formatting is to be  deprecated, this could be done by first deprecating  format=PERCENTS and requiring format=BRACES, and then changing the  default to format=BRACES.

+1.

* Create string subclasses which convert % use to .format calls::  __ = bracefmt_  logging.Formatter(fmt=("{asctime} - {name}"))_  The API code wouldn't have to change at all at first, as applying  % to bracefmt objects would call .format() instead. If  %-formatting is to be deprecated, this could be done by first  deprecating plain strings and requiring bracefmt strings, and  then allowing plain strings again but assuming they are {}-format  strings.

Uurgh. This just feels... icky. A badly-rationalized -1 from me.

* Teach the API to accept callables as well as strings::  logging.Formatter(fmt="{asctime} - {name}".format)  The API code would just call the object with .format() style  arguments if a callable was given instead of a string. If  %-formatting is to be deprecated, this could be done by first  deprecating plain strings and requiring callables, and then  allowing plain strings again but assuming they are {}-format  strings

+0.5. Seems like it could work, but the first solution feels cleaner.

* Create translators between %-format and {}-format::  assert tobraces("%(asctime)s") == "{asctime}"  assert topercents("{asctime}") == "%(asctime)s"  these could then either be used outside of the API::  logging.Formatter(fmt=topercents("{asctime} - {name}"))  or they could be used within the API combined with some sort of  heuristic for guessing whether a {}-format string or a %-format  string was passed in::  logging.Formatter(fmt="{asctime} - {name}")  If %-formatting is to be deprecated, the transition strategy here  is trivial. However, no one has yet written translators, and it is  not clear what heuristics should be used, e.g. should the method  just try %-formatting first and then {}-formatting if it fails?

I'm reserving judgement on this one until it becomes clear how feasible it is. Without having thought about it too hard, this sounds potentially tricky and bug-prone.

Mark



More information about the Python-Dev mailing list