I need to register an adapter for the dateutil's relativedelta class.
Here is what I did :
In [1]: import psycopg2
In [2]: psycopg2.__version__
Out[2]: '2.4.2 (dt dec pq3 ext)'
In [3]: import dateutil
In [4]: dateutil.__version__
Out[4]: '1.5'
In [5]: from psycopg2.extensions import adapt, adapters,
register_adapter, AsIs
In [6]: from dateutil.relativedelta import relativedelta
In [7]: register_adapter(relativedelta, lambda v: AsIs(adapt(str(v))))
In [8]: relativedelta in [k[0] for k in adapters.keys()]
Out[8]: True
In [9]: adapt(relativedelta(years=+1))
---------------------------------------------------------------------------
ProgrammingError Traceback (most recent call last)
/home/gl/src/MCA3/git2/<ipython console> in <module>()
ProgrammingError: can't adapt type 'instance'
I guess psycopg2 uses type instead of isinstance and this does not work
with old-style python class :
In [10]: [(isinstance(r, typ), type(r) == typ) for (typ, a), b in
....: adapters.items()]
Out[10]:
[(True, False),
(False, False),
(False, False),
(False, False),
[... SNIP ... ]
What is the "right" way to do this ?
--
Ghislain LEVEQUE - Clarisys Informatique
http://www.clarisys.fr - 09 72 11 43 60