Message168954
I came across this issue and thought it might be within my means to attempt a patch, so here's the first step towards one. I liked Terry's suggestion of implementing __format__ using .as_decimal, so that's what I'm working towards with this.
The first part is (or seems to be, I am pretty new at this and may well have missed something obvious :-)) pretty easy. The attached patch is pretty self-explanatory, I think. I figure if the user wants something specific from their fraction-as-a-decimal, they're probably working with other decimals already and will be able to supply a context. Otherwise, if they just want to see a decimal representation, they'll probably be fine with the default context and don't have to worry about it.
The __format__ part, though, seems kind of daunting. For one thing, Decimal's implementation looks pretty hairy and scary from my perspective; duplicating it for Fraction would be quite a feat. For another, the standard format spec doesn't seem to me to cleanly apply to fractional representations--what should be filled, how should alignment work, how would width be divided up, what exactly does precision mean?
I've had a thought, though; what if Fraction.__format__ simply looks for a 'D' at the end of format_spec? If it's found, just return format(self.as_decimal(), format_spec[:-1]. Otherwise, format using to-be-determined rules specific to fractions, including rules for, say, mixed fractions or denominator of a certain size.
Thoughts? |
|
Date |
User |
Action |
Args |
2012-08-23 18:12:00 | zach.ware | set | recipients:
+ zach.ware, rhettinger, terry.reedy, mark.dickinson, eric.smith, skrah, joncle, Ramchandra Apte |
2012-08-23 18:12:00 | zach.ware | set | messageid: <1345745520.63.0.880371492256.issue15136@psf.upfronthosting.co.za> |
2012-08-23 18:12:00 | zach.ware | link | issue15136 messages |
2012-08-23 18:11:59 | zach.ware | create | |
|