This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author dlesco
Recipients dlesco, rhettinger
Date 2009-03-09.16:13:39
SpamBayes Score 1.2163902e-08
Marked as misclassified No
Message-id <1236615222.26.0.135415804444.issue5448@psf.upfronthosting.co.za>
In-reply-to
Content
I had other code to check scale, but you are right, I should use 
quantize. There is certainly a lot to absorb in the IBM decimal 
specification.  I really appreciate you pointing me to quantize and 
Inexact. I guess I inadvertently used the issue tracker for help on 
the decimal module, I didn't really mean to do that, I really thought 
there was a need for Decimal.precision. The other unrelated issue I 
entered (#5445) should be more of a real issue.

My code constructs a conversion/validation closure for every field in 
the Schema, based on a SchemaField definition for each field. My 
SchemaFieldDecimal class includes precision and scale parameters, and 
now I'm going to add a rounding parameter, with None meaning raise an 
error on Inexact.

So pseudo-code for the fix:

scale = None if scale is None else Decimal((0,(1,),-scale))
traps = (InvalidOperation, Inexact) if rounding is None else 
(InvalidOperation,)
context = Context(prec=precision, rounding=rounding, traps=traps)

doing the conversion/validation:

For case if scale is not None:

try:
    with context:
        value = Decimal(value).quantize(scale)
except handlers...

For case if scale is None:

try:
   with context:
       value = Decimal(value)+Decimal(0) # will round or raise Inexact
except handlers...
History
Date User Action Args
2009-03-09 16:13:42dlescosetrecipients: + dlesco, rhettinger
2009-03-09 16:13:42dlescosetmessageid: <1236615222.26.0.135415804444.issue5448@psf.upfronthosting.co.za>
2009-03-09 16:13:41dlescolinkissue5448 messages
2009-03-09 16:13:39dlescocreate