<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:

def get_medium(self, agent_type, index=0):
        '''Returns the medium class for the
        given agent_type. Optional index tells which one to give.'''
        mediums = list(x for x in self.agency._agents
                       if x.get_descriptor().type_name == agent_type)
        try:
            return mediums[index]
        except KeyError:
            raise RuntimeError(
                'Requested medium class of %s with index %d, but found only '
                '%d medium of this type'
                % (agent_type, index, len(mediums))), None, sys.exc_info()[2]