One more test:

def getdb(): return db
db.reviews.pos = Field.Lazy(lambda row,db=getdb: 
db()((db().reviews_like.review_id == row.reviews.id) & 
(db().reviews_like.helpful == True)).count() )
db.reviews.neg = Field.Lazy(lambda row,db=getdb: 
db()((db().reviews_like.review_id == row.reviews.id) & 
(db().reviews_like.helpful == False)).count() )

Notice you can also do

db.reviews.pos = Field.Virtual(lambda row: db((db.reviews_like.review_id == 
row.reviews.id) & (db.reviews_like.helpful == True)).count() )
db.reviews.neg = Field.Virtual(lambda row: db((db.reviews_like.review_id == 
row.reviews.id) & (db.reviews_like.helpful == False)).count() )

and use rows.pos instead of rows.pos()

In this case the value is cached, not the callback function.


On Saturday, 1 September 2012 07:40:20 UTC-5, Paolo wrote:
>
> Hi Massimo, 
> I've just tried both strategies you suggested but I got the same 
> behavior; namely the first time it works, the second rises the error. 
>
> Paolo 
>
> 2012/9/1 Massimo Di Pierro <massimo....@gmail.com <javascript:>>: 
> > I do not fully understand this. It mush have something to do with the 
> > scoping of variables in python. 
> > 
> > Can you try replace 
> > 
> > db.reviews.pos = Field.Lazy(lambda row: db((db.reviews_like.review_id == 
> > row.reviews.id) & (db.reviews_like.helpful == True)).count() ) 
> > db.reviews.neg = Field.Lazy(lambda row: db((db.reviews_like.review_id == 
> > row.reviews.id) & (db.reviews_like.helpful == False)).count() ) 
> > 
> > with 
> > 
> > db.reviews.pos = Field.Lazy(lambda row,db=db: 
> db((db.reviews_like.review_id 
> > == row.reviews.id) & (db.reviews_like.helpful == True)).count() ) 
> > db.reviews.neg = Field.Lazy(lambda row,db=db: 
> db((db.reviews_like.review_id 
> > == row.reviews.id) & (db.reviews_like.helpful == False)).count() ) 
> > 
> > or with 
> > 
> > def lazy1(row,db=db): return db((db.reviews_like.review_id == 
> > row.reviews.id) & (db.reviews_like.helpful == True)).count() 
> > def lazy2(row,db=db): return db((db.reviews_like.review_id == 
> > row.reviews.id) & (db.reviews_like.helpful == False)).count() 
> > db.reviews.pos = Field.Lazy(lazy1) 
> > db.reviews.neg = Field.Lazy(lazy2) 
> > 
> > 
> > On Saturday, 1 September 2012 05:50:35 UTC-5, João Saraiva wrote: 
> >> 
> >> Hello, 
> >> 
> >> Could it be something related to Dropbox's sync mechanism? Have you 
> tried 
> >> running your app in another folder outside of Dropbox's reach? Would 
> >> probably explain why your example works fine on other user's machines. 
> >> 
> >> Best regards, 
> >> JS 
> >> 
> >> 
> >> On Saturday, September 1, 2012 6:34:39 AM UTC+1, Paolo wrote: 
> >>> 
> >>> Hi Massimo,  the error is: Cannot operate on a closed database. 
> >>> ticket: 
> >>> Traceback (most recent call last): 
> >>>   File "/home/paolo/Dropbox/git/pp/web2py/gluon/restricted.py", line 
> >>> 209, in restricted 
> >>>     exec ccode in environment 
> >>>   File 
> >>> 
> "/home/paolo/Dropbox/git/pp/web2py/applications/test_vf/views/default/test.html",
>  
>
> >>> line 3, in <module> 
> >>>     {{pass}} 
> >>>   File "/home/paolo/Dropbox/git/pp/web2py/gluon/dal.py", line 8825, in 
> >>> __call__ 
> >>>     return self.method(self.row,*args,**kwargs) 
> >>>   File 
> >>> "/home/paolo/Dropbox/git/pp/web2py/applications/test_vf/models/db.py", 
> >>> line 129, in <lambda> 
> >>>     db.reviews_s.pos = Field.Lazy(lambda row: 
> >>> db((db.reviews_like_s.review_id == row.reviews_s.id) & 
> >>> (db.reviews_like_s.helpful == True)).count() ) 
> >>>   File "/home/paolo/Dropbox/git/pp/web2py/gluon/dal.py", line 8690, in 
> >>> count 
> >>>     return db._adapter.count(self.query,distinct) 
> >>>   File "/home/paolo/Dropbox/git/pp/web2py/gluon/dal.py", line 1597, in 
> >>> count 
> >>>     self.execute(self._count(query, distinct)) 
> >>>   File "/home/paolo/Dropbox/git/pp/web2py/gluon/dal.py", line 1653, in 
> >>> execute 
> >>>     return self.log_execute(*a, **b) 
> >>>   File "/home/paolo/Dropbox/git/pp/web2py/gluon/dal.py", line 1647, in 
> >>> log_execute 
> >>>     ret = self.cursor.execute(*a, **b) 
> >>> ProgrammingError: Cannot operate on a closed database. 
> >>> 
> >>> I put the code of the simple app in a new app with the latest web2py, 
> >>> the first time I accessed the page all worked well but 
> >>> when I reloaded it, I got the error. Hope this can help you. 
> >>> 
> >>> Best, 
> >>> Paolo 
> >>> 
> >>> 
> >>> 
> >>> 2012/8/31 Massimo Di Pierro <massimo....@gmail.com>: 
> >>> > What's the error? Works for me. 
> >>> > 
> >>> > 
> >>> > On Friday, 31 August 2012 16:00:24 UTC-5, Paolo wrote: 
> >>> >> 
> >>> >> Hi Massimo, 
> >>> >> I made a simple app, accordingly this example the error seems 
> related 
> >>> >> to the cache, because without the cache all works fine. 
> >>> >> 
> >>> >> The model: 
> >>> >> db.define_table('reviews_like_s', 
> >>> >>                  Field('review_id', 'reference reviews'), 
> >>> >>                  Field('helpful', 'boolean')) 
> >>> >> 
> >>> >> db.define_table('reviews_s', 
> >>> >>                 Field('title')) 
> >>> >> db.reviews_s.pos = Field.Lazy(lambda row: 
> >>> >> db((db.reviews_like_s.review_id == row.reviews_s.id) & 
> >>> >> (db.reviews_like_s.helpful == True)).count() ) 
> >>> >> db.reviews_s.neg = Field.Lazy(lambda row: 
> >>> >> db((db.reviews_like_s.review_id == row.reviews_s.id) & 
> >>> >> (db.reviews_like_s.helpful == False)).count() ) 
> >>> >> 
> >>> >> 
> >>> >> Function: 
> >>> >> def test(): 
> >>> >>     id = db.reviews_s.insert(title='xxx') 
> >>> >>     db.reviews_like_s.insert(review_id=id, helpful=True) 
> >>> >>     rows = db(db.reviews_s.id > 0).select( cache=(cache.ram,3600) 
> ) 
> >>> >>     return dict(reviews=rows) 
> >>> >> 
> >>> >> The view: 
> >>> >> {{for review in reviews:}} 
> >>> >>         <p>{{=review.pos()}}</p> 
> >>> >> {{pass}} 
> >>> >> 
> >>> >> On my side the first time works well, the second rises the error. 
> >>> >> 
> >>> >> Best, 
> >>> >> Paolo 
> >>> >> 
> >>> >> 
> >>> >> 2012/8/30 paolo....@gmail.com <paolo....@gmail.com>: 
> >>> >> > Hi Massimo, 
> >>> >> > no, I do not have multiple models, I have defined the string 
> >>> >> > connection only once in db.py, that's all. 
> >>> >> > I have a few try...except but in other places, I do not thing 
> they 
> >>> >> > are 
> >>> >> > connected. 
> >>> >> > I will try to make a simple app ables to reproduce the problem as 
> >>> >> > soon 
> >>> >> > as possible. 
> >>> >> > 
> >>> >> > Paolo 
> >>> >> > 
> >>> >> > 2012/8/30 Massimo Di Pierro <massimo....@gmail.com>: 
> >>> >> >> Can you make a simple app to reproduce this? 
> >>> >> >> 
> >>> >> >> Do you have try .... except anywhere? Do you have multiple 
> models 
> >>> >> >> wich 
> >>> >> >> define the same db = DAL(...) connection? 
> >>> >> >> 
> >>> >> >> 
> >>> >> >> On Thursday, 30 August 2012 15:49:01 UTC-5, Paolo wrote: 
> >>> >> >>> 
> >>> >> >>> Hi Bruno, a simple db with sqlite without modules 
> >>> >> >>> 
> >>> >> >>> db = DAL('sqlite://storage.sqlite', lazy_tables=True ) 
> >>> >> >>> 
> >>> >> >>> sorry, but I've just discovered that the same problem is 
> happened 
> >>> >> >>> even 
> >>> >> >>> with lazy_tables=False 
> >>> >> >>> Paolo 
> >>> >> >>> 
> >>> >> >>> 2012/8/30 Bruno Rocha <rocha...@gmail.com>: 
> >>> >> >>> > How are you defining the db connection? are you using models 
> or 
> >>> >> >>> > modules? 
> >>> >> >>> > which db? 
> >>> >> >>> > 
> >>> >> >>> > -- 
> >>> >> >>> > 
> >>> >> >>> > 
> >>> >> >>> > 
> >>> >> >>> 
> >>> >> >>> 
> >>> >> >>> 
> >>> >> >>> -- 
> >>> >> >>>  Paolo 
> >>> >> >> 
> >>> >> >> -- 
> >>> >> >> 
> >>> >> >> 
> >>> >> >> 
> >>> >> > 
> >>> >> > 
> >>> >> > 
> >>> >> > -- 
> >>> >> >  Paolo 
> >>> >> 
> >>> >> 
> >>> >> 
> >>> >> -- 
> >>> >>  Paolo 
> >>> > 
> >>> > -- 
> >>> > 
> >>> > 
> >>> > 
> >>> 
> >>> 
> >>> 
> >>> -- 
> >>>  Paolo 
> > 
> > -- 
> > 
> > 
> > 
>
>
>
> -- 
>  Paolo 
>

-- 



Reply via email to