Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #33244 > unrolled thread
| Started by | "Colin J. Williams" <cjw@ncf.ca> |
|---|---|
| First post | 2012-11-13 10:08 -0500 |
| Last post | 2012-11-13 22:54 +0000 |
| Articles | 5 — 3 participants |
Back to article view | Back to comp.lang.python
Error messages from format() "Colin J. Williams" <cjw@ncf.ca> - 2012-11-13 10:08 -0500
Re: Error messages from format() Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-13 18:38 +0000
Re: Error messages from format() "Colin J. Williams" <cjw@ncf.ca> - 2012-11-13 15:24 -0500
Re: Error messages from format() Dave Angel <d@davea.name> - 2012-11-13 16:18 -0500
Re: Error messages from format() Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-13 22:54 +0000
| From | "Colin J. Williams" <cjw@ncf.ca> |
|---|---|
| Date | 2012-11-13 10:08 -0500 |
| Subject | Error messages from format() |
| Message-ID | <k7tnqp$4r2$1@theodyn.ncf.ca> |
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.
[toc] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2012-11-13 18:38 +0000 |
| Message-ID | <50a293a0$0$29999$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #33244 |
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
[toc] | [prev] | [next] | [standalone]
| From | "Colin J. Williams" <cjw@ncf.ca> |
|---|---|
| Date | 2012-11-13 15:24 -0500 |
| Message-ID | <k7uaak$mo4$1@theodyn.ncf.ca> |
| In reply to | #33257 |
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]
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <d@davea.name> |
|---|---|
| Date | 2012-11-13 16:18 -0500 |
| Message-ID | <mailman.3645.1352841512.27098.python-list@python.org> |
| In reply to | #33262 |
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
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2012-11-13 22:54 +0000 |
| Message-ID | <50a2cfb6$0$29999$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #33262 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web