2016-05-01 58 views
0

我一直在努力做一個氣象站,我希望能夠自動將當前天氣發佈到Twitter。到目前爲止,我已經能夠很容易地發佈正規的字符串,如t.statuses.update(status= 'twitter post!') 但每當我嘗試發佈一個變量,例如當前的溫度,我得到這個錯誤:用twitter api調用變量

Traceback (most recent call last): File "/home/pi/Desktop/Python2Projects/MAIN.py", line 79, in t.statuses.update (status= 'Current temperature in dowd house: %d F \n Windspeed: %d mph' %(temp, vmph)) AttributeError: 'int' object has no attribute 'statuses'

這裏是我的代碼,到目前爲止,在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

因爲您分配t的值5在這裏你得到這個問題:

#time in seconds you want sensor to collect data for average speed 
t = 5 

這一點後,你嘗試做t.statues當然這是行不通的,但由於t是一個整數,不是對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,它有關於如何格式化代碼的一些指導。

+0

非常感謝!我無法表達我多麼感激。這是我的第一個編碼項目,所以我正在努力學習。也感謝給我格式化的鏈接。我之前對此知之甚少,但現在從現在開始實施所有這些。非常感謝。 –