def backref_listeners(attribute, key, uselist):

    # use easily recognizable names for stack traces

    parent_token = attribute.impl.parent_token
    parent_impl = attribute.impl

    def _acceptable_key_err(child_state, initiator, child_impl):
        raise ValueError(
            "Bidirectional attribute conflict detected: "
            'Passing object %s to attribute "%s" '
            'triggers a modify event on attribute "%s" '
            'via the backref "%s".' % (
                state_str(child_state),
                initiator.parent_token,
                child_impl.parent_token,
                attribute.impl.parent_token
            )
        )

    def emit_backref_from_scalar_set_event(state, child, oldchild, initiator):
        if oldchild is child:
            return child
        if oldchild is not None and \
                oldchild is not PASSIVE_NO_RESULT and \
                oldchild is not NEVER_SET:
            # With lazy=None, there's no guarantee that the full collection is
            # present when updating via a backref.
            old_state, old_dict = instance_state(oldchild),\
                instance_dict(oldchild)
            impl = old_state.manager[key].impl

            if initiator.impl is not impl or \
                    initiator.op not in (OP_REPLACE, OP_REMOVE):
                impl.pop(old_state,
                         old_dict,
                         state.obj(),
                         parent_impl._append_token,
                         passive=PASSIVE_NO_FETCH)

        if child is not None:
            child_state, child_dict = instance_state(child),\
                instance_dict(child)
            child_impl = child_state.manager[key].impl
            if initiator.parent_token is not parent_token and \
                    initiator.parent_token is not child_impl.parent_token:
                _acceptable_key_err(state, initiator, child_impl)
            elif initiator.impl is not child_impl or \
                    initiator.op not in (OP_APPEND, OP_REPLACE):
                child_impl.append(
                    child_state,
                    child_dict,
                    state.obj(),
                    initiator,
                    passive=PASSIVE_NO_FETCH)
        return child

    def emit_backref_from_collection_append_event(state, child, initiator):
        if child is None:
            return

        child_state, child_dict = instance_state(child), \
            instance_dict(child)
        child_impl = child_state.manager[key].impl

        if initiator.parent_token is not parent_token and \
                initiator.parent_token is not child_impl.parent_token:
            _acceptable_key_err(state, initiator, child_impl)
        elif initiator.impl is not child_impl or \
                initiator.op not in (OP_APPEND, OP_REPLACE):
            child_impl.append(
                child_state,
                child_dict,
                state.obj(),
                initiator,
                passive=PASSIVE_NO_FETCH)
        return child

    def emit_backref_from_collection_remove_event(state, child, initiator):
        if child is not None:
            child_state, child_dict = instance_state(child),\
                instance_dict(child)
            child_impl = child_state.manager[key].impl
            if initiator.impl is not child_impl or \
                    initiator.op not in (OP_REMOVE, OP_REPLACE):
                child_impl.pop(
                    child_state,
                    child_dict,
                    state.obj(),
                    initiator,
                    passive=PASSIVE_NO_FETCH)

    if uselist:
        event.listen(attribute, "append",
                     emit_backref_from_collection_append_event,
                     retval=True, raw=True)
    else:
        event.listen(attribute, "set",
                     emit_backref_from_scalar_set_event,
                     retval=True, raw=True)
    # TODO: need coverage in test/orm/ of remove event
    event.listen(attribute, "remove",
                 emit_backref_from_collection_remove_event,
                 retval=True, raw=True)
Checks whether matrix is hermitian
dataset/ETHPy150Open RoseOu/flasky/venv/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py/backref_listeners--crossover with--dataset/ETHPy150Open sympy/sympy/sympy/matrices/densetools.py/isHermitian