<commit_before>import logging

from datetime import datetime
from multiprocessing.util import Finalize

from anyjson import deserialize, serialize
from django.db import transaction
from django.core.exceptions import ObjectDoesNotExist

from celery import schedules
from celery.beat import Scheduler, ScheduleEntry
from celery.utils.encoding import safe_str, safe_repr

from djcelery.models import (PeriodicTask, PeriodicTasks,
                             CrontabSchedule, IntervalSchedule)


class ModelEntry(ScheduleEntry):
    model_schedules = ((schedules.crontab, CrontabSchedule, "crontab"),
                       (schedules.schedule, IntervalSchedule, "interval"))
    save_fields = ["last_run_at", "total_run_count", "no_changes"]

    def __init__(self, model):
        self.name = model.name
        self.task = model.task
        self.schedule = model.schedule
        try:
            self.args = deserialize(model.args or u"[]")
            self.kwargs = deserialize(model.kwargs or u"{}")
        except ValueError:
            # disable because of error deserializing args/kwargs
            model.no_changes = True
            model.enabled = False
            model.save()
            raise

        self.options = {"queue": model.queue,
                        "exchange": model.exchange,
                        "routing_key": model.routing_key,
                        "expires": model.expires}
        self.total_run_count = model.total_run_count
        self.model = model

        if not model.last_run_at:
            model.last_run_at = self._default_now()
        self.last_run_at = model.last_run_at

    def is_due(self):
        if not self.model.enabled:
            return False, 5.0   # 5 second delay for re-enable.
        return self.schedule.is_due(self.last_run_at)

    def _default_now(self):
        return datetime.now()

    def next(self):
        self.model.last_run_at = datetime.now()
        self.model.total_run_count += 1
        self.model.no_changes = True
        return self.__class__(self.model)

    def save(self):
        # Object may not be synchronized, so only
        # change the fields we care about.
        obj = self.model._default_manager.get(pk=self.model.pk)
        for field in self.save_fields:
            setattr(obj, field, getattr(self.model, field))
        obj.save()

    @classmethod
    def to_model_schedule(cls, schedule):
        for schedule_type, model_type, model_field in cls.model_schedules:
            if isinstance(schedule, schedule_type):
                model_schedule = model_type.from_schedule(schedule)<commit_msg>2to3 converts o.next() to next(o) even if not actually an iterator<commit_after>import logging

from datetime import datetime
from multiprocessing.util import Finalize

from anyjson import deserialize, serialize
from django.db import transaction
from django.core.exceptions import ObjectDoesNotExist

from celery import schedules
from celery.beat import Scheduler, ScheduleEntry
from celery.utils.encoding import safe_str, safe_repr

from djcelery.models import (PeriodicTask, PeriodicTasks,
                             CrontabSchedule, IntervalSchedule)


class ModelEntry(ScheduleEntry):
    model_schedules = ((schedules.crontab, CrontabSchedule, "crontab"),
                       (schedules.schedule, IntervalSchedule, "interval"))
    save_fields = ["last_run_at", "total_run_count", "no_changes"]

    def __init__(self, model):
        self.name = model.name
        self.task = model.task
        self.schedule = model.schedule
        try:
            self.args = deserialize(model.args or u"[]")
            self.kwargs = deserialize(model.kwargs or u"{}")
        except ValueError:
            # disable because of error deserializing args/kwargs
            model.no_changes = True
            model.enabled = False
            model.save()
            raise

        self.options = {"queue": model.queue,
                        "exchange": model.exchange,
                        "routing_key": model.routing_key,
                        "expires": model.expires}
        self.total_run_count = model.total_run_count
        self.model = model

        if not model.last_run_at:
            model.last_run_at = self._default_now()
        self.last_run_at = model.last_run_at

    def is_due(self):
        if not self.model.enabled:
            return False, 5.0   # 5 second delay for re-enable.
        return self.schedule.is_due(self.last_run_at)

    def _default_now(self):
        return datetime.now()

    def next(self):
        self.model.last_run_at = datetime.now()
        self.model.total_run_count += 1
        self.model.no_changes = True
        return self.__class__(self.model)
    __next__ = next  # for 2to3

    def save(self):
        # Object may not be synchronized, so only
        # change the fields we care about.
        obj = self.model._default_manager.get(pk=self.model.pk)
        for field in self.save_fields:
            setattr(obj, field, getattr(self.model, field))
        obj.save()

    @classmethod
    def to_model_schedule(cls, schedule):
        for schedule_type, model_type, model_field in cls.model_schedules:
            if isinstance(schedule, schedule_type):
                model_schedule = model_type.from_schedule(schedule)
{'fraction_non_alphanumeric': 0.08378836377330069, 'fraction_numerical': 0.002259461495010356, 'mean_word_length': 2.3769866497139223, 'pattern_counts': {'":': 8, '<': 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': '3911487', 'n_tokens_mistral': 1506, 'n_tokens_neox': 1464, 'n_words': 371}