Re: flexi adaption/casting scheme

From: Tobias Oberstein <tobias(dot)oberstein(at)gmail(dot)com>
To: Daniele Varrazzo <daniele(dot)varrazzo(at)gmail(dot)com>
Cc: psycopg(at)postgresql(dot)org
Subject: Re: flexi adaption/casting scheme
Date: 2012-09-21 14:14:54
Message-ID: 505C765E.8060308@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: psycopg

Hi Daniel,

thanks for your hints .. I have learned some things!

>> Above should work with nested PG types (array of composite type with
>> an attribute again composite type etc etc).
>
> The composite caster already deals correctly with nested types: the
> change to make it return dicts instead of tuples should be a few lines
> of code.

Apparently I failed to articulate what I try to do: yes, adjusting
the CompositeCaster to _return_ dicts instead of tuples is a snap
replacing

return self._ctor(*attrs)

with

for i in xrange(len(self.atttypes)):
if attrs[i] is not None:
o[self.attnames[i]] = attrs[i]
return o

in CompositeCaster.parse.

(sidenote: somewhat inconvenient is the fact that I need to duplicate
the code .. cannot derive from the class, since the
CompositeCaster._from_db class method will always construct a
CompositeCaster, not my derived class).

What I am struggling with is the _opposite_ direction: have Python
dicts automatically adapt to composite types (when knowing the
respective target type).

In the meantime I have gotten quite close:

https://github.com/oberstet/scratchbox/blob/master/python/psycopg2/test6.py

Nested arrays of composite types still don't work ..

Cheers,
Tobias

In response to

Browse psycopg by date

  From Date Subject
Next Message Tobias Oberstein 2012-09-21 15:13:38 Re: flexi adaption/casting scheme
Previous Message Ronan Dunklau 2012-09-21 13:47:48 Re: flexi adaption/casting scheme