2010-05-03 30 views
0

我正在嘗試爲我正在處理的網站加載天氣插件。天氣插件是一個單獨的weather.py文件,位於/var/www/piss/plugins/base/weather.py。在PSP中,它似乎正確導入,但我無法訪問PSP中weather.py插件的任何變量或對象。下面是我的代碼:無法訪問.PSP中導入的.PY部分

...HTML and CSS stuff... 

<% 
sys.path.append('/var/www/piss/plugins/base/') 
pwd = os.getcwd() 
import sys, os 
import string 
from weather import weather 
%> 
      <%= pwd %> 
      <%= html1 %> 
      <%= currentWeather %> 
      </div> 
     </div> 
     <div id="footer">Piss + INK Version      0.00001</div> 
     <div id="bottom"></div> 
    </div> 
</body> 
</html> 

這裏的weather.py代碼:

from basePlugin import plugin 
import MySQLdb 
import pywapi 

class weather(plugin): 
    """ 
    weather.py 
    Built-in weather plugin. 
    """ 
    def __init__(self,zipcode): 
     self.zipcode = zipcode 

#Tested without DB access 
    #db=_mysql.connect(host="localhost",user="things",passwd="things",db="things") 
    #zip="SELECT zipcode FROM "+currentUser+"\;" 
    #c = db.cursor() 
    #dbResult = c.execute (zip) 
    wResult=pywapi.get_weather_from_google('05401') 
    html1=""" 
<div class="weather"> 
Weather:<br /> 
""" 
    print html1 
    currently = wResult['current_conditions'] 
    currentWeather = currently['condition'] 
    print currentWeather 
    print "<br />" 

    if currentWeather == "Mostly Cloudy": 
     print '<img src="themes/default/images/weather/cloudy.png" alt="cloudy">' 

    if currentWeather == "Cloudy": 
     print '<img src="themes/default/images/weather/cloudy.png" alt="cloudy">' 

    if currentWeather == "Sunny": 
     print '<img src="themes/default/images/weather/sunny.png" alt="sunny">' 

    if currentWeather == "Showers": 
     print '<img src="themes/default/images/weather/rain.png" alt="rain">' 

#More conditions will be added in actual plugin. 

print "</div>" 
+1

ugh,PSP(* vomits *) – nosklo 2010-05-03 11:33:16

回答

0

PSP是不是在Python世界中特別受歡迎,並有很好的理由。幾乎任何其他模板系統都是更好的選擇。自從我看PSP以來,這已經很長時間了,我可能會誤解它的工作原理,但我不確定你期望PSP知道pwd,html1currentWeather來自weather模塊導入的weather類。我也不確定課堂上的print聲明是否在做你認爲他們正在做的事情,因爲他們在班級正文中,而不是方法。

總而言之,我建議你選擇一個更理想的模板系統。 Django的模板,Mako,Jinja,除PSP以外的任何內容。

+0

是的,我期望html1和currentWeather在天氣插件導入時可用。打印語句僅用於測試模塊。 – chris 2010-05-03 22:55:22

+0

問題是*爲什麼*你期待這一點。 PSP如何知道'currentWeather'和'html1'的含義是從'weather'模塊的'weather'類的屬性中獲取的? – 2010-05-04 00:09:42

相關問題