我一直在研究气象站,我希望能够自动将当前天气发布到推特上。到目前为止,我已经能够轻松发布常规字符串,例如t.statuses.update(status= 'twitter post!')
但每当我尝试发布一个变量,例如当前的温度时,我就会收到这个错误:
Traceback(最近一次调用最后一次):文件 “/home/pi/Desktop/Python2Projects/MAIN.py”,第79行,in t.statuses.update(status ='当前房屋温度:%d F \ nWindspeed:%d mph'%(temp,vmph))AttributeError:'int'对象 没有属性'状态'
到目前为止,这是我的代码,twitter帖子位于最底层:
#sets up libraries
from sys import argv
import os
import glob
import subprocess
import RPi.GPIO as GPIO
import time
import datetime
#Sets up twitter library
from twitter import *
access_token = 'secret'
access_token_secret = 'cant tell you'
consumer_key = 'i have to change all these'
consumer_secret = 'they usually have my twitter access keys'
t = Twitter(auth=OAuth(access_token, access_token_secret, consumer_key, consumer_secret))
#sets up GPIO for windspeed Hall effect sensor
GPIO.setmode(GPIO.BCM)
GPIO.setup(27, GPIO.IN)
#sets up GPIO for temperature probe
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
#usus probe to take temperature
def read_temp_raw():
catdata = subprocess.Popen(['cat',device_file], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out,err = catdata.communicate()
out_decode = out.decode('utf-8')
lines = out_decode.split('\n')
return lines
def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
temp_f = temp_c * 9.0 / 5.0 + 32.0
return float(temp_f) #float(temp_c)
temp = read_temp()
#setup for windspeed sensor
timy = datetime.datetime.now()
timx = datetime.datetime.now()
rotations = 0
#radious of windspeed sensor in meters
r = .1
#time in seconds you want sensor to collect data for average speed
t = 5
#main windspeed loop
timeout = time.time() + t
while True:
GPIO.wait_for_edge(27, GPIO.BOTH)
hallActive = GPIO.input(27)
if time.time() > timeout:
break
elif( hallActive == False ):
rotations = rotations + 1
elif( hallActive == True ):
pass
#function that converts rotations/s to mph
vmph = (r*6.28*rotations*2.2369) / t
GPIO.cleanup()
print 'Current temperature: %d F \n Windspeed: %d mph \n' %(temp, vmph)
t.statuses.update (status= 'Current temperature: %d F \n Windspeed: %d mph' %(temp, vmph) )
代码结束
非常感谢任何帮助或建议!非常感谢。
答案 0 :(得分:0)
您遇到此问题是因为您在此处为t
分配了5
:
#time in seconds you want sensor to collect data for average speed
t = 5
在此之后,您尝试执行t.statues
,但当然这不起作用,因为t
是一个整数,而不是对twitter api的引用。
解决此问题的简单方法是在脚本的最顶部更改twitter api句柄的名称:
twitter_api = Twitter(auth=OAuth(access_token,
access_token_secret,
consumer_key, consumer_secret))
然后在底部,相应地调整您的代码:
temp_line = 'Current temperature: %d F \n Windspeed: %d mph \n' %(temp, vmph)
print(temp_line)
twitter_api.statuses.update(status=temp_line)
作为一般规则,尽量避免单个字符命名变量。它们只会给您的代码增加混乱(如本例所示),而且它们会使您的代码在将来难以维护(对于您或其他任何必须维护代码的人来说)。
Python提供了一个很好的样式指南,称为PEP-8,它有一些关于如何格式化代码的指南。