gpt4 book ai didi

python - 属性错误: 'Rules' object has no attribute '_Rules__temperature'

转载 作者:太空宇宙 更新时间:2023-11-03 18:27:52 25 4
gpt4 key购买 nike

我有一个程序,旨在从传感器获取输入,然后根据在类中存储和调用的一些预定义规则对这些传感器的输入采取行动。我在将传感器定义的变量传递到规则类中时遇到问题。我收到错误“规则对象没有属性。规则_温度”。我的代码如下:

class myInterfaceKit():
__interfaceKit = None

def __init__(self ):
try:
self.__interfaceKit = InterfaceKit()
except RuntimeError as e:
print("Runtime Exception: %s" % e.details)
print("Exiting....")
exit(1)

def open(self):
try:
self.__interfaceKit.openPhidget()
except PhidgetException as e:
print("Phidget Exception %i: %s" % (e.code, e.details))

def getInterfaceKit(self):
return self.__interfaceKit

def getSensor(self, sensor):
return self.__interfaceKit.getSensorValue(sensor)


class sensors():

__sound = 0
__temperature = 0
__light = 0
__motion = 0

__dbName = ""
__interfaceKit = None
__connection = None
__rules = None

def __init__(self, theInterfaceKit, dbName):
self.__sound = 0
self.__temperature=0
self.__light=0
self.__motion=0
self.__timeStamp=time.time()
self.__strTimeStamp=datetime.datetime.fromtimestamp(self.__timeStamp).strftime('%Y-%m-%d %H:%M:%S')
self.__dbName = dbName
self.__interfaceKit = theInterfaceKit
self.__connection = sqlite3.connect('testing1.db', check_same_thread=False) ######


def interfaceKitAttached(self, e):
attached = e.device
print("InterfaceKit %i Attached!" % (attached.getSerialNum()))

self.__interfaceKit.getInterfaceKit().setSensorChangeTrigger(0, 5)
self.__interfaceKit.getInterfaceKit().setSensorChangeTrigger(1, 35)
self.__interfaceKit.getInterfaceKit().setSensorChangeTrigger(2, 60)
self.__interfaceKit.getInterfaceKit().setSensorChangeTrigger(3, 200)

def sensorInputs(self, e):

temperature = (self.__interfaceKit.getSensor(0)*0.2222 - 61.111)
sound = (16.801 * math.log((self.__interfaceKit.getSensor(1))+ 9.872))
light = (self.__interfaceKit.getSensor(2))
motion = (self.__interfaceKit.getSensor(3))

# check temperature has changed - if yes, save and update
if temperature != self.__temperature:
self.__timeStamp=time.time()
self.__strTimeStamp=datetime.datetime.fromtimestamp(self.__timeStamp).strftime('%Y-%m-%d %H:%M:%S')
self.__temperature = temperature
print("Temperature is: %i:" % self.__temperature)

self.writeToDatabase( 'temperature', self.__temperature, self.__strTimeStamp)


#check sound has changed - if yes, save and update
if sound != self.__sound:
self.__timeStamp=time.time()
self.__strTimeStamp=datetime.datetime.fromtimestamp(self.__timeStamp).strftime('%Y-%m-%d %H:%M:%S')
self.__sound = sound
print("Sound is: %i " % self.__sound)
self.writeToDatabase( 'sound', self.__sound, self.__strTimeStamp )

#check light has changed - if yes, save and update
if light != self.__light:
self.__timeStamp=time.time()
self.__strTimeStamp=datetime.datetime.fromtimestamp(self.__timeStamp).strftime('%Y-%m-%d %H:%M:%S')
self.__light = light
print("Light is: %i " % self.__light)
self.writeToDatabase( 'light', self.__light, self.__strTimeStamp )


if motion != self.__motion:
self.__motion = motion
print ("motion is %i" % self.__motion)


def openDatabase(self):
cursor = self.__connection.cursor()
cursor.execute("CREATE TABLE " + self.__dbName + " (sensor text, value text, time_stamp text)")

def writeToDatabase(self, sensorType, data, time):
cursor = self.__connection.cursor()
cursor.execute("INSERT INTO " + self.__dbName + " VALUES (?, ?, ?)", (sensorType, data, time))
self.__connection.commit()

def closeDatabase():
self.__connection.close()

def start(self):
try:
self.__interfaceKit.getInterfaceKit().setOnAttachHandler(self.interfaceKitAttached)
self.__interfaceKit.getInterfaceKit().setOnSensorChangeHandler(self.sensorInputs)
self.openDatabase()
except PhidgetException as e:
print("Phidget Exception %i: %s" % (e.code, e.details))
print("Exiting....")
exit(1)

这就是问题所在:::

class Rules(sensors):

__tempLower=0
__tempUpper=0


def __init__(self):
self.__tempLower=28
self.__tempUpper=30
self.__temperature

def tempRule(self, sensors): ##Temperature rule
if self.__temperature == self.__tempLower:
print("testing")


phidgetInterface = myInterfaceKit()
phidgetInterface.open()

theSensors = sensors(phidgetInterface, "event156")
theSensors.start()

theRule = Rules()
theRule.tempRule()

chr = sys.stdin.read(1)

编辑,错误消息:

回溯(最近一次调用最后一次): 文件“H:\Project\Student\Prototype 1\eventProto.py”,第 159 行,位于 规则=规则()

文件“H:\Project\Student\Prototype 1\eventProto.py”,第 146 行,init self .__温度AttributeError:“规则”对象没有属性“规则_温度”

最佳答案

私有(private)成员(以__开头)不能被子类访问。 (嗯,可以使用一些黑客技术来访问它们,但不应该)。(参见here)

在您的例子中,__Temperature 是基类sensors 的私有(private)成员,但您是从子类Rules 访问它。

将双下划线前缀替换为单下划线 (__Temperature -> _Temperature),然后就可以开始了。

关于python - 属性错误: 'Rules' object has no attribute '_Rules__temperature' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22880239/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com