gmpy adapter - Mailing list psycopg

From Daniel Popowich
Subject gmpy adapter
Date
Msg-id 19819.54231.861653.438162@io.astro.umass.edu
Whole thread Raw
Responses Re: gmpy adapter  (Daniele Varrazzo <daniele.varrazzo@gmail.com>)
List 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


psycopg by date:

Previous
From: Daniele Varrazzo
Date:
Subject: Re: Using real libpq parameters
Next
From: "A.M."
Date:
Subject: Re: Using real libpq parameters