<commit_before>import pymongo
from ..utils import current_time


class PanMongo(object):

    """ Connection to the running MongoDB instance

    This is a collection of parameters that are initialized when the unit
    starts and can be read and updated as the project is running. The server
    is a wrapper around a mongodb collection.
    """

    def __init__(self, host='localhost', port=27017):
        # Get the mongo client
        self._client = pymongo.MongoClient(host, port)

        collections = [
            'camera',
            'config',
            'current',
            'environment',
            'images',
            'mount',
            'visits',
            'state',
            'target',
            'weather',
        ]

        # Setup static connections to the collections we want
        for collection in collections:
            # Add the collection as an attribute
            setattr(self, collection, getattr(self._client.panoptes, 'panoptes.{}'.format(collection)))

    def insert_current(self, collection, obj):

        col = getattr(self, collection)

        current_obj = {
            'type': collection,
            'data': obj,
            'date': current_time(utcnow=True),
        }

        # Update `current` record
        self.current.replace_one({'type': collection}, current_obj, True)

        # Insert record into db
        col.insert_one(current_obj)

    def get_param(self, key=None):
        """ Gets a value from the param server.

        Args:
                key: name of parameter.

        Returns:
                A value for the named parameter. This can be any object that
                is stored in a dict. If no key is specified, warning is given
                and nothing is returned.
        """

        val = None

        if key is not None:
            param = self._db.param_server.find_one({key: {'$exists': True}})
            val = param.get(key)

        return val
<commit_msg>Don't connect to mongo on client creation<commit_after>import pymongo
from ..utils import current_time


class PanMongo(object):

    """ Connection to the running MongoDB instance

    This is a collection of parameters that are initialized when the unit
    starts and can be read and updated as the project is running. The server
    is a wrapper around a mongodb collection.
    """

    def __init__(self, host='localhost', port=27017, connect=False):
        # Get the mongo client
        self._client = pymongo.MongoClient(host, port, connect=connect)

        collections = [
            'camera',
            'config',
            'current',
            'environment',
            'images',
            'mount',
            'visits',
            'state',
            'target',
            'weather',
        ]

        # Setup static connections to the collections we want
        for collection in collections:
            # Add the collection as an attribute
            setattr(self, collection, getattr(self._client.panoptes, 'panoptes.{}'.format(collection)))

    def insert_current(self, collection, obj):

        col = getattr(self, collection)

        current_obj = {
            'type': collection,
            'data': obj,
            'date': current_time(utcnow=True),
        }

        # Update `current` record
        self.current.replace_one({'type': collection}, current_obj, True)

        # Insert record into db
        col.insert_one(current_obj)

    def get_param(self, key=None):
        """ Gets a value from the param server.

        Args:
                key: name of parameter.

        Returns:
                A value for the named parameter. This can be any object that
                is stored in a dict. If no key is specified, warning is given
                and nothing is returned.
        """

        val = None

        if key is not None:
            param = self._db.param_server.find_one({key: {'$exists': True}})
            val = param.get(key)

        return val

{'fraction_non_alphanumeric': 0.08696741854636592, 'fraction_numerical': 0.002506265664160401, 'mean_word_length': 1.8446186742694226, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4862003', 'n_tokens_mistral': 1050, 'n_tokens_neox': 1000, 'n_words': 362}