<commit_before>import os
import pickle
import pylibmc

import sklearn.cross_validation

CACHE_KEY = 'fickle:predictor'
CACHE = pylibmc.Client(
        servers = [os.environ.get('MEMCACHE_SERVERS', '127.0.0.1')],
        username=os.environ.get('MEMCACHE_USERNAME'),
        password=os.environ.get('MEMCACHE_PASSWORD'),
        binary=True
)

class Backend(object):
    def __init__(self):
        self.dataset_id = 0
        self.__random_id = 0
        self.__dataset = None
        self.__model = None

    def load(self, dataset):
        self.__model = None
        self.dataset_id += 1
        self.__dataset = dataset
        self.__data = dataset['data']
        self.__target = dataset['target']
        return True

    def loaded(self):
        return bool(self.__dataset)

    def fit(self):
        if not self.loaded():
            return
        model = self.model()
        model.fit(self.__data, self.__target)
        self.__model = model
        self.__dump()
        return True

    def trained(self):
        return bool(self.__model)

    def predict(self, value):
        if not self.trained() and not self.__load():
            return
        return self.__model.predict(value)

    def validate(self):
        if not self.loaded():
            return
        self.__random_id += 1
        if random_state is None:
            random_state = self.__random_id
        model = self.model()
        X_train, X_test, y_train, y_test = sklearn.cross_validation.train_test_split(
            self.__data, self.__target, test_size = None, random_state = self.__random_id
        )
        model.fit(X_train, y_train)
        return [model.score(X_test, y_test)]

    def __load(self):
        string = CACHE.get(CACHE_KEY)
        if not string:
            return
        model = pickle.loads(string)
        self.__model = model
        return True

    def __dump(self):
        if not self.trained():
            return
        string = pickle.dumps(self.__model)
        CACHE.set(CACHE_KEY, string)
        return True
<commit_msg>Move cache load to guard method<commit_after>import os
import pickle
import pylibmc

import sklearn.cross_validation

CACHE_KEY = 'fickle:predictor'
CACHE = pylibmc.Client(
        servers = [os.environ.get('MEMCACHE_SERVERS', '127.0.0.1')],
        username=os.environ.get('MEMCACHE_USERNAME'),
        password=<PASSWORD>('MEMCACHE_PASSWORD'),
        binary=True
)

class Backend(object):
    def __init__(self):
        self.dataset_id = 0
        self.__random_id = 0
        self.__dataset = None
        self.__model = None

    def load(self, dataset):
        self.__model = None
        self.dataset_id += 1
        self.__dataset = dataset
        self.__data = dataset['data']
        self.__target = dataset['target']
        return True

    def loaded(self):
        return bool(self.__dataset)

    def fit(self):
        if not self.loaded():
            return
        model = self.model()
        model.fit(self.__data, self.__target)
        self.__model = model
        self.__dump()
        return True

    def trained(self):
        return bool(self.__model) or bool(self.__load())

    def predict(self, value):
        if not self.trained():
            return
        return self.__model.predict(value)

    def validate(self):
        if not self.loaded():
            return
        self.__random_id += 1
        if random_state is None:
            random_state = self.__random_id
        model = self.model()
        X_train, X_test, y_train, y_test = sklearn.cross_validation.train_test_split(
            self.__data, self.__target, test_size = None, random_state = self.__random_id
        )
        model.fit(X_train, y_train)
        return [model.score(X_test, y_test)]

    def __load(self):
        string = CACHE.get(CACHE_KEY)
        if not string:
            return
        model = pickle.loads(string)
        self.__model = model
        return True

    def __dump(self):
        if not self.trained():
            return
        string = pickle.dumps(self.__model)
        CACHE.set(CACHE_KEY, string)
        return True

{'fraction_non_alphanumeric': 0.0993939393939394, 'fraction_numerical': 0.0048484848484848485, 'mean_word_length': 2.0540340488527016, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '718809', 'n_tokens_mistral': 1232, 'n_tokens_neox': 1200, 'n_words': 280}