human
Can you provide a the Python code you requested given the following:
Complete the below
def instances(query, cursor, context):

    ''''Return an ORM result as an iterator.'
    '''
assistant
#Here is the Python code you requested.
    session = query.session
    context.runid = _new_runid()
    filter_fns = [ent.filter_fn for ent in query._entities]
    filtered = (id in filter_fns)
    single_entity = ((len(query._entities) == 1) and query._entities[0].supports_single_entity)
    if filtered:
        if single_entity:
            filter_fn = id
        else:
            def filter_fn(row):
                return tuple((fn(x) for (x, fn) in zip(row, filter_fns)))
    custom_rows = (single_entity and query._entities[0].custom_rows)
    (process, labels) = list(zip(*[query_entity.row_processor(query, context, custom_rows) for query_entity in query._entities]))
    while True:
        context.progress = {}
        context.partials = {}
        if query._yield_per:
            fetch = cursor.fetchmany(query._yield_per)
            if (not fetch):
                break
        else:
            fetch = cursor.fetchall()
        if custom_rows:
            rows = []
            for row in fetch:
                process[0](row, rows)
        elif single_entity:
            rows = [process[0](row, None) for row in fetch]
        else:
            rows = [util.KeyedTuple([proc(row, None) for proc in process], labels) for row in fetch]
        if filtered:
            rows = util.unique_list(rows, filter_fn)
        if (context.refresh_state and query._only_load_props and (context.refresh_state in context.progress)):
            context.refresh_state._commit(context.refresh_state.dict, query._only_load_props)
            context.progress.pop(context.refresh_state)
        statelib.InstanceState._commit_all_states(list(context.progress.items()), session.identity_map)
        for (state, (dict_, attrs)) in context.partials.items():
            state._commit(dict_, attrs)
        for row in rows:
            (yield row)
        if (not query._yield_per):
            break