Error messages from format()

C

Colin J. Williams

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.
 
S

Steven D'Aprano

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.
 
C

Colin J. Williams

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.
 
D

Dave Angel

<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 builtin#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.
 
S

Steven D'Aprano

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top