def get_mapping(session, table, candidates, generator, key_map):
    """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
    """
    for cand in candidates:
        # Grab the old values currently in the DB
        try:
            temp = session.query(table).filter(table.candidate_id == cand.id).one()
            cand_map = dict(zip(temp.keys, temp.values))
        except NoResultFound:
            cand_map = {}

        map_args = {"candidate_id": cand.id}
        for cid, key, value in generator(cand):
            if value == 0:
                continue
            cand_map[key] = value

        # Assemble label arguments
        map_args["keys"] = [*cand_map.keys()]
        map_args["values"] = [*cand_map.values()]

        # Update key_map by adding the candidate class for each key
        for key in map_args["keys"]:
            try:
                key_map[key].add(cand.__class__.__tablename__)
            except KeyError:
                key_map[key] = {cand.__class__.__tablename__}
        yield map_args
def get_mapping ( session , table , candidates , generator , key_map ) : for cand in candidates : try : temp = session . query ( table ) . filter ( table . candidate_id == cand . id ) . one ( ) cand_map = dict ( zip ( temp . keys , temp . values ) ) except NoResultFound : cand_map = { } map_args = { "candidate_id" : cand . id } for cid , key , value in generator ( cand ) : if value == 0 : continue cand_map [ key ] = value map_args [ "keys" ] = [ * cand_map . keys ( ) ] map_args [ "values" ] = [ * cand_map . values ( ) ] for key in map_args [ "keys" ] : try : key_map [ key ] . add ( cand . __class__ . __tablename__ ) except KeyError : key_map [ key ] = { cand . __class__ . __tablename__ } yield map_args
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