Message169090
| Author |
zach.ware |
| Recipients |
Ramchandra Apte, cvrebert, eric.smith, joncle, mark.dickinson, rhettinger, skrah, terry.reedy, zach.ware |
| Date |
2012年08月24日.21:17:38 |
| SpamBayes Score |
-1.0 |
| Marked as misclassified |
Yes |
| Message-id |
<1345843060.06.0.644730903819.issue15136@psf.upfronthosting.co.za> |
| In-reply-to |
| Content |
(Mark:)
>To the patch: It looks fine, as far as it goes. It needs tests.
I remembered tests about 5 minutes after I submitted the initial patch :P. Here's a patch with some tests. Note that I've never really done tests, so please let me know if there need to be more/less/different tests.
>To avoid the repetition of the division code, I'd suggest doing something like:
>
> if context is None:
> context = getcontext()
(Stefan:)
>If this goes in, I'd prefer that as_decimal() always uses a localcontext().
>As the patch stands, using as_decimal() pollutes the global context flags,
>which can be quite unexpected
My first attempt was simply:
def as_decimal(self, context=None):
with localcontext(context):
return Decimal(self.numerator) / Decimal(self.denominator)
Looking through decimal.py, it looks like that should work (and in fact it does in 3.2.3), but it seems that _decimal will only accept a context as the argument to localcontext(), not None. Is that expected, or does that need an issue?
I didn't catch that the patch pollutes the global context flags, that is indeed ugly and unacceptable.
>If the function takes a context argument, it might be better to move it
>into the decimal module as Decimal.from_fraction(context).
Perhaps both? Implement Decimal.from_fraction(f, context) to do the real work, and then Fraction.to_decimal() as:
def to_decimal(self):
return Decimal.from_fraction(self, None)
Those who really care about the context (Decimal users working with a Fraction) can get what they want, and those who don't (Fraction users who want to see a Decimal representation) don't have to bother, and fractions.py isn't polluted with stuff about contexts. |
|