2016-05-01 13 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

あなたがここに値5tを割り当てたため、この問題を得ている:tが整数であるので、その点の後

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

、あなたがt.statuesをやろうもちろん、動作しないことと、ツイッター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

ありがとうございました!私は感謝しています。これは私の最初のコーディングプロジェクトなので、私が行くにつれて学びたいと思っています。また、私にフォーマットのリンクを与えてくれてありがとう。私はこれまでにほとんど知りませんでしたが、今からすべてのことを実行し始めています。非常に高く評価。 –

関連する問題