[Python-ideas] Added a function to parse str.format() mini-language specifiers

Mark Dickinson dickinsm at gmail.com
Mon Mar 16 17:26:10 CET 2009


On Mon, Mar 16, 2009 at 2:23 PM, Eric Smith <eric at trueblade.com> wrote:
> I'd like to add a function (or method) to parse str.format()'s standard
> mini-language format specifiers.

+1 from me.  (Of course. :-)  Once the 'n' format code goes in, decimal.py
will contain over 200 lines of Python code that really has very little to
do with the decimal module at all.  I'd like to see that code move
somewhere else, partly out of a desire to unclutter the decimal module,
and partly to make it easier to cope with changes and new features
in the formatting mini-language.

Out of curiosity, does anyone know of any numeric types (other
than Decimal) that might benefit from this?

Something like the '_format_align' function from decimal.py
might also be of general use:  it just does the job of padding
and aligning a numeric string (as well as dealing with the sign).

> be exposed to Python code. I propose a function that takes a single str (or
> unicode) and returns a named tuple with the appropriate values filled in.

Are there advantages to using a named tuple instead of a dict?
If there's a possibility that some fields may or may not be
defined depending on the value of other fields, then a dict
may make more sense.  (Not sure whether this can happen
with the mini-language in its current form.)

> So, is such a function desirable, and if so, where would it go?

Yes, and don't know!

> It could be a method on str and unicode, but I'm not sure that's most
> appropriate.

Doesn't seem right to me, either.

Mark



More information about the Python-ideas mailing list