<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def _cancel(self, action: Callable = None, aid: int = None):
 """ Cancel scheduled events :param action: (optional) scheduled action. If specified, all scheduled events for the action are cancelled. :param aid: (options) scheduled event id. If specified, scheduled event with the aid is cancelled. """

        if action is not None:
            if action in self.scheduled:
                logger.trace("{} cancelling all events for action {}, ids: {}"
                             "".format(self, action, self.scheduled[action]))
                self.scheduled[action].clear()
        elif aid is not None:
            for action, aids in self.scheduled.items():
                try:
                    aids.remove(aid)
                except ValueError:
                    pass
                else:
                    logger.trace("{} cancelled action {} with id {}".format(self, action, aid))
                    break