[issue5448] Add precision property to decimal.Decimal
Daniel Lescohier
report at bugs.python.org
Mon Mar 9 17:13:42 CET 2009
Daniel Lescohier <daniel.lescohier at cbs.com> added the comment:
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...
----------
message_count: 2.0 -> 3.0
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue5448>
_______________________________________
More information about the Python-bugs-list
mailing list