[Python-3000] More PEP 3101 changes incoming

Ron Adam rrr at ronadam.com
Sat Aug 4 18:30:24 CEST 2007



Ron Adam wrote:

> An alternative I thought of this morning is to reuse the alignment symbols 
> '^', '+', and '-' and require a minimum width if a maximum width is specified.

One more (or two) additions to this...

In the common cases of generating columnar reports, the min_width and 
max_width values would be equal.  So instead of repeating the numbers we 
could just prefix this case with double alignment symbols.

So instead of:

    '{0:+20+20}, {1:^100+100}, {2:-15+15}'

We could use:

    '{0:++20}, {1:^+100}, {2:-+15}'

Which would result in a first column that right aligns, a second column 
that centers unless the value is longer than 100, in which case it right 
align, and cuts the end, and a third column that left aligns, but cuts off 
the right if it's over 15.


One other feature might be to use the fill syntax form to specify an 
overflow replacement character...

    '{0:10+10/#}'.format('Python')                 ->  'Python    '

    '{0:10+10/#}'.format('To be, or not to be.')   ->  '##########'



Another way to think of the double alignment specification term, is that it 
moves slicing and preformatting of exceptional cases into the string format 
operation so we don't have to do the following just to catch the rare 
possibility of exceptional cases.  And it avoids altering the source data.

if len(value1)>max_width:
     value = value[:max_width]    # or [len(value)-max_with:]

Etc... for value2, value3 ...

line = format_string.format(value1, value2, value3, ...)


Cheers,
    Ron








More information about the Python-3000 mailing list