Re: Can't register an adapter for an "old style" python class

Поиск
Список
Период
Сортировка
От Daniele Varrazzo
Тема Re: Can't register an adapter for an "old style" python class
Дата
Msg-id CA+mi_8a4AVSdCmdkv5_xb-2xSt+JXz0U+86oRJyP6fT6kP2GNQ@mail.gmail.com
обсуждение исходный текст
Ответ на Can't register an adapter for an "old style" python class  (Ghislain LEVEQUE <ghislain.leveque@clarisys.fr>)
Ответы Re: Can't register an adapter for an "old style" python class  (Ghislain LEVEQUE <ghislain.leveque@clarisys.fr>)
Список psycopg
On Wed, Nov 2, 2011 at 10:56 AM, Ghislain LEVEQUE
<ghislain.leveque@clarisys.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

В списке psycopg по дате отправления:

Предыдущее
От: Ghislain LEVEQUE
Дата:
Сообщение: Can't register an adapter for an "old style" python class
Следующее
От: Ghislain LEVEQUE
Дата:
Сообщение: Re: Can't register an adapter for an "old style" python class