Use namedtuple._make in NamedTupleCursor and CompositeCaster

Makes things more natural as _make has the same signature of the tuple (see
_ctor in CompositeCaster) and is probably more efficient with less
intermediate sequences to build.
This commit is contained in:
Daniele Varrazzo 2012-09-20 16:27:50 +01:00
parent abe2df5f57
commit 526e270934

View File

@ -289,21 +289,21 @@ class NamedTupleCursor(_cursor):
nt = self.Record nt = self.Record
if nt is None: if nt is None:
nt = self.Record = self._make_nt() nt = self.Record = self._make_nt()
return nt(*t) return nt._make(t)
def fetchmany(self, size=None): def fetchmany(self, size=None):
ts = _cursor.fetchmany(self, size) ts = _cursor.fetchmany(self, size)
nt = self.Record nt = self.Record
if nt is None: if nt is None:
nt = self.Record = self._make_nt() nt = self.Record = self._make_nt()
return [nt(*t) for t in ts] return map(nt._make, ts)
def fetchall(self): def fetchall(self):
ts = _cursor.fetchall(self) ts = _cursor.fetchall(self)
nt = self.Record nt = self.Record
if nt is None: if nt is None:
nt = self.Record = self._make_nt() nt = self.Record = self._make_nt()
return [nt(*t) for t in ts] return map(nt._make, ts)
def __iter__(self): def __iter__(self):
it = _cursor.__iter__(self) it = _cursor.__iter__(self)
@ -313,10 +313,10 @@ class NamedTupleCursor(_cursor):
if nt is None: if nt is None:
nt = self.Record = self._make_nt() nt = self.Record = self._make_nt()
yield nt(*t) yield nt._make(t)
while 1: while 1:
yield nt(*it.next()) yield nt._make(it.next())
try: try:
from collections import namedtuple from collections import namedtuple
@ -854,9 +854,8 @@ class CompositeCaster(object):
"expecting %d components for the type %s, %d found instead" % "expecting %d components for the type %s, %d found instead" %
(len(self.atttypes), self.name, len(tokens))) (len(self.atttypes), self.name, len(tokens)))
attrs = [ curs.cast(oid, token) return self._ctor(curs.cast(oid, token)
for oid, token in zip(self.atttypes, tokens) ] for oid, token in zip(self.atttypes, tokens))
return self._ctor(*attrs)
_re_tokenize = regex.compile(r""" _re_tokenize = regex.compile(r"""
\(? ([,)]) # an empty token, representing NULL \(? ([,)]) # an empty token, representing NULL
@ -886,10 +885,10 @@ class CompositeCaster(object):
from collections import namedtuple from collections import namedtuple
except ImportError: except ImportError:
self.type = tuple self.type = tuple
self._ctor = lambda *args: tuple(args) self._ctor = self.type
else: else:
self.type = namedtuple(name, attnames) self.type = namedtuple(name, attnames)
self._ctor = self.type self._ctor = self.type._make
@classmethod @classmethod
def _from_db(self, name, conn_or_curs): def _from_db(self, name, conn_or_curs):