[DB-SIG] Accepted method for handling decimals?

Carsten Haese carsten at uniqsys.com
Tue Nov 29 03:34:45 CET 2005

On Mon, 28 Nov 2005 22:59:40 +0100, M.-A. Lemburg wrote
> Carsten Haese wrote:
> > Hi everybody:
> > 
> > I am planning to implement support for decimals in InformixDB
> > (http://informixdb.sourceforge.net) using the python decimal module if
> > it's available. My question is, should fetches simply return Decimal
> > instances instead of floats, or should there be some kind of
> > configuration setting to choose whether to return floats or Decimals? If
> > so, on what level should that setting reside, module, connection, or
> > cursor?
> It is usually a good idea to have such attributes on connections
> and cursors - cursors then inherit the setting from the connection
> at the time they are created.
> This allows the user to customize the behavior at a connection
> or cursor level, as needed.
> > I am leaning towards providing an optional argument to the Cursor()
> > constructor for turning on Decimal handling, and by default the cursor
> > would return floats. My reasoning here is that returning Decimals would
> > incur a (small) performance penalty that the user should only have to
> > pay if they need the precision that using Decimals gives them. As far as
> > the name of this optional argument, I'm considering "use_decimal", but
> > I'm open to suggestions.
> .cursor() normally doesn't take any arguments.

That may be true, but InformixDB already has optional arguments to cursor()
for providing a cursor name and to enable scroll cursors and cursors with
hold. Even mxODBC seems to allow an optional name argument to cursor() unless
the documentation you're linking to below is mistaken.

> I think the better way is to introduce settable attributes
> to connection and cursor objects, e.g. .decimalformat
> having values 'float' or 'decimal'.
> mxODBC uses the attributes .datetimeformat and .stringformat
> for such customizations:
> http://www.egenix.com/files/python/mxODBC.html#Cursors
> However, in general, I think that we should come up with
> an easy to use way to map database types to Python
> constructors. One which is flexible enough to set these
> mappings on a per-cursor and even per-column basis.

That would be ideal, but I'm not sure that my userbase -- which I estimate
cautiously according to the SourceForge statistics as "dozens of users" ;-) --
is ready for such flexibility.

For now I'm going with the optional use_decimal argument to the cursor, but
contrary to my original plan I'm going to default it to True.


Carsten Haese.

More information about the DB-SIG mailing list