<commit_before>import os
import json
from mongoquery import Query
from ..template.mds import MDSTemplate, MDSROTemplate
from ..template.core import ASCENDING, DESCENDING


class JSONCollection(object):
    def __init__(self, fp):
        self._fp = fp
        self.refresh()

    def refresh(self):
        with open(self._fp, 'r') as f:
            self._docs = json.load(f)

    def find(self, query, sort=None):
        match = Query(query).match
        result = filter(match, self._docs)
        if sort is None:
            return result
        elif len(sort) > 2:
            raise NotImplementedError("Only one sort key is supported.")
        else:
            sort, = sort
            # ascending_or_descending is -1 (descending) or 1 (ascending)
            key, ascending_or_descending = sort
            reverse = (ascending_or_descending == DESCENDING)
            sorted_result = sorted(result, key=lambda x: x[key], reverse=reverse)
            # Make it a generator so it is the same as the unsorted code path.
            return (elem for elem in sorted_result)

    def find_one(self, query):
        match = Query(query).match
        for doc in self._docs:
            if match(doc):
                return doc
            return None

    def insert_one(self, doc):
        with open(self._fp, 'r') as f:
            data = json.load(f)
        data.append(doc)
        self._docs = data
        with open(self._fp, 'w') as f:
            json.dump(data, f)

    def insert(self, docs):
        with open(self._fp, 'r') as f:
            data = json.load(f)
        data.extend(docs)
        self._docs = data
        with open(self._fp, 'w') as f:
            json.dump(data, f)


class _CollectionMixin(object):
    def __init__(self, *args, **kwargs):
        self._config = None
        super().__init__(*args, **kwargs)
        self.__event_col = None
        self.__descriptor_col = None
        self.__runstart_col = None
        self.__runstop_col = None

    @property
    def config(self):
        return self._config

    @config.setter
    def config(self, val):
        self._config = val
        self.__event_col = None<commit_msg>FIX: If JSON files do not yet exist, create them.<commit_after>import os
import json
from mongoquery import Query
from ..template.mds import MDSTemplate, MDSROTemplate
from ..template.core import ASCENDING, DESCENDING


class JSONCollection(object):
    def __init__(self, fp):
        self._fp = fp
        self.refresh()

    def refresh(self):
        with open(self._fp, 'r') as f:
            self._docs = json.load(f)

    def find(self, query, sort=None):
        match = Query(query).match
        result = filter(match, self._docs)
        if sort is None:
            return result
        elif len(sort) > 2:
            raise NotImplementedError("Only one sort key is supported.")
        else:
            sort, = sort
            # ascending_or_descending is -1 (descending) or 1 (ascending)
            key, ascending_or_descending = sort
            reverse = (ascending_or_descending == DESCENDING)
            sorted_result = sorted(result, key=lambda x: x[key], reverse=reverse)
            # Make it a generator so it is the same as the unsorted code path.
            return (elem for elem in sorted_result)

    def find_one(self, query):
        match = Query(query).match
        for doc in self._docs:
            if match(doc):
                return doc
            return None

    def insert_one(self, doc):
        if os.path.isfile(self._fp):
            with open(self._fp, 'r') as f:
                data = json.load(f)
        else:
            data = []
        data.append(doc)
        self._docs = data
        with open(self._fp, 'w') as f:
            json.dump(data, f)

    def insert(self, docs):
        if os.path.isfile(self._fp):
            with open(self._fp, 'r') as f:
                data = json.load(f)
        else:
            data = []
        data.extend(docs)
        self._docs = data
        with open(self._fp, 'w') as f:
            json.dump(data, f)


class _CollectionMixin(object):
    def __init__(self, *args, **kwargs):
        self._config = None
        super().__init__(*args, **kwargs)
        self.__event_col = None
        self.__descriptor_col = None
        self.__runstart_col = None
        self.__runstop_col = None

    @property
    def config(self):
        return self._config

    @config.setter
    def config(self, val):
        self._config = val
        self.__event_col = None
{'fraction_non_alphanumeric': 0.09902955447728275, 'fraction_numerical': 0.0013233348037053375, 'mean_word_length': 1.9051889814221652, 'pattern_counts': {'":': 0, '<': 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': '2861526', 'n_tokens_mistral': 1348, 'n_tokens_neox': 1319, 'n_words': 392}