<commit_before># -*- coding: utf-8 -*-

import datetime
import pkg_resources
import shelve


class _ScorekeeperSharedState(object):
    _shared_state = {}

    def __init__(self):
        self.__dict__ = _Borg._shared_state.setdefault(type(self), {"score": 0})


class Scorekeeper(_ScorekeeperSharedState):
    def __init__(self):
        _Borg.__init__(self)

    def __call__(self, score, threshold=100, decay=None, callback=None):
        return_value = None
        self.score = self._get_score()[1]
        if decay:
            self._decay(decay)
        self.score += score
        if self.score > threshold:
            self.reset_score()
            return_value = self.default_callback() if not callback else callback()
        self._set_score()
        return return_value

    def default_callback(self):
        raise ScoreExceededError("Score has been exceeded!")

    def reset_score(self):
        self.score = 0

    def _get_score(self):
        with shelve.open(self._shelve_path()) as db:
            key = type(self).__name__
            result = db.get(key) or {}
        return result.get("datetime"), result.get("score", 0)
<commit_msg>Ensure score is retrieved on instantiation<commit_after># -*- coding: utf-8 -*-

import datetime
import pkg_resources
import shelve


class _ScorekeeperSharedState(object):
    _shared_state = {}

    def __init__(self):
        self.__dict__ = _ScorekeeperSharedState._shared_state.setdefault(type(self), {})


class Scorekeeper(_ScorekeeperSharedState):
    def __init__(self):
        _ScorekeeperSharedState.__init__(self)
        self.score = self._get_score()[1]

    def __call__(self, score, threshold=100, decay=None, callback=None):
        return_value = None
        self.score = self._get_score()[1]
        if decay:
            self._decay(decay)
        self.score += score
        if self.score > threshold:
            self.reset_score()
            return_value = self.default_callback() if not callback else callback()
        self._set_score()
        return return_value

    def default_callback(self):
        raise ScoreExceededError("Score has been exceeded!")

    def reset_score(self):
        self.score = 0

    def _get_score(self):
        with shelve.open(self._shelve_path()) as db:
            key = type(self).__name__
            result = db.get(key) or {}
        return result.get("datetime"), result.get("score", 0)

{'fraction_non_alphanumeric': 0.10839884153909805, 'fraction_numerical': 0.006619776582540339, 'mean_word_length': 2.697247706422018, 'pattern_counts': {'":': 1, '<': 3, '<?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': '7629002', 'n_tokens_mistral': 722, 'n_tokens_neox': 693, 'n_words': 161}