Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Error messages from format()

35 views
Skip to first unread message

Colin J. Williams

unread,
Nov 13, 2012, 10:08:59 AM11/13/12
to
Is there some way to get more informative error messages from the
builtin format?

Most messages are such as:
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
ValueError: Invalid conversion specification

This example doesn't point to the first invalid case.

[Dbg]>>> format((25, 31),'{0^9o} a(1:9x}')
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
ValueError: Invalid conversion specification

Basically, I'm trying to make use of the format function with Python
3.2, but find little in the way of examples in the docs.

Colin W.

Steven D'Aprano

unread,
Nov 13, 2012, 1:38:25 PM11/13/12
to
On Tue, 13 Nov 2012 10:08:59 -0500, Colin J. Williams wrote:

> Is there some way to get more informative error messages from the
> builtin format?

Yes -- post a feature request on the Python bug tracker, then wait until
Python 3.4 comes out in about 16 months.

:(


> Most messages are such as:
> Traceback (most recent call last):
> File "<interactive input>", line 1, in <module>
> ValueError: Invalid conversion specification
>
> This example doesn't point to the first invalid case.

Better error messages would be valuable.


> [Dbg]>>> format((25, 31),'{0^9o} a(1:9x}')
> Traceback (most recent call last):
> File "<interactive input>", line 1, in <module>
> ValueError: Invalid conversion specification

I see at least three problems.

(1) The first brace substitution is missing the colon between the
argument selector "0" and the format spec "^9o": should be "{0:^9o}".

(2) The second format string has an opening round bracket instead of
brace: (1:9x}

(3) But you are confusing the str.format method with the format function.
The format function doesn't take brace substitutions!

The string format method takes a template including brace substitutions,
plus multiple "objects to be substituted", like this:

py> '{0:^9o} a{1:9x}'.format(25, 31)
' 31 a 1f'

In this case, the template '{0:^9o} a{1:9x}' requires two arguments since
it has two substitutions, {0} and {1}. Each substitution has a format
spec following the colon: {0:^9o} and {1:9x}

But the format function only takes a single "object to be substituted",
and so doesn't take a brace substitution. Instead, it just takes the
format spec part:


py> format(25, '^9o')
' 31 '
py> format(31, '^9o')
' 37 '

format will not split a tuple into multiple arguments for you, since the
tuple is considered a single object.



--
Steven

Colin J. Williams

unread,
Nov 13, 2012, 3:24:53 PM11/13/12
to
On 13/11/2012 1:38 PM, Steven D'Aprano wrote:
> On Tue, 13 Nov 2012 10:08:59 -0500, Colin J. Williams wrote:
>
>> Is there some way to get more informative error messages from the
>> builtin format?
>
> Yes -- post a feature request on the Python bug tracker, then wait until
> Python 3.4 comes out in about 16 months.
>
> :(
>
Many thanks :)

I am working on the assumption that the first argument of the format
builtin function and be a sequence of values, which can be selected
with {1:}, {2:}, {0:} etc.

The docs don't make this clear. I would appreciate advice.

Colin W.
> [snip]

Dave Angel

unread,
Nov 13, 2012, 4:18:07 PM11/13/12
to Colin J. Williams, pytho...@python.org
On 11/13/2012 03:24 PM, Colin J. Williams wrote:
> <SNIP>
>
> I am working on the assumption that the first argument of the format
> builtin function and be a sequence of values, which can be selected
> with {1:}, {2:}, {0:} etc.
>
> The docs don't make this clear. I would appreciate advice.
>

The built-in function format():

http://docs.python.org/3.3/library/functions.html?highlight=format%20builtin#format

The first parameter is a single object, NOT a sequence. One object, one
format. If you want more generality, use the str.format() method:

http://docs.python.org/3.3/library/stdtypes.html?highlight=format#str.format

where you can supply a list or a dictionary of multiple items to be
formatted into a single string. That's the one where you supply the
curly braces.


--

DaveA

Steven D'Aprano

unread,
Nov 13, 2012, 5:54:46 PM11/13/12
to
On Tue, 13 Nov 2012 15:24:53 -0500, Colin J. Williams wrote:

> On 13/11/2012 1:38 PM, Steven D'Aprano wrote:
>> On Tue, 13 Nov 2012 10:08:59 -0500, Colin J. Williams wrote:
>>
>>> Is there some way to get more informative error messages from the
>>> builtin format?
>>
>> Yes -- post a feature request on the Python bug tracker, then wait
>> until Python 3.4 comes out in about 16 months.
>>
>> :(
>>
> Many thanks :)
>
> I am working on the assumption that the first argument of the format
> builtin function and be a sequence of values, which can be selected with
> {1:}, {2:}, {0:} etc.

Um, did you read the rest of my post? I already told you that this is
incorrect.



--
Steven
0 new messages