[Python-3000] More PEP 3101 changes incoming

Ron Adam rrr at ronadam.com
Fri Aug 3 22:37:36 CEST 2007



Talin wrote:
 > Ron Adam wrote:
 >> After a fair amount of experimenting today, I think I've found a nice
 >> middle ground that meets some of what both you and Guido are looking
 >> for. (And a bit my own preference too.)
 >
 > First off, thank you very much for taking the time to think about this
 > in such detail. There are a lot of good ideas here.

Thanks, I use string operations a *lot* and I really do want it to work as 
easy as possible in a wide variety of situations.


 > What's missing, however, is a description of how all of this interacts
 > with the __format__ hook. The problem we are facing right now is
 > sometimes we want to override the __format__ hook and sometimes we
 > don't. Right now, the model that we want seems to be:
 >
 >   1) High precedence type coercion, i.e. 'r', which bypasses __format__.

I think you are looking for an internal simplicity which isn't needed, and 
most people won't even think about.

The exposed interface doesn't have any ambiguities if 'r' is a format 
specification just like 's', 'd', or 'f'.  These are what the formatter 
will dispatch on.  I think a few if/else's to catch types that will call 
__repr__ and __str__, instead of __format__ aren't that costly.  I think 
there are other areas that can be optimized more, and/or other points where 
we can hook into and modify the results.

Or am I missing something still?  Maybe if you give an example where it 
makes a difference it would help us sort it out.


 >   2) Check for __format__, and let it interpret the format specifier.
 >   3) Regular type coercion, i.e. 'd', 'f' and so on.
 >   4) Regular formatting based on type.



The sequence of parsing I have so far.


1. Split string into a dictionary of fields and a list of string parts.

     'Partnumber: {0:10},  Price: ${1:f.2}'.format('123abc', 99.95)

Results in...

      {'0':('', 10), '1':('f.2', '')}        # key:(format_spec, align_spec)

      ['Partnumber: ', '{0}', '  Price: $', '{1}']


2.  Apply the format_spec and then the alignment_spec to the arguments.

     {'0':'123abc    ', '1':'99.95'}

     * If the arguments are a sequence, they are enumerated to get keys.
     * If they are a dict, the existing keys are used.
     * Passing both *args and **kwds should also work.


3.  Replace the keys in the string list with the corresponding formatted 
dictionary values.

    ['Partnumber: ', '123abc    ', '  Price: $', '99.95']


4.  Join the string parts back together.

    'Partnumber: 123abc      Price: $99.95'


It may be useful to expose some of these intermediate steps so that we 
could pre-process the specifications, or post-process the formatted results 
before it gets merged back into the string.

Which seems to fit with some of your thoughts, although I think you are 
thinking more in the line of overriding methods instead of directly 
accessing the data.  A little bit of both could go a long ways.

Cheers,
    Ron


More information about the Python-3000 mailing list