Re: INET type and arrays: too much casting?
Stephane Bortzmeyer <[email protected]>
| Newsgroups | gmane.comp.python.db.psycopg.devel |
|---|---|
| Organization | NIC France |
| Message-ID | <[email protected]> |
On Tue, Sep 16, 2008 at 10:45:16AM +0200, Federico Di Gregorio <fog-NGVKUo/i/[email protected]> wrote a message of 86 lines which said: > The right way would be to provide an adapter for a scalar INET type > (mapped to an Inet class in Python) Here is what I did and which seems to work: from psycopg2.extensions import adapt, register_adapter, AsIs class Inet(object): def __init__(self, addr): """ Never call it with unchecked data, there is no protection against injection """ # TODO: check it is an IP address self.addr = addr def adapt_inet(address): return AsIs("'%s'::inet" % address.addr) register_adapter(Inet, adapt_inet) # Then, use it that way (data come from DNS Python, # http://www.dnspython.org/ ) : aaaa_www_zone = [Inet(a.address) for a in self.aaaa_www_zone]