Skip site navigation (1) Skip section navigation (2)

gmpy adapter

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: (view raw, whole thread or download thread mbox)
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)

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)

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'))


  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?




psycopg by date

Next:From: A.M.Date: 2011-02-28 18:49:38
Subject: Re: Using real libpq parameters
Previous:From: Daniele VarrazzoDate: 2011-02-27 23:35:43
Subject: Re: Using real libpq parameters

Privacy Policy | About PostgreSQL
Copyright © 1996-2017 The PostgreSQL Global Development Group