rowfactory

Thomas Heller <[email protected]>
Newsgroups gmane.comp.python.db.pysqlite.user
Message-ID <[email protected]>
It would be nice if the rows returned by fetchall() calls
would implement __cmp__ and __hash__ so that they can be compared
and put into dictionaries and sets.

For now, I have implemented this myself as a subclass; maybe
it should be considered to implement these methods in the sqlite
extension.

Thanks,
Thomas

from pysqlite2 import dbapi2 as sqlite3

class MyRow(sqlite3.Row):
    # A row-factory for sqlite that implements __cmp__ and __hash__
    # correctly.
    def __hash__(self):
        return hash(tuple(self)) ^ hash(tuple(self.keys()))

    def __cmp__(self, other):
        if not isinstance(other, type(self)):
            return -1
        if self.keys() != other.keys():
            return -1
        return cmp(tuple(self), tuple(other))

    def __repr__(self):
        return repr(list(self))
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.