Reputation: 6891
I just ran this simple code snippet provided by the wiki, because I couldn't get sessions working:
import web
web.config.debug = False
urls = (
"/count", "count",
"/reset", "reset"
)
app = web.application(urls, locals())
session = web.session.Session(app, web.session.DiskStore('sessions'), initializer={'count': 0})
class count:
def GET(self):
session.count += 1
return str(session.count)
class reset:
def GET(self):
session.kill()
return ""
if __name__ == "__main__":
app.run()
But it results in this error:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/web/application.py", line 237, in process
return self.handle()
File "/usr/local/lib/python2.7/dist-packages/web/application.py", line 228, in handle
return self._delegate(fn, self.fvars, args)
File "/usr/local/lib/python2.7/dist-packages/web/application.py", line 411, in _delegate
return handle_class(cls)
File "/usr/local/lib/python2.7/dist-packages/web/application.py", line 387, in handle_class
return tocall(*args)
File "temp.py", line 12, in GET
session.count += 1
File "/usr/local/lib/python2.7/dist-packages/web/session.py", line 71, in __getattr__
return getattr(self._data, name)
AttributeError: 'ThreadedDict' object has no attribute 'count'
Is webpy not compatible with 2.7.3? I'm running this on the internal webserver of webpy. I'm using Ubuntu 12.04.
Upvotes: 2
Views: 4327
Reputation: 2614
The problem is the python version. I had the same problem and I solved it when I executed 2.7 version of python. Just doing > python2.7 code.py and the sessions works perfectly. It's a pity that the doc to web.py is very poor.
Upvotes: 0
Reputation: 2717
Until my pull request get accepted on GitHub, i post the snippet of code illustrating the use of a simple incrementor:
import web
import shelve
urls = (
'/add', 'counter',
'/reset', 'reset'
)
shelf = shelve.open('session')
shelfStore = web.session.ShelfStore(shelf)
app = web.application(urls, globals())
s = web.session.Session(app, shelfStore)
class counter:
def GET(self):
numberToAdd = web.input().get('number')
if not numberToAdd:
numberToAdd = 1
try:
print numberToAdd
s.store.shelf["count"] += int(numberToAdd)
except Exception:
s.store.shelf["count"] = 1
return s.store.shelf.get("count")
class reset:
def GET(self):
s.store.shelf.clear()
if __name__ == "__main__":
app.run()
Upvotes: 0
Reputation: 2717
Ok for the trick of try...except also i'm not convinced it's the best way to do so (not clean at all). Like previously said, Session constructor offer a way to initialize the session's variable.
I'm not really sure we can rely on the "very simple session simple example".
First, we hardly have any explication on the purpose of various variablse. For instance, what is the purpose the *db_parameter* dict ?
Last but not least, it needs a serious update. The provided code simply didn't work with the actual framework. There is simply no web.ctx.session.
By the way, I implemented a simple counter like in the example. The displayed error you had is due to a drastic change in the Session's API. You cannot just call "counter" from your session. That would be more smthg like that: session.store.store_instance.get('counter') .Where store_instance is either a shelf or a db. Like i said, the official documentation needs a serious update.
That said, I noticed that this is not the same for the docstring. To progress i start Ipython and i see every posibilities I have. I know it's pure guessing but naming is good so we can figure out what to do.
I will submit my example to the team of web.py so they can update the official doc.
Upvotes: 0
Reputation: 318498
session.count += 1
is equal to session.count = session.count + 1
so session.count
must exist for this to work.
Add the following check to make it work:
if 'count' not in session:
session.count = 0
session.count += 1
There is also another way which is even shown in the very simple session simple example of the docs:
try:
s.click += 1
except AttributeError:
s.click = 1
Upvotes: 3