<commit_before># Copyright (c) 2010-2013, GEM Foundation.
#
# OpenQuake is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# OpenQuake is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with OpenQuake.  If not, see <http://www.gnu.org/licenses/>.

"""Base code for calculator classes."""

from openquake.engine import logs
from openquake.engine.performance import EnginePerformanceMonitor
from openquake.engine.utils import tasks

# Routing key format string for communication between tasks and the control
# node.
ROUTING_KEY_FMT = 'oq.job.%(job_id)s.tasks'


class Calculator(object):
    """
    Base class for all calculators.

    :param job: :class:`openquake.engine.db.models.OqJob` instance.
    """

    #: The core calculation Celery task function, which accepts the arguments
    #: generated by :func:`task_arg_gen`.
    core_calc_task = None

    def __init__(self, job):
        self.job = job
        self.num_tasks = None
        self.progress = dict(total=0, computed=0, in_queue=0)
        self.progress_handler = lambda _p, _c: None

    def register_progress_handler(self, fn):
        self.progress_handler = fn

    def monitor(self, operation):
        return EnginePerformanceMonitor(
            operation, self.job.id, tracing=True, flush=True)

    def task_arg_gen(self, block_size):
        """
        Generator function for creating the arguments for each task.

        Subclasses must implement this.

        :param int block_size:
            The number of work items per task (sources, sites, etc.).
        """
        raise NotImplementedError

    def block_size(self):
        """
        Number of work items per task.

        Subclasses must implement this.
        """
        raise NotImplementedError()

    def concurrent_tasks(self):
        """
        Number of tasks to be in queue at any given time.

        Subclasses must implement this.
        """<commit_msg>added documentation<commit_after># Copyright (c) 2010-2013, GEM Foundation.
#
# OpenQuake is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# OpenQuake is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with OpenQuake.  If not, see <http://www.gnu.org/licenses/>.

"""Base code for calculator classes."""

from openquake.engine import logs
from openquake.engine.performance import EnginePerformanceMonitor
from openquake.engine.utils import tasks

# Routing key format string for communication between tasks and the control
# node.
ROUTING_KEY_FMT = 'oq.job.%(job_id)s.tasks'


# Default progress handler. It just ignores its arguments
DEFAULT_PROGRESS_HANDLER = lambda _p, _c: None


class Calculator(object):
    """
    Base class for all calculators.

    :param job: :class:`openquake.engine.db.models.OqJob` instance.
    """

    #: The core calculation Celery task function, which accepts the arguments
    #: generated by :func:`task_arg_gen`.
    core_calc_task = None

    def __init__(self, job):
        self.job = job
        self.num_tasks = None
        self.progress = dict(total=0, computed=0, in_queue=0)
        self.progress_handler = DEFAULT_PROGRESS_HANDLER

    def register_progress_handler(self, fn):
        """
        Register a callback which provides information on the progress
        of the calculation.

        :param callable fn:
            a callable accepting two arguments:
            1) the job status (e.g. "pre-executing", "22%")
            2) the calculation object
        """
        self.progress_handler = fn

    def monitor(self, operation):
        return EnginePerformanceMonitor(
            operation, self.job.id, tracing=True, flush=True)

    def task_arg_gen(self, block_size):
        """
        Generator function for creating the arguments for each task.

        Subclasses must implement this.

        :param int block_size:
            The number of work items per task (sources, sites, etc.).
        """
        raise NotImplementedError

    def block_size(self):
        """
        Number of work items per task.

        Subclasses must implement this.
        """
        raise NotImplementedError()

    def concurrent_tasks(self):
        """
        Number of tasks to be in queue at any given time.

        Subclasses must implement this.
        """
{'fraction_non_alphanumeric': 0.07686335403726709, 'fraction_numerical': 0.005434782608695652, 'mean_word_length': 2.912680334092635, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1064581', 'n_tokens_mistral': 1413, 'n_tokens_neox': 1346, 'n_words': 548}