[Twisted-web] A question with storm orm and Nevow

Daniel Yang daniel.yang.zhenyu at gmail.com
Fri Jul 17 02:56:23 EDT 2009


using storm orm(twisted version) in Nevow:

Here is my approach:

First, create a pool:

from storm.databases.sqlite import SQLite

from storm.uri import URI

from storm.twisted.store import StorePool


database = SQLite(URI(*'sqlite:///test.db'*))

pool = StorePool(database, 2, 5)

pool.start()


In pages, we can use store in render functions like:

def *render_PeopleList*(*self*, ctx, data):

pat = inevow.IQ(ctx).patternGenerator(*'PeopleItem'*)


 def *cb_find*(results, s):

results.config(offset=0, limit=10)

return results.all().addCallback(cb_all, s)

 def *cb_all*(items, s):

#recycle this store.

pool.put(s)

ps = []

for item in items:

p = pat()

p.fillSlots(*'nickname'*, item.nickname)

ps.append(p)

 return ctx.tag[ ps ]

 def *cb_get*(s):

return s.find(User).addBoth(cb_find, s)


 return pool.get().addCallback(cb_get).addErrback(lambda error:
log.msg(error))

That's it.

Thank you guys for help.

FYI: Storm ORM with twisted integration is not quite stale yet.


On Thu, Jul 9, 2009 at 10:18 AM, Daniel Yang
<daniel.yang.zhenyu at gmail.com>wrote:

> Thanks, Tristan and Colin.
> Yes. the code was taken from storm unittests.
>
> I was planning to do some rendering work in cb_result.
> Let me try first.
>
>
> On Thu, Jul 9, 2009 at 9:59 AM, Tristan Seligmann <mithrandi at mithrandi.net
> > wrote:
>
>> On Thu, Jul 9, 2009 at 3:30 AM, Daniel Yang<daniel.yang.zhenyu at gmail.com>
>> wrote:
>> > def test_execute(self):
>>
>> This looks like a unit test, not Nevow code?
>>
>> >     def cb_execute(result):
>> >         #this result is a DeferredResult, not the actual data.
>> >         return result.get_one().addCallback(cb_result)
>> >     def cb_result(result):
>> >         #the data returns
>> >          print result
>>
>> Return your ultimate result here; note that if you're not actually
>> going to do anything to result in cb_result, you could just have:
>>
>> def cb_execute(result):
>>    return result.get_one()
>>
>> >     self.store.execute("SELECT title FROM foo WHERE
>> id=10").addCallback(cb_execute)
>> >     return ?
>>
>> return self.store.execute("SELECT title FROM foo WHERE
>> id=10").addCallback(cb_execute)
>>
>> The result returned from cb_result is what will ultimately used by
>> whatever invoked test_execute, assuming that it pays attention to the
>> return value; I'm not clear how test_execute is being used, so I can't
>> say this will work for sure. If test_execute is, for example, a render
>> method, then it should work just fine.
>> --
>> mithrandi, i Ainil en-Balandor, a faer Ambar
>>
>> _______________________________________________
>> Twisted-web mailing list
>> Twisted-web at twistedmatrix.com
>> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-web
>>
>
>
>
> --
> -----------------------------------------------
> Yours
> Faithfully
>
> Daniel Yang
>



-- 
-----------------------------------------------
Yours
Faithfully

Daniel Yang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-web/attachments/20090717/6044bc09/attachment.htm 


More information about the Twisted-web mailing list