<commit_before>import time
import threading
import transaction
import datetime

from pyramid.threadlocal import get_current_registry

import zmq
from zmq.eventloop.ioloop import IOLoop
from zmq.eventloop.zmqstream import ZMQStream

from .core import FlowNode, BehavioralFlowNode, ProcessFinished
from dace.z3 import Job
from dace import log

# shared between threads
callbacks = {}
callbacks_lock = threading.Lock()

class DelayedCallback(object):
    """Schedule the given callback to be called once.

    The callback is called once, after callback_time milliseconds.

    `start` must be called after the DelayedCallback is created.

    The timeout is calculated from when `start` is called.
    """
    def __init__(self, callback, callback_time, io_loop=None):
        self.callback = callback
        self.callback_time = callback_time
        self.io_loop = io_loop or IOLoop.instance()
        self._timeout = None

    def start(self):
        """Start the timer."""
        def start_delayed_callback(dc):
            ioloop = IOLoop.current()
            dc._timeout = ioloop.add_timeout(
                ioloop.time() + self.callback_time / 1000.0, self.callback)
        self.io_loop.add_callback(start_delayed_callback, self)

    def stop(self):
        """Stop the timer."""
        if self._timeout is not None:
            def stop_delayed_callback(dc):
                ioloop = IOLoop.current()
                ioloop.remove_timeout(dc._timeout)
                dc._timeout = None
            self.io_loop.add_callback(stop_delayed_callback, self)


def push_callback_after_commit(event, callback, callback_params, deadline):
    # Create job object now before the end of the interaction so we have
    # the logged in user.
    job = Job()
    def after_commit_hook(status, *args, **kws):
        # status is true if the commit succeeded, or false if the commit aborted.
        if status:
            # Set callable now, we are sure to have p_oid
            event, callback, callback_params, deadline, job = args
            job.callable = callback
            job.args = callback_params
            dc = DelayedCallback(job, deadline)
            with callbacks_lock:
                callbacks[event._p_oid] = dc
            dc.start()

    transaction.get().addAfterCommitHook(after_commit_hook, args=(event, callback, callback_params, deadline, job))


class Event(BehavioralFlowNode, FlowNode):
    def __init__(self, definition, eventKind):
        super(Event, self).__init__(definition)
        self.eventKind = eventKind
        self.execution_prepared = False
        self.execution_finished = False
        if eventKind:
            eventKind.event = self

    def __call__(self, transition):
        pass

    def prepare_for_execution(self):
        self.execution_prepared = True
        self.execution_finished = False

    def start(self, transaction):
        wi = self._get_workitem()
        if wi is not None:
            super(Event, self).start(transaction)
            self.finish_behavior(wi)
            self.execution_finished = True
        else:
            self.stop()

    def replay_path(self, decision, transaction):
        if not self.execution_finished:
            self.finish_behavior(decision)

        super(Event, self).replay_path(decision, transaction)

    def finish_behavior(self, wi):
        self.execute()
        super(Event, self).finish_behavior(wi)

    def validate(self):
        pass # pragma: no cover

    def execute(self):
        pass # pragma: no cover

    def stop(self):
        self.setproperty('workitems', [])


class Throwing(Event):

    def validate(self):
        return True

    def prepare_for_execution(self):
        if not self.execution_prepared:
            super(Throwing, self).prepare_for_execution()
            if self.validate():
                self.start(None)
            else:
                self.stop()

    # l' operation est sans parametres (les parametres sont sur la definition est sont calculable)
    def execute(self):
        if self.eventKind is None:
            return

        return self.eventKind.execute()


class Catching(Event):

    def validate(self):
        if self.eventKind is None:<commit_msg>don't delete workitems at stop<commit_after>import time
import threading
import transaction
import datetime

from pyramid.threadlocal import get_current_registry

import zmq
from zmq.eventloop.ioloop import IOLoop
from zmq.eventloop.zmqstream import ZMQStream

from .core import FlowNode, BehavioralFlowNode, ProcessFinished
from dace.z3 import Job
from dace import log

# shared between threads
callbacks = {}
callbacks_lock = threading.Lock()

class DelayedCallback(object):
    """Schedule the given callback to be called once.

    The callback is called once, after callback_time milliseconds.

    `start` must be called after the DelayedCallback is created.

    The timeout is calculated from when `start` is called.
    """
    def __init__(self, callback, callback_time, io_loop=None):
        self.callback = callback
        self.callback_time = callback_time
        self.io_loop = io_loop or IOLoop.instance()
        self._timeout = None

    def start(self):
        """Start the timer."""
        def start_delayed_callback(dc):
            ioloop = IOLoop.current()
            dc._timeout = ioloop.add_timeout(
                ioloop.time() + self.callback_time / 1000.0, self.callback)
        self.io_loop.add_callback(start_delayed_callback, self)

    def stop(self):
        """Stop the timer."""
        if self._timeout is not None:
            def stop_delayed_callback(dc):
                ioloop = IOLoop.current()
                ioloop.remove_timeout(dc._timeout)
                dc._timeout = None
            self.io_loop.add_callback(stop_delayed_callback, self)


def push_callback_after_commit(event, callback, callback_params, deadline):
    # Create job object now before the end of the interaction so we have
    # the logged in user.
    job = Job()
    def after_commit_hook(status, *args, **kws):
        # status is true if the commit succeeded, or false if the commit aborted.
        if status:
            # Set callable now, we are sure to have p_oid
            event, callback, callback_params, deadline, job = args
            job.callable = callback
            job.args = callback_params
            dc = DelayedCallback(job, deadline)
            with callbacks_lock:
                callbacks[event._p_oid] = dc
            dc.start()

    transaction.get().addAfterCommitHook(after_commit_hook, args=(event, callback, callback_params, deadline, job))


class Event(BehavioralFlowNode, FlowNode):
    def __init__(self, definition, eventKind):
        super(Event, self).__init__(definition)
        self.eventKind = eventKind
        self.execution_prepared = False
        self.execution_finished = False
        if eventKind:
            eventKind.event = self

    def __call__(self, transition):
        pass

    def prepare_for_execution(self):
        self.execution_prepared = True
        self.execution_finished = False

    def start(self, transaction):
        wi = self._get_workitem()
        if wi is not None:
            super(Event, self).start(transaction)
            self.execute()
            self.finish_behavior(wi)
            self.execution_finished = True
        else:
            self.stop()

    def replay_path(self, decision, transaction):
        if not self.execution_finished:
            self.execute()
            self.finish_behavior(decision)
            self.execution_finished = True

        super(Event, self).replay_path(decision, transaction)

    def validate(self):
        pass # pragma: no cover

    def execute(self):
        pass # pragma: no cover

    def stop(self):
        pass # pragma: no cover


class Throwing(Event):

    def validate(self):
        return True

    def prepare_for_execution(self):
        if not self.execution_prepared:
            super(Throwing, self).prepare_for_execution()
            if self.validate():
                self.start(None)
            else:
                self.stop()

    # l' operation est sans parametres (les parametres sont sur la definition est sont calculable)
    def execute(self):
        if self.eventKind is None:
            return

        return self.eventKind.execute()


class Catching(Event):

    def validate(self):
        if self.eventKind is None:
{'fraction_non_alphanumeric': 0.07968786947268858, 'fraction_numerical': 0.0014187751241428233, 'mean_word_length': 2.4470252648736754, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3194998', 'n_tokens_mistral': 2392, 'n_tokens_neox': 2247, 'n_words': 668}