| From: | Daniele Varrazzo <daniele(dot)varrazzo(at)gmail(dot)com> |
|---|---|
| To: | Ghislain LEVEQUE <ghislain(dot)leveque(at)clarisys(dot)fr> |
| Cc: | psycopg(at)postgresql(dot)org |
| Subject: | Re: Can't register an adapter for an "old style" python class |
| Date: | 2011-11-02 12:00:17 |
| Message-ID: | CA+mi_8a4AVSdCmdkv5_xb-2xSt+JXz0U+86oRJyP6fT6kP2GNQ@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | psycopg |
On Wed, Nov 2, 2011 at 10:56 AM, Ghislain LEVEQUE
<ghislain(dot)leveque(at)clarisys(dot)fr> wrote:
> I need to register an adapter for the dateutil's relativedelta class.
> [...]
> I guess psycopg2 uses type instead of isinstance and this does not work with
> old-style python class :
Yes, as far as I can see it has always worked this way: roughly there
is a mapping type -> adapter in psycopg2.extensions.adapters and the
adapter is looked up by adapters[type(obj)]. If we had to use
isinstance instead, it would take a linear scan of the adapters map
for every adapted object. So It seems psycopg has worked with new
style classes since they were really new, and never bothered with
old-style ones.
> What is the "right" way to do this ?
There are several sub-optimal ways to do it, each one flawed in some
way. You could create an adapter for type(relativedelta()), being
"instance" but you can do this only for one old-style class in the
entire runtime. You can also create a new-style object out of it by
"class MyRelativeDelta(relativedelta, object): pass", but I've checked
and every operation you make on it (e.g. delta1 + delta2) will create
a relativedelta instance.
I would say very simply copy the dateutil module into your project,
patch it to make a new style class it and use it: I think the license
(PSF) allows that. Also consider filing a request to the dateutil
author to make it a new-style class. Oh, it looks like you are a
contributor: <https://launchpad.net/dateutil>. Why don't you make it a
new-style class?
There is also this, cropped up in a google search, don't know what it
is: <http://pypi.python.org/pypi/psycopg2-dateutils/0.1>.
-- Daniele
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Ghislain LEVEQUE | 2011-11-02 13:28:44 | Re: Can't register an adapter for an "old style" python class |
| Previous Message | Ghislain LEVEQUE | 2011-11-02 10:56:18 | Can't register an adapter for an "old style" python class |