<commit_before>"""
Stores jobs in a file governed by the :mod:`shelve` module.
"""

import shelve
import pickle
import random

from apscheduler.jobstores.base import JobStore
from apscheduler.util import obj_to_ref


def store(mode):
    def outer(func):
        def wrapper(self, *args, **kwargs):
            store = shelve.open(self.path, mode, self.protocol)
            try:
                return func(self, store, *args, **kwargs)
            finally:
                store.close()
        return wrapper
    return outer


class ShelveJobStore(JobStore):
    MAX_ID = 1000000

    def __init__(self, path, protocol=pickle.HIGHEST_PROTOCOL):
        self.jobs = []
        self.path = path
        self.protocol = protocol

    def _generate_id(self, store):
        id = None
        while not id:
            id = str(random.randint(1, self.MAX_ID))
            if not id in store:
                return id

    @store('c')
    def add_job(self, store, job):
        job.func_ref = obj_to_ref(job.func)
        job.id = self._generate_id(store)
        self.jobs.append(job)
        store[job.id] = job

    @store('w')
    def update_job(self, store, job):
        store[job.id] = job

    @store('w')
    def remove_job(self, store, job):
        del store[job.id]
        self.jobs.remove(job)

    @store('r')
    def load_jobs(self, store):
        self.jobs = store.values()

    def __repr__(self):
        return '<%s (path=%s)>' % (self.__class__.__name__, self.path)<commit_msg>shelve.open() needs to be called with flags='c' in all cases<commit_after>"""
Stores jobs in a file governed by the :mod:`shelve` module.
"""

import shelve
import pickle
import random

from apscheduler.jobstores.base import JobStore
from apscheduler.util import obj_to_ref


def store(func):
    def wrapper(self, *args, **kwargs):
        store = shelve.open(self.path, 'c', self.protocol)
        try:
            return func(self, store, *args, **kwargs)
        finally:
            store.close()
    return wrapper


class ShelveJobStore(JobStore):
    MAX_ID = 1000000

    def __init__(self, path, protocol=pickle.HIGHEST_PROTOCOL):
        self.jobs = []
        self.path = path
        self.protocol = protocol

    def _generate_id(self, store):
        id = None
        while not id:
            id = str(random.randint(1, self.MAX_ID))
            if not id in store:
                return id

    @store
    def add_job(self, store, job):
        job.func_ref = obj_to_ref(job.func)
        job.id = self._generate_id(store)
        self.jobs.append(job)
        store[job.id] = job

    @store
    def update_job(self, store, job):
        store[job.id] = job

    @store
    def remove_job(self, store, job):
        del store[job.id]
        self.jobs.remove(job)

    @store
    def load_jobs(self, store):
        self.jobs = store.values()

    def __repr__(self):
        return '<%s (path=%s)>' % (self.__class__.__name__, self.path)
{'fraction_non_alphanumeric': 0.11671177266576455, 'fraction_numerical': 0.005412719891745603, 'mean_word_length': 2.253025302530253, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '6629999', 'n_tokens_mistral': 988, 'n_tokens_neox': 946, 'n_words': 244}