Select access to web.py database

Recently, I have been messing around with web.py and wanted to take something from db and return the Storage object to me. The code I use to call my information is:

db = web.database(dbn='sqlite', db='sqlfile.sqlite')
sely = db.select('carp', order="id ASC")

when sely starts, it displays the text like this:

<Storage {'lvl': 0, 'symbol': u'formb', 'logged': u'false', 'id': 1, 'display': u'Classic'}>

when you print, the storage line comes out. How can I get a dictionary from this object?

+5
source share
3 answers

A common Python trick for working with unknown APIs is to use dirbuiltin . Try dir(sely)in the interpreter to find out which variables and member functions are defined for the object you receive.

  • - __iter__, list(sely) , , .
  • - __getitem__, .

, , sely web.utils.IterBetter ( 0 , 3 ). ( , ).

+4
db = web.database(dbn='sqlite', db='sqlfile.sqlite')
sely = db.select('carp', order="id ASC").list()

sely list , storage dict, obj.key obj["key"]. dict(obj) storage dict.

+3

return list(db.select('top',what='score',where="name = $id",vars=locals())

. .

ubuntu

db.select('top',what='score',where="name = $id",vars=locals())[0]["score"]

, , .

-1

All Articles