Esempio n. 1
0
class ResultFilter(object):

    def __init__(self, results_class):
        self._rf_worker = Worker()
        self._results_class = results_class
        self.thresholds = [20,1,1]

    def ready_to_shutdown(self):
        return self._rf_worker.ready_to_shutdown()

    def submit_ranker_result(self, rank_result):
        """
        This function is used by Ranker.
        Takes [page_url, page_weight, [(link_soup,weight)]] and passes it to rf_worker
        as
        [page_url, page_weight, [(link_soup,weight)], self.thresholds, self._results_class]
        """
        args = list(rank_result) + [self.thresholds, self._results_class]
        rfw_task = WorkerTask(args, result_filter_routine, args[0])
        self._rf_worker.add_task(rfw_task)

    def get_result(self, filter_match = (lambda x: True)):
        """
        Returns [page_url, page_weight, [(link,weight)]]
        """
        completed_rfw_task = self._rf_worker.get_completed_task(filter_match)
        if completed_rfw_task is None:
            return None
        return completed_rfw_task.result
    
    def purge_tasks(self, filter_not_match):
        """
        Removes all tasks, for which <filter_not_match> returns False.
        """
        self._rf_worker.purge_tasks(filter_not_match)