<commit_before>from multipledispatch import dispatch

from pymongo.mongo_client import MongoClient
from io import TextIOBase


@dispatch(TextIOBase)
def close(f):
    """
    Closes stream.
    """
    f.close()


@dispatch(MongoClient)
def close(f):
    """
    Closes connection to MongoDB server.
    """<commit_msg>feat: re-implement get_db in module backend<commit_after>from multipledispatch import dispatch

from pymongo.mongo_client import MongoClient
from io import TextIOBase


@dispatch(TextIOBase)
def close(f):
##############################################################################

def get_db(uri, connections):
    """
    Retrieves database from connection string.  The connection string
    follows one of the formats:
    'mongodb://[username][[:[password]]@]<host>/<db_name>'
    or 'file://<path>'
    or simply '<path>'.
    """

    if uri.startswith('mongodb://'):
        db_name_pos = uri.rfind("/") + 1
        client      = MongoClient(uri[:db_name_pos])
        db          = client[uri[db_name_pos:]]
    else:
        if uri.startswith("file://"):
            uri = uri[len("file://"):]
        client      = uri
        db          = uri

    try:
        connections.index(client)
    except Exception:
        connections.append(client)

    return connections, db



@dispatch(MongoClient)
def close(f):
    """
    Closes connection to MongoDB server.
    """
{'fraction_non_alphanumeric': 0.1718972895863053, 'fraction_numerical': 0.0007132667617689016, 'mean_word_length': 2.9971509971509973, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '1594984', 'n_tokens_mistral': 427, 'n_tokens_neox': 411, 'n_words': 102}