Re: flexi adaption/casting scheme
| От | Tobias Oberstein |
|---|---|
| Тема | Re: flexi adaption/casting scheme |
| Дата | |
| Msg-id | 505C9EBC.7000006@gmail.com обсуждение исходный текст |
| Ответ на | Re: flexi adaption/casting scheme (Daniele Varrazzo <daniele.varrazzo@gmail.com>) |
| Ответы |
Re: flexi adaption/casting scheme
|
| Список | psycopg |
Hi Daniele,
> For example, to cast composite to dictionaries one can subclass it as:
>
> class DictComposite(psycopg2.extras.CompositeCaster):
> def make(self, attrs):
> return dict(zip(self.attnames, attrs))
>
This is nifty! I can then just slightly adjust above to filter for attrs
which are not None (only include those into the dict).
> Tobias: as expected it works ok with composite types, as the original
> CompositeCaster does:
>
> # from psql: CREATE TYPE card_back AS (face card, back text);
>
> c2 = DictComposite.from_db('card_back', cnn)
> c2.register()
>
> cur.execute("select ((8, 'hearts'), 'blue')::card_back")
> cur.fetchone()[0]
> {'back': 'blue', 'face': {'suit': 'hearts', 'value': 8}}
>
> if it doesn't work for you, you are probably doing something wrong.
I am struggling with _adaption_ (Python dict => PG composite), not
_typecasting_ (PG composite => dict).
My understanding would be that the following code should work with an
unmodified Psycopg, but it does not:
https://github.com/oberstet/scratchbox/blob/master/python/psycopg2/test7.py
What am I doing wrong?
Thanks!
Tobias
В списке psycopg по дате отправления: