Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Error messages from format()
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Colin J. Williams  
View profile  
 More options Nov 13 2012, 10:09 am
Newsgroups: comp.lang.python
From: "Colin J. Williams" <c...@ncf.ca>
Date: Tue, 13 Nov 2012 10:08:59 -0500
Local: Tues, Nov 13 2012 10:08 am
Subject: Error messages from format()
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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven D'Aprano  
View profile  
 More options Nov 13 2012, 1:38 pm
Newsgroups: comp.lang.python
From: Steven D'Aprano <steve+comp.lang.pyt...@pearwood.info>
Date: 13 Nov 2012 18:38:25 GMT
Local: Tues, Nov 13 2012 1:38 pm
Subject: Re: Error messages from format()

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Colin J. Williams  
View profile  
 More options Nov 13 2012, 3:24 pm
Newsgroups: comp.lang.python
From: "Colin J. Williams" <c...@ncf.ca>
Date: Tue, 13 Nov 2012 15:24:53 -0500
Local: Tues, Nov 13 2012 3:24 pm
Subject: Re: Error messages from format()
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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Dave Angel  
View profile  
 More options Nov 13 2012, 4:18 pm
Newsgroups: comp.lang.python
From: Dave Angel <d...@davea.name>
Date: Tue, 13 Nov 2012 16:18:07 -0500
Local: Tues, Nov 13 2012 4:18 pm
Subject: Re: Error messages from format()
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%20...

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

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


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Steven D'Aprano  
View profile  
 More options Nov 13 2012, 5:54 pm
Newsgroups: comp.lang.python
From: Steven D'Aprano <steve+comp.lang.pyt...@pearwood.info>
Date: 13 Nov 2012 22:54:46 GMT
Local: Tues, Nov 13 2012 5:54 pm
Subject: Re: Error messages from format()

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

--
Steven


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »