저는 원 와이어 센서에서 온도를 가져 와서 온도를 재 지정하기 위해 스크립트 작업을하고 있습니다. 이 스크립트는 alias1.py라는 파일에서 단선 장치 목록을 가져옵니다.이 장치는 친숙한 이름과 단선 버스 센서 주소 쌍을 포함하고 차례로 온도를받습니다. 그러나 센서를 사용할 수없는 경우 예외를 잡아서 단계별로 처리하고 싶습니다. 이 순간에 이것은 6도까지만 설정되지만 나중에 동작을 변경할 수 있습니다. 불행하게도 나는 이것으로 어려움을 겪고 있으며 다른 경우에는 직접적인 평행을 볼 수 없다.가져온 모듈에서 파이썬 예외를 트랩 할 수 없습니다.
#!/usr/bin/python3
import logging
from onewire import Onewire
import redis
from alias1 import sensors
logging.basicConfig(level=logging.DEBUG)
room_float = {}
room_integer = {}
rooms ={}
ow = Onewire("%s:%d" % ("localhost", 4304))
for key in sensors:
roomID = sensors[key]
s = ow.sensor(roomID)
try:
temperature = s.read("temperature")
logging.info("found device %s (type = %s)" % (s.path,s.sensor_type))
except (RuntimeError, TypeError, NameError, AttributeError):
temperature = bytes([54])
logging.info("device or attribute not found %s")
rooms[key] = temperature
float_temp = float(temperature)
int_temp = int(float_temp)
r_server = redis.Redis('localhost')
r_server.hset(key, "temp_now", int_temp)
for 문이 누락 된 센서에 도달하면 스크립트가 무기한 중지되고 Ctrl 키를 치면 문제를 볼 수 있습니다. BTW 내가
File "./one-wire-redis-write2.py", line 17, in <module>
temperature = s.read("temperature")
File "/usr/local/lib/python3.4/dist-packages/onewire/__init__.py", line 85, in read
return self.__getattr__(attr)
File "/usr/local/lib/python3.4/dist-packages/onewire/__init__.py", line 80, in __getattr__
if attr not in self.attrs:
< Above line 80 repeated ad nauseum >
if attr not in self.attrs:
File "/usr/local/lib/python3.4/dist-packages/onewire/__init__.py", line 107, in attrs
self._attrs = self._ow.get(self.path).split(',')
File "/usr/local/lib/python3.4/dist-packages/onewire/__init__.py", line 33, in get
return _ow.get(str(os.path.join(self._path, *path)))
KeyboardInterrupt
가 기본 onewire 모듈의 라인 (80)을보고 절망 : 거기에 꽤 몇 가지 예외 문을 뒀다
https://github.com/kipe/python-onewire/blob/master/onewire/init.py
def __getattr__(self, attr):
if attr not in self.attrs:
raise AttributeError('Attribute "%s" not found in %s.' % (attr, self.__str__()))
return self._ow.get(self.path, attr)
'AtrributeError 제기 것 같다하지만 난 할 수 그것을 잡아라. 센서 주소가 잘못되어 센서에 "온도"속성이 없으므로 존재하지 않으므로 예외를 발생시켜야합니다.이 오류를 어떻게 볼 수 있으며 예외 또는 유사한 구조로 사용할 수 있습니까?
'raise AttributeError'는 결코 도달하지 않습니다.'self.attrs' 속성이 무한 재귀 나선을 일으키는 것으로 보입니다. 그러나 루프가있는 추적의 조각에서 나는 이해할 수 없습니다. –
다음은 명령 행에서 3 개의 주석으로 분리 된 것입니다. Python 3.4.0 (default, Apr 11, 2014, 13:05:11) [GCC 4.8.2] 리눅스에서 >>>에서 온주 가져 오기 012 디렉토리를 클릭합니다. >>> s = ow.sensor ("28.02CA27060000") >>> 온도 = s.read ("온도") >>> s = ow.sensor ("28.12CA27060000") >>> 온도 = s.read ("온도") –
^가장 최근의 CTraceback 마지막으로 전화) : 파일 "", 줄 1, 파일 "/usr/local/lib/python3.4/dist-packages/onewire/__init__.py"85 번째 줄에 읽음 return self .__ getattr__ (attr) 파일 "/usr/local/lib/python3.4/dist-packages/onewire/__init__.py", 80 행, __getattr__ attr이 self.attrs에없는 경우 : –