<commit_before>from pymongo.son_manipulator import AutoReference
from pymongo.son_manipulator import NamespaceInjector
from pymongo.connection import Connection

import logging

log = logging.getLogger("citools.mongo")

def get_mongo_and_database_connections(hostname=None, port=None, database=None, username=None, password=None):
    connection = Connection(hostname, port)

    database = connection[database]

    database.add_son_manipulator(NamespaceInjector())
    database.add_son_manipulator(AutoReference(database))

    if username or password:
        auth = database.authenticate(username, password)
        if auth is not True:
            log.msg("FATAL: Not connected to Mongo Database, authentication failed")
            raise AssertionError("Not authenticated to use selected database")

    return (database, connection)


def get_database_connection(hostname=None, port=None, database=None, username=None, password=None):
    return get_mongo_and_database_connections(hostname=hostname, port=port, database=database, username=username, password=password)[0]
<commit_msg>Retype mongo port as int (checked by mongo instead of duck-typed)<commit_after>from pymongo.son_manipulator import AutoReference
from pymongo.son_manipulator import NamespaceInjector
from pymongo.connection import Connection

import logging

log = logging.getLogger("citools.mongo")

def get_mongo_and_database_connections(hostname=None, port=27017, database=None, username=None, password=None):
    connection = Connection(hostname, int(port))

    database = connection[database]

    database.add_son_manipulator(NamespaceInjector())
    database.add_son_manipulator(AutoReference(database))

    if username or password:
        auth = database.authenticate(username, password)
        if auth is not True:
            log.msg("FATAL: Not connected to Mongo Database, authentication failed")
            raise AssertionError("Not authenticated to use selected database")

    return (database, connection)


def get_database_connection(hostname=None, port=None, database=None, username=None, password=None):
    return get_mongo_and_database_connections(hostname=hostname, port=port, database=database, username=username, password=password)[0]

{'fraction_non_alphanumeric': 0.08087578194816801, 'fraction_numerical': 0.003127792672028597, 'mean_word_length': 5.703592814371257, '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': '4776004', 'n_tokens_mistral': 617, 'n_tokens_neox': 606, 'n_words': 152}