Message62916
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__. |
|
Date |
User |
Action |
Args |
2008-02-24 16:40:27 | mark.dickinson | set | spambayes_score: 0.00223571 -> 0.0022357076 recipients:
+ mark.dickinson, rhettinger, facundobatista, eric.smith |
2008-02-24 16:40:27 | mark.dickinson | set | spambayes_score: 0.00223571 -> 0.00223571 messageid: <1203871227.19.0.215972136099.issue2110@psf.upfronthosting.co.za> |
2008-02-24 16:40:26 | mark.dickinson | link | issue2110 messages |
2008-02-24 16:40:25 | mark.dickinson | create | |
|