def _collect_update_commands(
        uowtransaction, table, states_to_update,
        bulk=False):

    for state, state_dict, mapper, connection, \
            update_version_id in states_to_update:

        if table not in mapper._pks_by_table:
            continue

        pks = mapper._pks_by_table[table]

        value_params = {}

        propkey_to_col = mapper._propkey_to_col[table]

        if bulk:
            params = dict(
                (propkey_to_col[propkey].key, state_dict[propkey])
                for propkey in
                set(propkey_to_col).intersection(state_dict).difference(
                    mapper._pk_keys_by_table[table])
            )
            has_all_defaults = True
        else:
            params = {}
            for propkey in set(propkey_to_col).intersection(
                    state.committed_state):
                value = state_dict[propkey]
                col = propkey_to_col[propkey]

                if isinstance(value, sql.ClauseElement):
                    value_params[col] = value
                # guard against values that generate non-__nonzero__
                # objects for __eq__()
                elif state.manager[propkey].impl.is_equal(
                        value, state.committed_state[propkey]) is not True:
                    params[col.key] = value

            if mapper.base_mapper.eager_defaults:
                has_all_defaults = mapper._server_onupdate_default_cols[table].\
                    issubset(params)
            else:
                has_all_defaults = True

        if update_version_id is not None and \
                mapper.version_id_col in mapper._cols_by_table[table]:

            if not bulk and not (params or value_params):
                # HACK: check for history in other tables, in case the
                # history is only in a different table than the one
                # where the version_id_col is.  This logic was lost
                # from 0.9 -> 1.0.0 and restored in 1.0.6.
                for prop in mapper._columntoproperty.values():
                    history = (
                        state.manager[prop.key].impl.get_history(
                            state, state_dict,
                            attributes.PASSIVE_NO_INITIALIZE))
                    if history.added:
                        break
                else:
                    # no net change, break
                    continue

            col = mapper.version_id_col
            params[col._label] = update_version_id

            if (bulk or col.key not in params) and \
                    mapper.version_id_generator is not False:
                val = mapper.version_id_generator(update_version_id)
                params[col.key] = val

        elif not (params or value_params):
            continue

        if bulk:
            pk_params = dict(
                (propkey_to_col[propkey]._label, state_dict.get(propkey))
                for propkey in
                set(propkey_to_col).
                intersection(mapper._pk_keys_by_table[table])
            )
        else:
            pk_params = {}
            for col in pks:
                propkey = mapper._columntoproperty[col].key

                history = state.manager[propkey].impl.get_history(
                    state, state_dict, attributes.PASSIVE_OFF)

                if history.added:
                    if not history.deleted or \
                            ("pk_cascaded", state, col) in \
                            uowtransaction.attributes:
                        pk_params[col._label] = history.added[0]
                        params.pop(col.key, None)
                    else:
                        # else, use the old value to locate the row
                        pk_params[col._label] = history.deleted[0]
                        params[col.key] = history.added[0]
                else:
                    pk_params[col._label] = history.unchanged[0]
                if pk_params[col._label] is None:
                    raise orm_exc.FlushError(
                        "Can't update table %s using NULL for primary "
                        "key value on column %s" % (table, col))

        if params or value_params:
            params.update(pk_params)
            yield (
                state, state_dict, params, mapper,
                connection, value_params, has_all_defaults)
Add a new optimizer to the list of optimizer
dataset/ETHPy150Open zzzeek/sqlalchemy/lib/sqlalchemy/orm/persistence.py/_collect_update_commands--crossover with--dataset/ETHPy150Open koenbok/Cactus/cactus/static/external/manager.py/ExternalManager.register_optimizer