def state_logic(state):
    """
    State create/replace/reuse logic. The function name is not very good :(

        state      |  other_state  | same source  | what to reuse from other_state
    ---------------|---------------|--------------|-------------------------------
       replace     |    replace    |    True      |  uid, buffer, source
       replace     |    replace    |    False     |  uid, buffer
       replace     |   no-replace  |    True      |  source
       replace     |   no-replace  |    False     |  nothing
      no-replace   |    replace    |    True      |  uid, buffer, source
      no-replace   |    replace    |    False     |  uid, buffer
      no-replace   |   no-replace  |    True      |  source
      no-replace   |   no-replace  |    False     |  nothing
    """
    # Only consider states which are in the same container
    in_same_container = filter(lambda s: s.container == state.container, states.get_states())

    other = iterator.find(
        lambda s: s.source == state.source and s.options['replace'] == True,
        in_same_container
    )

    if other:
        states.remove(other, wipeout_buffer=False)
        state.uid = other.uid
        state.buff = other.buff
        state.source = other.source
        return state, other

    other = iterator.find(
        lambda s: s.source != state.source and s.options['replace'] == True,
        in_same_container,
    )

    if other:
        states.remove(other, wipeout_buffer=False)
        state.uid = other.uid
        state.buff = other.buff
        state.source.populate_candidates()
        ui.set_buffer_contents(state.buff, state.source.formatted_candidates())
        return state, other

    other = iterator.find(lambda s: s.source <= state.source, in_same_container)
    if other:
        state.source = other.source
    else:
        state.source.populate_candidates()
    return state, None
dataset/ETHPy150Open azure-satellite/pyunite/pyunite/core.py/state_logic/operator `==` -> `<=`