Home > Mobile >  How do I get my Jupyter Notebook to stop repeating the code into the output?
How do I get my Jupyter Notebook to stop repeating the code into the output?

Time:10-17

How do I fix my Jupyter Notebook to stop running the code so many times? I should only be getting three outputs but instead I am getting more than that and idk how to fix it!? It is running data from a previous code as well and I am not sure why?

The AnimalShelter.py code

import pymongo

from pymongo import MongoClient
from bson.objectid import ObjectId

class AnimalShelter(object):
    """ CRUD operations for Animal collection in MongoDB """

    def __init__(self, username, password):
        #Initializing the MongoClient. This helps to access the MongoDB databases and collections.
        self.client = MongoClient('mongodb://%s:%s@localhost:45344' % (username, password))
        #where xxxx is your unique port number
        self.database = self.client['AAC']
    
#Complete this create method to implement the C in CRUD.
    def create(self, data):
        if data is not None:
            insert = self.database.animals.insert(data) #data should be dictionary
        
        else:
            raise Exception("Nothing to save, because data parameter is empty")
            
#Create method to implement the R in CRUD.
    def read(self, searchData):
        if searchData:
            data = self.database.animals.find(searchData, {"_id": False})
        
        else:
            data = self.database.animals.find({}, {"_id": False})
        return data

#Create method to implement U in CRUD.
    def update(self, searchData, updateData):
        if searchData is not None:
            result = self.database.animals.update_many(searchData, {"$set": updateData})
        else:
            return "{}"
        return result.raw_result

#Create method to implement D in CRUD.
    def delete(self, deleteData):
        if deleteData is not None:
            result = self.database.animals.delete_many(deleteData)
        
        else:
            return "{}"
        return result.raw_result

My ipnyb code:

from AnimalShelter import AnimalShelter

a = AnimalShelter("aacuser","French")
animal_data = [
    {
        "name":"Hades",
        "type":"dog"
    },
    {
        "name":"Fable",
        "type":"cat"
    },
    {
        "name":"Buddy",
        "type":"dog"
    }
]

for i in animal_data:
    a.create(i)
    
dogs = a.read( {"type":"dog"} )
for dog in dogs:
    print(dog)

cats = a.read( {"type":"cat"} )
for cat in cats:
    print(cat)

The output:

{'name': 'bruno', 'type': 'dog'}
{'name': 'sticky', 'type': 'dog'}
{'name': 'bruno', 'type': 'dog'}
{'name': 'sticky', 'type': 'dog'}
{'name': 'bruno', 'type': 'dog'}
{'name': 'sticky', 'type': 'dog'}
{'name': 'bruno', 'type': 'dog'}
{'name': 'sticky', 'type': 'dog'}
{'name': 'bruno', 'type': 'dog'}
{'name': 'sticky', 'type': 'dog'}
{'name': 'bruno', 'type': 'dog'}
{'name': 'sticky', 'type': 'dog'}
{'name': 'bruno', 'type': 'dog'}
{'name': 'sticky', 'type': 'dog'}
{'name': 'bruno', 'type': 'dog'}
{'name': 'sticky', 'type': 'dog'}
{'name': 'bruno', 'type': 'dog'}
{'name': 'sticky', 'type': 'dog'}
{'name': 'bruno', 'type': 'dog'}
{'name': 'sticky', 'type': 'dog'}
{'name': 'bruno', 'type': 'dog'}
{'name': 'sticky', 'type': 'dog'}
{'name': 'Hades', 'type': 'dog'}
{'name': 'Buddy', 'type': 'dog'}
{'name': 'Hades', 'type': 'dog'}
{'name': 'Buddy', 'type': 'dog'}
{'name': 'Hades', 'type': 'dog'}
{'name': 'Buddy', 'type': 'dog'}
{'name': 'missy', 'type': 'cat'}
{'name': 'missy', 'type': 'cat'}
{'name': 'missy', 'type': 'cat'}
{'name': 'missy', 'type': 'cat'}
{'name': 'missy', 'type': 'cat'}
{'name': 'missy', 'type': 'cat'}
{'name': 'missy', 'type': 'cat'}
{'name': 'missy', 'type': 'cat'}
{'name': 'missy', 'type': 'cat'}
{'name': 'missy', 'type': 'cat'}
{'name': 'missy', 'type': 'cat'}
{'name': 'Fable', 'type': 'cat'}
{'name': 'Fable', 'type': 'cat'}
{'name': 'Fable', 'type': 'cat'}

I have tried to restart the entire thing, made a new notebook to work out of, and cleared the outputs/reset the kernel/ran the program again and each time it adds another listing instead of it showing the three listings. Is this a bug or did I do something wrong?

CodePudding user response:

MongoDB is a persistent database; so each create() adds more data in to the database.

You already have a delete method so you could add something like:

a = AnimalShelter("aacuser","French")
a.delete({"type":"dog"})
a.delete({"type":"cat"})

near the start of your code to delete any existing data before you start.

  • Related