def get_mapping(session, table, candidates, generator, key_map):
for cand in candidates:<EOL><INDENT>try:<EOL><INDENT>temp = session.query(table).filter(table.candidate_id == cand.id).one()<EOL>cand_map = dict(zip(temp.keys, temp.values))<EOL><DEDENT>except NoResultFound:<EOL><INDENT>cand_map = {}<EOL><DEDENT>map_args = {"<STR_LIT>": cand.id}<EOL>for cid, key, value in generator(cand):<EOL><INDENT>if value == <NUM_LIT:0>:<EOL><INDENT>continue<EOL><DEDENT>cand_map[key] = value<EOL><DEDENT>map_args["<STR_LIT>"] = [*cand_map.keys()]<EOL>map_args["<STR_LIT>"] = [*cand_map.values()]<EOL>for key in map_args["<STR_LIT>"]:<EOL><INDENT>try:<EOL><INDENT>key_map[key].add(cand.__class__.__tablename__)<EOL><DEDENT>except KeyError:<EOL><INDENT>key_map[key] = {cand.__class__.__tablename__}<EOL><DEDENT><DEDENT>yield map_args<EOL><DEDENT>
Generate map of keys and values for the candidate from the generator.

    :param session: The database session.
    :param table: The table we will be inserting into (i.e. Feature or Label).
    :param candidates: The candidates to get mappings for.
    :param generator: A generator yielding (candidate_id, key, value) tuples.
    :param key_map: A mutable dict which values will be added to as {key:
        [relations]}.
    :type key_map: Dict
    :return: Generator of dictionaries of {"candidate_id": _, "keys": _, "values": _}
    :rtype: generator of dict