From: | Tobias Oberstein <tobias(dot)oberstein(at)gmail(dot)com> |
---|---|
To: | Daniele Varrazzo <daniele(dot)varrazzo(at)gmail(dot)com> |
Cc: | Ronan Dunklau <rdunklau(at)gmail(dot)com>, psycopg(at)postgresql(dot)org |
Subject: | Re: flexi adaption/casting scheme |
Date: | 2012-09-21 17:07:08 |
Message-ID: | 505C9EBC.7000006@gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | 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
From | Date | Subject | |
---|---|---|---|
Next Message | Daniele Varrazzo | 2012-09-21 17:39:54 | Re: flexi adaption/casting scheme |
Previous Message | Ronan Dunklau | 2012-09-21 16:42:39 | Re: flexi adaption/casting scheme |