[DB-SIG] Accepted method for handling decimals?

M.-A. Lemburg mal at egenix.com
Mon Nov 28 22:59:40 CET 2005


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.

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.

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Nov 28 2005)
>>> Python/Zope Consulting and Support ...        http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ...             http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________

::: Try mxODBC.Zope.DA for Windows,Linux,Solaris,FreeBSD for free ! ::::


More information about the DB-SIG mailing list