我尝试解决“艰难学习Python”一书中的练习52,但我遇到了一定的错误。
这是一个使用web.py模块的练习。
当我使用“python bin / app.py”加载文件并使用浏览器查找页面时出现以下错误:
public void refresh(ArrayList<Customer> customers){
this.customers = customers;
notifyDataSetChanged();
}
你能帮我找出这个错误发生的原因吗?
似乎这一行
AttributeError: 'ThreadedDict' object has no attribute 'room'
不会正确编辑会话文件。我解码了会话文件,得到了以下内容:
session.room = map.START
文件结构如下:
>>> x = base64.b64decode(open("sessions/15efb89316fa05f7c77e614ab1ddb70e918b9af3").read())
>>> pickle.loads(x)
{'ip': u'127.0.0.1', 'room': None, 'session_id': '15efb89316fa05f7c77e614ab1ddb70e918b9af3'}
app.py
.
├── bin
│ ├── app_old.py
│ ├── app.py
│ ├── app.pyc
│ ├── __init__.py
│ ├── __init__.pyc
│ └── session_sample.py
├── docs
├── gothonweb
│ └── __init__.pyc
├── __init__.py
├── __init__.pyc
├── map.py
├── map.pyc
├── sessions
│ └── 15efb89316fa05f7c77e614ab1ddb70e918b9af3
├── static
│ └── main.css
├── templates
│ ├── layout.html
│ ├── show_room.html
│ └── you_died.html
└── tests
├── app_tests.py
├── app_tests.pyc
├── __init__.py
├── __init__.pyc
├── map_tests.py
├── map_tests.pyc
├── tools.py
└── tools.pyc
map.py
import web
import map
urls = (
'/game', 'GameEngine',
'/', 'Index',
)
app = web.application(urls, globals())
# little hack so that debug mode works with sessions
if web.config.get('_session') is None:
store = web.session.DiskStore('sessions')
session = web.session.Session(app, store, initializer={'room': None})
else:
session = web.config._session
render = web.template.render('templates/', base="layout")
class Index(object):
def GET(self):
# this is used to "setup" the session with starting values
session.room = map.START
print "session room var:" + str(session.room)
web.seeother("/game")
class GameEngine(object):
def GET(self):
if session.room:
return render.show_room(room=session.room)
else:
#why is there here? do you need it?
return render.you_died()
def POST(self):
form = web.input(action=None)
# there is a bug here, can you fix it?
if session.room and form.action:
session.room = session.room.go(form.action)
web.seeother("/game")
if __name__ == "__main__":
app.run()