<commit_before>import os
import logging
import copy

from tinydb import *
from operator import itemgetter
import operator
from uuid import uuid1
from bson.objectid import ObjectId

logger = logging.getLogger(__name__)


class TinyMongoClient(object):
    def __init__(self, foldername="tinydb"):
        self.foldername = foldername
        try:
            os.mkdir(foldername)
        except FileExistsError:
            pass

    def __getitem__(self, key):
        return TinyMongoDatabase(key, self.foldername)

    def close(self):
        pass

    def __getattr__(self, name):
        return TinyMongoDatabase(name, self.foldername)


class TinyMongoDatabase(object):
    def __init__(self, database, foldername):
        self.foldername = foldername
        self.tinydb = TinyDB(os.path.join(foldername, database + ".json"))

    def __getattr__(self, name):
        return TinyMongoCollection(name, self)


class TinyMongoCollection(object):
    """
    This class represents a collection and all of the operations that are commonly performed on a collection
    """

    def __init__(self, table, parent=None):
        """
        Initilialize the collection

        :param table: the table name
        :param parent: the parent db name
        """
        self.tablename = table
        self.table = None
        self.parent = parent
        self.insert_one = self.insert
        self.insert_many = self.insert
        self.update_one = self.update_one
        self.update_many = self.update_one

    def __getattr__(self, name):
        if self.table is None:
            self.tablename += "." + name
        return self

    def build_table(self):
        self.table = self.parent.tinydb.table(self.tablename)

    def insert(self, data, **kwargs):
        if self.table is None:
            self.build_table()

        if not isinstance(data, list):
            data = [data]

        eids = []
        for adat in data:
            if not "_id" in adat:
                theid = str(uuid1()).replace("-", "")
                eids.append(theid)
                adat["_id"] = theid
            else:
                eids.append(adat["_id"])
            self.table.insert(adat)

        if len(eids) == 1:
            return eids[0]

        return eids

    def parse_query(self, query):
        logger.debug('query to parse2: {}'.format(query))

        # this should find all records
        if query == {}:
            return (Query()._id != '-1')

        q = None
        # find the final result of the generator
        for c in self.parse_condition(query):
            q = c

        logger.debug('new query item2: {}'.format(q))

        return q

    def parse_condition(self, query, prev_key=None, conditions=None):
        # use this to determine gt/lt/eq on prev_query
        logger.debug('query: {} prev_query: {}'.format(query, prev_key))

        q = Query()

        # deal with the {'name': value} case by injecting a previous key
        if not prev_key:
            temp_query = copy.deepcopy(query)
            k, v = temp_query.popitem()
            prev_key = k

        # deal with the conditions
        for key, value in query.items():
            logger.debug('conditions: {} {}'.format(key, value))

            if key == '$gte':
                conditions = (q[prev_key] >= value) if not conditions else (conditions & (q[prev_key] >= value))
            elif key == '$gt':
                conditions = (q[prev_key] > value) if not conditions else (conditions & (q[prev_key] > value))
            elif key == '$lte':
                conditions = (q[prev_key] <= value) if not conditions else (conditions & (q[prev_key] <= value))
            elif key == '$lt':
                conditions = (q[prev_key] < value) if not conditions else (conditions & (q[prev_key] < value))
            else:
                conditions = (q[prev_key] == value) if not conditions else (conditions & (q[prev_key] == value))

            logger.debug('c: {}'.format(conditions))
            if isinstance(value, dict):
                yield from self.parse_condition(value, key)
            else:
                yield conditions

    def update_one(self, query, data, argsdict={}, **kwargs):
        if self.table is None:
            self.build_table()

        if "$set" in data:
            data = data["$set"]

        allcond = self.parse_query(query)

        try:
            self.table.update_one(data, allcond)
        except:
            # todo: exception too broad
            return False

        # todo: return result with result.matched_count and result.modified_count
        return True

    def find(self, query=None):
        if self.table is None:<commit_msg>Fixed a problem with table update<commit_after>import os
import logging
import copy

from tinydb import *
from operator import itemgetter
import operator
from uuid import uuid1
from bson.objectid import ObjectId

logger = logging.getLogger(__name__)


class TinyMongoClient(object):
    def __init__(self, foldername="tinydb"):
        self.foldername = foldername
        try:
            os.mkdir(foldername)
        except FileExistsError:
            pass

    def __getitem__(self, key):
        return TinyMongoDatabase(key, self.foldername)

    def close(self):
        pass

    def __getattr__(self, name):
        return TinyMongoDatabase(name, self.foldername)


class TinyMongoDatabase(object):
    def __init__(self, database, foldername):
        self.foldername = foldername
        self.tinydb = TinyDB(os.path.join(foldername, database + ".json"))

    def __getattr__(self, name):
        return TinyMongoCollection(name, self)


class TinyMongoCollection(object):
    """
    This class represents a collection and all of the operations that are commonly performed on a collection
    """

    def __init__(self, table, parent=None):
        """
        Initilialize the collection

        :param table: the table name
        :param parent: the parent db name
        """
        self.tablename = table
        self.table = None
        self.parent = parent
        self.insert_one = self.insert
        self.insert_many = self.insert
        self.update_one = self.update_one
        self.update_many = self.update_one

    def __getattr__(self, name):
        if self.table is None:
            self.tablename += "." + name
        return self

    def build_table(self):
        self.table = self.parent.tinydb.table(self.tablename)

    def insert(self, data, **kwargs):
        if self.table is None:
            self.build_table()

        if not isinstance(data, list):
            data = [data]

        eids = []
        for adat in data:
            if not "_id" in adat:
                theid = str(uuid1()).replace("-", "")
                eids.append(theid)
                adat["_id"] = theid
            else:
                eids.append(adat["_id"])
            self.table.insert(adat)

        if len(eids) == 1:
            return eids[0]

        return eids

    def parse_query(self, query):
        logger.debug('query to parse2: {}'.format(query))

        # this should find all records
        if query == {}:
            return (Query()._id != '-1')

        q = None
        # find the final result of the generator
        for c in self.parse_condition(query):
            q = c

        logger.debug('new query item2: {}'.format(q))

        return q

    def parse_condition(self, query, prev_key=None, conditions=None):
        # use this to determine gt/lt/eq on prev_query
        logger.debug('query: {} prev_query: {}'.format(query, prev_key))

        q = Query()

        # deal with the {'name': value} case by injecting a previous key
        if not prev_key:
            temp_query = copy.deepcopy(query)
            k, v = temp_query.popitem()
            prev_key = k

        # deal with the conditions
        for key, value in query.items():
            logger.debug('conditions: {} {}'.format(key, value))

            if key == '$gte':
                conditions = (q[prev_key] >= value) if not conditions else (conditions & (q[prev_key] >= value))
            elif key == '$gt':
                conditions = (q[prev_key] > value) if not conditions else (conditions & (q[prev_key] > value))
            elif key == '$lte':
                conditions = (q[prev_key] <= value) if not conditions else (conditions & (q[prev_key] <= value))
            elif key == '$lt':
                conditions = (q[prev_key] < value) if not conditions else (conditions & (q[prev_key] < value))
            else:
                conditions = (q[prev_key] == value) if not conditions else (conditions & (q[prev_key] == value))

            logger.debug('c: {}'.format(conditions))
            if isinstance(value, dict):
                yield from self.parse_condition(value, key)
            else:
                yield conditions

    def update_one(self, query, data, argsdict={}, **kwargs):
        if self.table is None:
            self.build_table()

        if "$set" in data:
            data = data["$set"]

        allcond = self.parse_query(query)

        try:
            self.table.update(data, allcond)
        except:
            # todo: exception too broad
            return False

        # todo: return result with result.matched_count and result.modified_count
        return True

    def find(self, query=None):
        if self.table is None:
{'fraction_non_alphanumeric': 0.10533021873009131, 'fraction_numerical': 0.0014865151836908049, 'mean_word_length': 2.0073435504469987, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '2322554', 'n_tokens_mistral': 2682, 'n_tokens_neox': 2581, 'n_words': 774}