我正在尝试为我正在处理的网站加载天气插件。天气插件是一个单独的weather.py文件,位于/var/www/piss/plugins/base/weather.py。在PSP中它似乎正确导入,但我无法从PSP中的weather.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>"
答案 0 :(得分:0)
PSP在Python世界中并不是特别受欢迎,并且有充分的理由。几乎任何其他模板系统都是更好的选择。自从我查看PSP已经很长时间了,我可能会错误地了解它是如何工作的,但我不确定您是否希望PSP知道pwd
,html1
和currentWeather
来自您从weather
模块导入的weather
类。我也不确定班级中的print
陈述是否正在做你认为他们正在做的事情,因为他们是在课堂上,而不是方法。
总而言之,我建议你选择一个更健全的模板系统。 Django的模板,Mako,Jinja,除了PSP之外的所有东西。