SelectResult bug

Issue #180 resolved
Former user created an issue

in some cases of use this mapper I need something like

t = Table('Z', engine,
    Column('id', Integer, primary_key=True),
    )
class Z(object):
    pass

assign_mapper(t, Z, extension=SelectResultsExt())

item = random.choice(Z.select())

Because choice implemented as {{{seq[* len(seq))](int(self.random())}}}. I need {{{len(Z.select())}}}. SelectResults has not {{{len}}} member, only {{{count}}} present. Simplest way to imlement this is

class SelectResults(object):
  ...
  __len__ = count

it is not preferred solution from performance eye but very easy to implement. I think you don't need attached patch for one.

Comments (1)

  1. Mike Bayer repo owner

    unfortunately, adding len to the SelectResults object has disastrous results, as when you call list() on the results to actually evaluate the object, it looks for len and calls it in order to speed up its work, which then hits the count() call unnecessarily and essentially performs all queries twice !

    so if you need the length without running the query, you have to call count. Or, if you want to convert to a list() first, then youll get the whole thing, i.e.:

       len(list(Z.select()))
    
  2. Log in to comment