Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Bugs

[issue2802] str.format() :n integer output

 

 

Python bugs RSS feed   Index | Next | Previous | View Threaded


report at bugs

May 9, 2008, 7:08 AM

Post #1 of 9 (375 views)
Permalink
[issue2802] str.format() :n integer output

New submission from Mark Summerfield <mark [at] qtrac>:

In Py30a5 the 'n' format option is not v. useful for integers:

>>> for x in range(8):
print("{0:n} ".format(10**x), end="")

1 10 100 1,000 10,000 100,000 1e+06 1e+07

This is because it behaves like g once a number grows large. That makes
sense for floats, but since Python has unlimited size integers there is
currently no built-in way to get, 10**6 to output as 1,000,000 (or using
whatever the user's locale-dependent separator is). (It is easy to write
a suitable function for this, but it just seems that n is a bit of a
teaser in this regard.)

I think that n should stay the same for floats, but for integers should
never switch to g, but just use as many separators as needed.

----------
messages: 66471
nosy: mark
severity: normal
status: open
title: str.format() :n integer output
type: feature request
versions: Python 3.0

__________________________________
Tracker <report [at] bugs>
<http://bugs.python.org/issue2802>
__________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

May 9, 2008, 7:23 AM

Post #2 of 9 (352 views)
Permalink
[issue2802] str.format() :n integer output [In reply to]

Mark Dickinson <dickinsm [at] gmail> added the comment:

> I think that n should stay the same for floats, but for integers should
> never switch to g, but just use as many separators as needed.

I agree with this, in principle. It might be some work to implement,
though: for floats, Python gets to use the OS-supplied formatting
functions. Indeed, it looks as though all that happens here is that the
integer is converted to a float before formatting:

>>> print("{0:n} ".format(10**400), end="")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: Python int too large to convert to C double

For integers, we'd have to roll our own code. I had similar problems
trying to implement the 'n' format code for Decimal; in the end I just
gave up and left it unimplemented. Maybe using 'n' for an integer should
just raise an exception, for now?

Eric, what do you think?

----------
assignee: -> eric.smith
nosy: +eric.smith, marketdickinson
priority: -> normal

__________________________________
Tracker <report [at] bugs>
<http://bugs.python.org/issue2802>
__________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

May 9, 2008, 7:42 AM

Post #3 of 9 (351 views)
Permalink
[issue2802] str.format() :n integer output [In reply to]

Eric Smith <eric [at] trueblade> added the comment:

The reason for this is that 'n' is defined in PEP 3101 as being a float
format only, and the rule is that if an integer sees a float format, it
does a float conversion and then prints the float with the supplied format.

I'd be okay with adding 'n' as an integer format, with the loose
definition of "just like 'd', but adding thousands separators".

As to the implementation, the OS supplied float formatting does not add
thousands separators. I added the function add_thousands_grouping() to
Python/pystrtod.c in order implement this for floats. It would be easy
to make this same code work for integers (and in fact it might already
work, although there are probably memory allocation issues to deal with).

Maybe we should bring up modifying the PEP on python-dev or python-3000.

This issue exists in 2.6 as well.

----------
components: +Interpreter Core
versions: +Python 2.6

__________________________________
Tracker <report [at] bugs>
<http://bugs.python.org/issue2802>
__________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

May 9, 2008, 7:44 AM

Post #4 of 9 (352 views)
Permalink
[issue2802] str.format() :n integer output [In reply to]

Mark Summerfield <mark [at] qtrac> added the comment:

On 2008-05-09, Mark Dickinson wrote:
> Mark Dickinson <dickinsm [at] gmail> added the comment:
> > I think that n should stay the same for floats, but for integers should
> > never switch to g, but just use as many separators as needed.
>
> I agree with this, in principle. It might be some work to implement,
> though: for floats, Python gets to use the OS-supplied formatting
> functions. Indeed, it looks as though all that happens here is that the
>
> integer is converted to a float before formatting:
> >>> print("{0:n} ".format(10**400), end="")
>
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> OverflowError: Python int too large to convert to C double
>
> For integers, we'd have to roll our own code. I had similar problems
> trying to implement the 'n' format code for Decimal; in the end I just
> gave up and left it unimplemented. Maybe using 'n' for an integer should
> just raise an exception, for now?
>
> Eric, what do you think?

It isn't hard (in Python):

import locale
locale.setlocale(locale.LC_ALL, "")
separator = locale.localeconv()["thousands_sep"]

def n_format(integer, separator):
chars = []
for i, char in enumerate(reversed("{0:d}".format(integer))):
if i and not i % 3:
chars.insert(0, separator)
chars.insert(0, char)
return "".join(chars)

__________________________________
Tracker <report [at] bugs>
<http://bugs.python.org/issue2802>
__________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

May 9, 2008, 7:55 AM

Post #5 of 9 (352 views)
Permalink
[issue2802] str.format() :n integer output [In reply to]

Eric Smith <eric [at] trueblade> added the comment:

> It isn't hard (in Python):

<code deleted>

It's more complex, because the "3" is locale dependent, and is allowed
to be something like "3, then 2, then 3, then 1, repeating until the
start of the string".

See _group() in Lib/locale.py.

In any event, the code needs to be in C (sadly). But as I said in my
previous comment (which probably crossed paths with yours), this C code
already exists.

__________________________________
Tracker <report [at] bugs>
<http://bugs.python.org/issue2802>
__________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

May 9, 2008, 7:55 AM

Post #6 of 9 (351 views)
Permalink
[issue2802] str.format() :n integer output [In reply to]

Mark Summerfield <mark [at] qtrac> added the comment:

On 2008-05-09, Eric Smith wrote:
> Eric Smith <eric [at] trueblade> added the comment:
>
> The reason for this is that 'n' is defined in PEP 3101 as being a float
> format only, and the rule is that if an integer sees a float format, it
> does a float conversion and then prints the float with the supplied format.
>
> I'd be okay with adding 'n' as an integer format, with the loose
> definition of "just like 'd', but adding thousands separators".
>
> As to the implementation, the OS supplied float formatting does not add
> thousands separators. I added the function add_thousands_grouping() to
> Python/pystrtod.c in order implement this for floats. It would be easy
> to make this same code work for integers (and in fact it might already
> work, although there are probably memory allocation issues to deal with).
>
> Maybe we should bring up modifying the PEP on python-dev or python-3000.

I hope that you do:-)

> This issue exists in 2.6 as well.
>
> ----------
> components: +Interpreter Core
> versions: +Python 2.6
>
> __________________________________
> Tracker <report [at] bugs>
> <http://bugs.python.org/issue2802>
> __________________________________

__________________________________
Tracker <report [at] bugs>
<http://bugs.python.org/issue2802>
__________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

May 9, 2008, 8:12 AM

Post #7 of 9 (352 views)
Permalink
[issue2802] str.format() :n integer output [In reply to]

Mark Dickinson <dickinsm [at] gmail> added the comment:

> As to the implementation, the OS supplied float formatting does not add
> thousands separators. I added the function add_thousands_grouping() to
> Python/pystrtod.c in order implement this for floats.

Excellent! I didn't realise this code was already there. Maybe there's
also some way to use it to implement 'n' formatting for Decimal (which in
some ways behaves like a hybrid floating-point and integer type).

I can't think of any reason that the LC_NUMERIC stuff shouldn't apply to
integers as well as floats.

__________________________________
Tracker <report [at] bugs>
<http://bugs.python.org/issue2802>
__________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

May 11, 2008, 12:53 PM

Post #8 of 9 (329 views)
Permalink
[issue2802] str.format() :n integer output [In reply to]

Eric Smith <eric [at] trueblade> added the comment:

Implemented in 2.6 as r63078. I'll port this to py3k shortly.

__________________________________
Tracker <report [at] bugs>
<http://bugs.python.org/issue2802>
__________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

May 11, 2008, 2:17 PM

Post #9 of 9 (332 views)
Permalink
[issue2802] str.format() :n integer output [In reply to]

Eric Smith <eric [at] trueblade> added the comment:

Implemented in 3.0 as r63093. I'm closing this issue.

I added the C code that does the grouping insertion as
_PyString_InsertThousandsGrouping and _PyUnicode_InsertThousandsGrouping
(in 3.0). This might be useful to others, although the API is fairly
complicated.

Mark Dickinson: For Decimal, you can probably get what you need from
Lib/locale.py, although the function _group() is private.

----------
resolution: -> fixed
status: open -> closed

__________________________________
Tracker <report [at] bugs>
<http://bugs.python.org/issue2802>
__________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com

Python bugs RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.