From: | Daniel Popowich <danielpopowich(at)gmail(dot)com> |
---|---|
To: | psycopg(at)postgresql(dot)org |
Subject: | gmpy adapter |
Date: | 2011-02-28 16:56:55 |
Message-ID: | 19819.54231.861653.438162@io.astro.umass.edu |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | psycopg |
Hello, all,
I have a client app that reads in many values from a numeric column,
does some heavy computations, then writes the results back to another
numeric column.
Python's decimal.Decimal is SLOOOOoooow. I'm trying to use gmpy.mpq
instead. I have the adapter for reading the values from the database
working fine:
numeric2mpq = lambda d,c: None if d is None else gmpy.mpq(d)
MPQ = psycopg2.extensions.new_type((1700,), "MPQ", numeric2mpq)
psycopg2.extensions.register_type(MPQ)
This is the adapter I'm using for the reverse (converting the mpq to a
string suitable for casting to numeric):
def mpq2numeric(mpq):
s = '%d::numeric/%d::numeric' % (mpq.numer(), mpq.denom())
return psycopg2.extensions.AsIs(s)
psycopg2.extensions.register_adapter(gmpy.mpq(0).__class__,
mpq2numeric)
While the adapter works, it seems less than optimal as it creates an
expression for the server to process, e.g:
print psycopg2.extensions.adapt(gmpy.mpq('.333'))
333::numeric/1000::numeric
Questions:
1) Is there something I'm overlooking with gmpy that could make this
much simpler?
2) What other solutions do folk use for working around pythons slow,
slow, slow Decimal?
Thanks,
Dan
From | Date | Subject | |
---|---|---|---|
Next Message | A.M. | 2011-02-28 18:49:38 | Re: Using real libpq parameters |
Previous Message | Daniele Varrazzo | 2011-02-27 23:35:43 | Re: Using real libpq parameters |