<commit_before>import pymongo
from IPython.core.magic import Magics, magics_class, line_magic

@magics_class
class MongoDB(Magics):
    _conn = None
    @line_magic('mongo_connect')
    def conn(self, line, host="localhost",  port=27017):
        self._conn = pymongo.connection.Connection(host, port)
        return self._conn

    @line_magic('show_dbs')
    def show_dbs(self, line):
        if self._conn:
            return self._conn.database_names()
        else:
            print "please connect to mongodb using %mongo_connect"
            return line

def load_ipython_extension(ipython):
    ipython.register_magics(MongoDB)<commit_msg> close #1 show dbs  close #2 show collections<commit_after>from pymongo import MongoClient
from pymongo.database import Database
from IPython.core.magic import Magics, magics_class, line_cell_magic, line_magic

@magics_class
class MongoDB(Magics):
    _conn = None
    @line_magic('mongo_connect')
    def conn(self, line):
        if line:
            uri = line
        else:
            uri = 'mongodb://localhost:27017'
        self._conn = MongoClient(uri)
        return self._conn

    @line_magic('show_dbs')
    def show_dbs(self, line):
        if self._conn:
            return self._conn.database_names()
        else:
            print "[ERROR] please connect to mongodb using %mongo_connect"
            return line

    @line_magic('show_collections')
    def show_collections(self, line):
        if self._conn and line:
            collections = Database(self._conn, line).collection_names()
        if not line:
            return "[ERROR] usage %show_collections <dbname>"
        if not self._conn:
            return "[ERROR] connect mongodb before %show_collections"
        if not collections:
            print "[ERROR] check your database name there no collection"
            collections = self.show_dbs(self)
        return collections

def load_ipython_extension(ipython):
    ipython.register_magics(MongoDB)
{'fraction_non_alphanumeric': 0.08564231738035265, 'fraction_numerical': 0.006045340050377834, 'mean_word_length': 2.7613636363636362, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, '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': '2159573', 'n_tokens_mistral': 612, 'n_tokens_neox': 584, 'n_words': 153}