简体   繁体   English

AttributeError: 'AnimalShelter' object 没有属性 'database'

[英]AttributeError: 'AnimalShelter' object has no attribute 'database'

I am not understanding why I am receiving this error as my.py file indicates that database is in fact an attribute.我不明白为什么我会收到此错误,因为 my.py 文件表明数据库实际上是一个属性。 I have made sure that everything is indented as it should be and made sure that the correct.py is notated when importing AnimalShelter.我已确保所有内容都按应有的方式缩进,并确保在导入 AnimalShelter 时注明了正确的.py。 I am following a walkthrough for this for class and the.ipynb definitely details everything as is it is in the walkthrough so there has to be something wrong with the.py file.我正在关注 class 的演练,而.ipynb 肯定会详细说明演练中的所有内容,因此 .py 文件肯定有问题。 I just dont understand what...我只是不明白什么...

from jupyter_plotly_dash import JupyterDash
import dash
import dash_leaflet as dl
import dash_core_components as dcc
import dash_html_components as html
import plotly.express as px
import dash_table
from dash.dependencies import Input, Output
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from pymongo import MongoClient
#### FIX ME #####
# change animal_shelter and AnimalShelter to match your CRUD Python module file name and class name
from AAC import AnimalShelter

###########################
# Data Manipulation / Model
###########################
# FIX ME update with your username and password and CRUD Python module name
username = "aacuser"
password = "monogoadmin"
shelter = AnimalShelter(username, password)
# class read method must support return of cursor object and accept projection json input
df = pd.DataFrame.from_records(shelter.read({}))

print (df)
import pymongo
from pymongo import MongoClient
from bson.objectid import ObjectId

class AnimalShelter(object):
    """CRUD operations for Animal collection in Mongodatabase"""
    #Initializes MongoClient
    def _init_(self, username, password):
        self.client = MongoClient('mongodb://127.0.0.1:38574' % (username, password))
        self.database = self.client['project']  
    #Implement create method
    def create(self, data):
        if data is not None:
            return self.database.animals.insert_one(data)
        else:
            raise Exception("Nothing to save, because data parameter is empty")
    #Implement read method
    def read(self, data):
        if data is not None:
            return self.database.animals.find(data)
        else:
            raise Exception("Nothing to read, because data parameter is empty")
    #Implement update method
    def update(self, data):
        if find is not None:
            return self.database.animals.update_one(data)
        else:
            raise Exception("Nothing to update, because data parameter is empty")
    #Implement delete method
    def delete(self, data):
        if data is not None:
            return self.database.animals.delete_one(data)
        else:
            raise Exception("Nothing to delete, because data parameter is empty")
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-11-2eb0b77f93e5> in <module>
     23 shelter = AnimalShelter()
     24 # class read method must support return of cursor object and accept projection json input
---> 25 df = pd.DataFrame.from_records(shelter.read({}))
     26 
     27 print (df)

~/Desktop/AAC.py in read(self, data)
     18 def read(self, data):
     19         if data is not None:
---> 20                 return self.database.animals.find(data)
     21         else:
     22                 raise Exception("Nothing to read, because data parameter is empty")

AttributeError: 'AnimalShelter' object has no attribute 'database'

You need to change _init_ to __init__ :您需要将_init_更改为__init__

Change改变

#Initializes MongoClient
def _init_(self, username, password):
    self.client = MongoClient('mongodb://127.0.0.1:38574' % (username, password))
    self.database = self.client['project']

to

#Initializes MongoClient
def __init__(self, username, password):
    self.client = MongoClient('mongodb://127.0.0.1:38574' % (username, password))
    self.database = self.client['project']  

When you call AnimalShelter(...) , Python tries to call __init__ if it exists, and if it doesn't, it uses a default implementation.当您调用AnimalShelter(...)时,Python 会尝试调用__init__ (如果存在),如果不存在,则使用默认实现。 It doesn't automatically call _init_ .它不会自动调用_init_

Therefore, self.database = self.client['project'] is never executed, so AnimalShelter doesn't have a database attribute, which is the cause of your error.因此, self.database = self.client['project']永远不会执行,因此AnimalShelter没有database属性,这是您错误的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM