def _iter_wrap_rv(self, rv):
    from olo.model import ModelMeta
    entity_count = len(self._entities)
    raw = self._raw
    producers = []
    idx = (- 1)

    def make_field_producer(idx, v):

        def producer(item):
            if raw:
                return item[idx]
            model = v.get_model()
            attrs = model._parse_attrs({v.attr_name: item[idx]})
            return attrs[v.attr_name]
        return producer
    for v in self._entities:
        idx += 1
        if isinstance(v, ModelMeta):
            fields_count = len(v.__fields__)
            producers.append((lambda idx, v: (lambda item: v._olo_instantiate(**dict(izip(v.__sorted_fields__, item[idx:(idx + fields_count)])))))(idx, v))
            idx += (fields_count - 1)
            continue
        if isinstance(v, Field):
            producers.append(make_field_producer(idx, v))
            continue
        producers.append((lambda idx, v: (lambda item: item[idx]))(idx, v))
    session = QuerySession()
    seen = set()
    for (idx, item) in enumerate(rv):
        new_item = tuple(imap((lambda f: f(item)), producers))
        new_item = new_item[:entity_count]
        if (entity_count == 1):
            new_item = new_item[0]
            if isinstance(self._entities[0], DISTINCT):
                if (new_item in seen):
                    continue
                seen.add(new_item)
        session.add_entity(new_item)
    for entity in session.entities:
        (yield entity)
def _iter_wrap_rv(self, rv):
    from olo.model import ModelMeta
    entity_count = len(self._entities)
    raw = self._raw
    producers = []
    idx = (- 1)

    def make_field_producer(idx, v):

        def producer(item):
            if raw:
                return item[idx]
            model = v.get_model()
            attrs = model._parse_attrs({v.attr_name: item[idx]})
            return attrs[v.attr_name]
        return producer
    for v in self._entities:
        idx += 1
        if isinstance(v, ModelMeta):
            fields_count = len(v.__fields__)
            producers.append((lambda idx, v: (lambda item: v._olo_instantiate(**dict(izip(v.__sorted_fields__, item[idx:(idx + fields_count)])))))(idx, v))
            idx += (fields_count - 1)
            continue
        if isinstance(v, Field):
            producers.append(make_field_producer(idx, v))
            continue
        producers.append((lambda idx, v: (lambda item: item[idx]))(idx, v))
    session = QuerySession()
    seen = set()
    for (idx, item) in enumerate(rv):
        new_item = tuple(imap((lambda f: f(item)), producers))
        new_item = new_item[:entity_count]
        if (entity_count == 1):
            new_item = new_item[0]
            if isinstance(self._entities[0], DISTINCT):
                if (new_item in seen):
                    continue
                seen.add(new_item)
        session.add_entity(new_item)
    for entity in session.entities:
        (yield entity)