'AttributeError: 'AnimalShelter' object has no attribute 'read'
I am getting this weird error "AttributeError: 'AnimalShelter' object has no attribute 'read'". It looks like the file is completely ignoring that I defined read in the Mod4.py file. I've attached both files. Please let me know if you notice something I don't. I am using Jupyter and Jupyter Notebook.
My error message
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-92-a045b9e49a08> in <module>
27
28 # call the read method
---> 29 results = assignment.read({"type":"dog"})
30 print(results)
AttributeError: 'AnimalShelter' object has no attribute 'read'
My Mod4Test.ipynb
from Mod4 import AnimalShelter
assignment = AnimalShelter()
animal_data = [
{"name":"Ruby",
"type":"dog"},
{"name":"Max",
"type":"dog"},
{"name":"Penguin",
"type":"cat"}
]
#for i in animal_data:
# a.create(i)
#data = a.read({"type":"dog"})
#for dog in data:
# print(dog)
criteria = {"type":"dog"}
# instantiate an object of AnimalShelter class
# call the create method
success = assignment.create(animal_data)
print(success)
# call the read method
results = assignment.read({"type":"dog"})
print(results)
and here is my Mod4.py file
from pymongo import MongoClient
from bson.objectid import ObjectId
class AnimalShelter(object):
""" CRUD operations for Animal collection in MongoDB """
def __init__(self):
# Initializing the MongoClient. This helps to
# access the MongoDB databases and collections.
self.client = MongoClient('mongodb://localhost:28369')
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 \n",
if insert!=0:
return True
else:
return False
else:
raise Exception("Nothing to save, because data parameter is empty")
# Create method to implement the R in CRUD.
def read(self, criteria=None):
# criteria is not None then this find will return all rows which matches the criteria
if criteria:
# {'_id':False} just omits the unique ID of each row
data = self.database.animals.find(criteria,{"_id":False})
else:
#if there is no search criteria then all the rows will be return
data = self.database.animals.find({} , {"_id":False})
return data
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
