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

Steven Bethard steven.bethard at gmail.com
Wed Sep 30 06:35:10 CEST 2009


On Tue, Sep 29, 2009 at 8:15 PM, "Martin v. Löwis" <martin at v.loewis.de> wrote:

Steven Bethard wrote:

Consider an example from the logging docs:

logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") We'd like to support both this style as well as the following style: logging.Formatter() Now, that's different. IIUC, you are not actually performing the substitution here, but only at a later place. So changing to the new formatting mechanism is an API change. I agree that the new placeholder syntax needs to be supported.

Just to be clear, I don't think logging is the only place these kind of things happen. Some others I found looking around:

BaseHTTPServer.BaseHTTPRequestHandler.error_message_format http://docs.python.org/library/basehttpserver.html#BaseHTTPServer.BaseHTTPRequestHandler.error_message_format

BaseHTTPServer.BaseHTTPRequestHandler.log_message http://docs.python.org/3.1/library/http.server.html#http.server.BaseHTTPRequestHandler.log_message

email.generator.DecodedGenerator http://docs.python.org/library/email.generator.html#email.generator.DecodedGenerator

There may be more.

I would propose that the format argument gets an argument name, according to the syntax it is written in. For PEP 3101 format, I would call the argument "format" (like the method name of the string type), i.e.

logging.Formatter(  format="{asctime} - {name} - {levelname} - {message}") For the % formatting, I suggest "dicttemplate" (assuming that you have to use dictionary %(key)s style currently). The positional parameter would also mean dicttemplate, and would be deprecated (eventually requiring a keyword-only parameter).

This is a nice solution for the cases where we can be confident that the parameter is currently only used positionally. However, at least in Python 3.1, "fmt" is already documented as a keyword parameter:

http://docs.python.org/3.1/library/logging.html#logging.Formatter

I guess we could follow the same approach though, and have fmt= be the %-style formatting, and use some other keyword argument for {}-style formatting.

We've got a similar problem for the BaseHTTPRequestHandler.error_message_format attribute. I guess we'd want to introduce some other attribute which is the error message format for the {}-style formatting?

Steve

Where did you get that preposterous hypothesis? Did Steve tell you that? --- The Hiphopopotamus



More information about the Python-Dev mailing list