2014-04-05 40 views
1

我有一个程序,旨在从传感器获取输入,然后根据这些传感器的输入从一些预定义的规则中存储并在类中调用。我遇到了传感器被定义为规则类的变量的问题。我在哪里得到'规则对象没有属性的错误。 规则 _temperature。我的代码如下:AttributeError:'规则'对象没有属性'_Rules__temperature'

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行,在 theRule = Rules()

文件”H:\ Project \ Student \ Prototype 1 \ eventProto.py“,第146行,在初始化 自.__温度 AttributeError的:“规则”对象有没有属性“规则 _temperature”

+0

请正确格式化您的代码,并包含错误日志 –

+1

您能否解释为什么您使用双引号下划线? – Matthias

回答

-1

双下划线的成员被认为是私人的,你必须预先考虑那些类名定义所述构件。

使用这些神奇的私有成员这样的:

class sensors: 
    __temp = 42 

assert sensors()._sensors__temp == 42 

class Rules(sensors): 
    def test(self): 
     assert self._sensors__temp == 42 

Rules().test() 
+1

[PEP-8](http://legacy.python.org/dev/peps/pep-0008/#method-names-and-instance-variables)说:“通常,双引号下划线应该只用于避免名称与设计为子类的类中的属性冲突。“ – Matthias

2

私有成员(从__)不能由子类访问。 (好吧,他们可以使用一些hackery访问,但不应该)。 (见here

在你的情况,__temperature是基类sensors的私有成员,但你从子类Rules访问它。

用一个下划线替换双下划线前缀(__temperature - >_temperature),你很好。

相关问题