<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def set_state(self, state):
 """ Switches state of the TDS session. It also does state transitions checks. :param state: New state, one of TDS_PENDING/TDS_READING/TDS_IDLE/TDS_DEAD/TDS_QUERING """

        prior_state = self.state
        if state == prior_state:
            return state
        if state == tds_base.TDS_PENDING:
            if prior_state in (tds_base.TDS_READING, tds_base.TDS_QUERYING):
                self.state = tds_base.TDS_PENDING
            else:
                raise tds_base.InterfaceError('logic error: cannot chage query state from {0} to {1}'.
                                              format(tds_base.state_names[prior_state], tds_base.state_names[state]))
        elif state == tds_base.TDS_READING:
            # transition to READING are valid only from PENDING
            if self.state != tds_base.TDS_PENDING:
                raise tds_base.InterfaceError('logic error: cannot change query state from {0} to {1}'.
                                              format(tds_base.state_names[prior_state], tds_base.state_names[state]))
            else:
                self.state = state
        elif state == tds_base.TDS_IDLE:
            if prior_state == tds_base.TDS_DEAD:
                raise tds_base.InterfaceError('logic error: cannot change query state from {0} to {1}'.
                                              format(tds_base.state_names[prior_state], tds_base.state_names[state]))
            self.state = state
        elif state == tds_base.TDS_DEAD:
            self.state = state
        elif state == tds_base.TDS_QUERYING:
            if self.state == tds_base.TDS_DEAD:
                raise tds_base.InterfaceError('logic error: cannot change query state from {0} to {1}'.
                                              format(tds_base.state_names[prior_state], tds_base.state_names[state]))
            elif self.state != tds_base.TDS_IDLE:
                raise tds_base.InterfaceError('logic error: cannot change query state from {0} to {1}'.
                                              format(tds_base.state_names[prior_state], tds_base.state_names[state]))
            else:
                self.rows_affected = tds_base.TDS_NO_COUNT
                self.internal_sp_called = 0
                self.state = state
        else:
            assert False
        return self.state