classification
Title: Implement __format__ for Decimal
Type: behavior Stage: commit review
Components: Library (Lib) Versions: Python 3.1, Python 2.7
process
Status: closed Resolution: accepted
Dependencies: Superseder:
Assigned To: mark.dickinson Nosy List: eric.smith, facundobatista, mark.dickinson, rhettinger
Priority: critical Keywords: patch

Created on 2008-02-14 13:04 by facundobatista, last changed 2009-03-17 23:16 by mark.dickinson. This issue is now closed.

Files
File name Uploaded Description Edit
decimal_format.patch mark.dickinson, 2008-02-24 16:40
decimal_n_format.patch mark.dickinson, 2009-03-13 21:47 Implement 'n' format specifier for Decimals
decimal_n_format2.patch mark.dickinson, 2009-03-17 21:17
Messages (13)
msg62389 - (view) Author: Facundo Batista (facundobatista) * (Python committer) Date: 2008-02-14 13:04
A remainder.
msg62418 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2008-02-15 00:05
I can take a look at this if you like.  But I don't want to spoil your fun 
:)
msg62461 - (view) Author: Facundo Batista (facundobatista) * (Python committer) Date: 2008-02-16 17:36
Please, be my guest!

Thanks!
msg62916 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2008-02-24 16:40
Here's a first attempt at Decimal.__format__; the patch is against the 
trunk, and should be forward ported as usual to 3.0, with obvious minor 
changes related to str/unicode.  It still needs some cleanup and some 
more tests, but I'm posting it now in the hope of getting some feedback.

I'm adding Raymond Hettinger and Eric Smith to the nosy list in case 
they want to comment.

Some points of interest:

 * I decided to make plain 'e', 'f' and 'g' formats (without an explicit 
precision) do no rounding:  they return a string that captures the exact 
value of the Decimal instance (though it can lose information about 
significant zeros).  So format(x, 'e') is basically a way to say 'give 
me str(x), but always include an exponent', and format(x, 'f') gives a 
way to print the value without ever including an exponent.  format(x, 
'g') is identical to str(x), except that it always uses the character 
'e' for an exponent (instead of using 'e' or 'E' depending on the 
context).

There are other possible options here (have a default precision;  use 
the precision from the context), but this one seemed to make most sense.  
I'd appreciate opinions.

 * the integer format specifiers are not supported.  After noticing that
the integer format specifiers aren't supported for floats either, I 
think this is the right thing to do.

 * the 'n' format specifier is not supported either;  it's supposed to 
use the current locale.  I can't find any easy way to do this---it seems 
as though the only real option is to manually fix the decimal point 
character, figure out where to place thousands separators, etc.  This 
would add quite a lot of not-really-Decimal-related code to decimal.py, 
and I'm reluctant to do that.  The right solution probably involves 
writing some support code and putting it somewhere else in the std. lib.

 * on the subject of not-really-Decimal-related code, it would be great
if the parse_format_specifier and format_align functions (near the end 
of decimal.py) could be moved somewhere else.  I'm planning to reuse 
these functions for Fraction.__format__.
msg63119 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2008-02-29 02:18
I've committed a reworked version of the patch in r61123.
msg68447 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2008-06-20 09:29
Reopening this;  I'd like to have a second go at implementing the 'n'
format specifier for the Decimal type.

See issue 2802 for hints about how to go about this.
msg83504 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2009-03-12 21:11
Adding support for the 'n' format specifier should be done before 3.1 goes 
out.
msg83537 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2009-03-13 21:47
Here's a patch to implement the 'n' format specifier for Decimals
(see also issue 5481).

Raymond, could you give this a sanity check?
msg83538 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2009-03-13 22:36
Sure, I will take a look.
msg83663 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2009-03-17 07:28
Mark, this looks fine.

Can you add support for PEP 378?
msg83697 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2009-03-17 21:17
New version of decimal_n_format.patch, with support for the thousands 
separator (PEP 378).  As discussed on python-dev, during zero-padding the 
patched code adds an extra '0' on the left to avoid a leading ',' if 
necessary.  For example:

>>> format(Decimal('123456'), '08,')
'0,123,456'

The Decimal.__format__ method (and support code) had to be fairly 
significantly reworked.  However, I'm reasonably confident that this code 
is correct:  a patch review would be welcome if anyone has the time;  
otherwise I'll commit this in a couple of days or so.
msg83699 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2009-03-17 21:23
The tests you submitted are reassuring.  I think you should go ahead and
commit this.
msg83710 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2009-03-17 23:16
Committed, r70439 and r70440.
History
Date User Action Args
2009-03-17 23:17:13mark.dickinsonlinkissue5481 superseder
2009-03-17 23:16:18mark.dickinsonsetstatus: open -> closed

messages: + msg83710
2009-03-17 21:23:32rhettingersetresolution: accepted
messages: + msg83699
2009-03-17 21:17:48mark.dickinsonsetfiles: + decimal_n_format2.patch
type: behavior
messages: + msg83697

stage: commit review
2009-03-17 07:28:47rhettingersetassignee: rhettinger -> mark.dickinson
messages: + msg83663
2009-03-13 22:36:38rhettingersetmessages: + msg83538
2009-03-13 21:47:54mark.dickinsonsetresolution: accepted -> (no value)
2009-03-13 21:47:40mark.dickinsonsetfiles: + decimal_n_format.patch

messages: + msg83537
assignee: mark.dickinson -> rhettinger
2009-03-12 21:11:32mark.dickinsonsetpriority: high -> critical

messages: + msg83504
versions: + Python 3.1, Python 2.7, - Python 2.6, Python 3.0
2008-12-05 21:49:58mark.dickinsonsetpriority: high
2008-06-20 12:44:02benjamin.petersonsetstatus: closed -> open
2008-06-20 09:29:31mark.dickinsonsetassignee: facundobatista -> mark.dickinson
messages: + msg68447
2008-02-29 02:18:57mark.dickinsonsetstatus: open -> closed
resolution: accepted
messages: + msg63119
2008-02-24 16:40:26mark.dickinsonsetkeywords: + patch
nosy: + rhettinger, eric.smith
messages: + msg62916
files: + decimal_format.patch
versions: + Python 2.6
2008-02-16 17:36:43facundobatistasetmessages: + msg62461
2008-02-15 00:05:13mark.dickinsonsetnosy: + mark.dickinson
messages: + msg62418
2008-02-14 13:04:14facundobatistacreate