experimental stuff: Deferring proxies...
git-svn-id: svn://svn.cy55.de/Zope3/src/cybertools/trunk@1908 fd906abe-77d9-0310-91a1-e0d9ade77398
This commit is contained in:
parent
65564049ec
commit
8f96dca0a4
3 changed files with 163 additions and 0 deletions
72
twisted/deferring.py
Normal file
72
twisted/deferring.py
Normal file
|
@ -0,0 +1,72 @@
|
||||||
|
#
|
||||||
|
# Copyright (c) 2007 Helmut Merz helmutm@cy55.de
|
||||||
|
#
|
||||||
|
# This program is free software; you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
# the Free Software Foundation; either version 2 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License
|
||||||
|
# along with this program; if not, write to the Free Software
|
||||||
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||||
|
#
|
||||||
|
|
||||||
|
"""
|
||||||
|
Objects that defer attribute access.
|
||||||
|
|
||||||
|
$Id$
|
||||||
|
"""
|
||||||
|
|
||||||
|
from twisted.internet import reactor
|
||||||
|
from twisted.internet.defer import Deferred
|
||||||
|
|
||||||
|
|
||||||
|
class Deferring(Deferred):
|
||||||
|
|
||||||
|
def __init__(self, context):
|
||||||
|
self.context = context
|
||||||
|
Deferred.__init__(self)
|
||||||
|
|
||||||
|
def __getattr__(self, attr):
|
||||||
|
d = Deferring(self)
|
||||||
|
reactor.callLater(0, d.getattr, self, attr)
|
||||||
|
return d
|
||||||
|
|
||||||
|
def __call__(self, *args, **kw):
|
||||||
|
d = Deferring(None)
|
||||||
|
reactor.callLater(0, d.call, self, *args, **kw)
|
||||||
|
return d
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return '<Deferring on %s>' % repr(self.context)
|
||||||
|
|
||||||
|
__str__ = __repr__
|
||||||
|
|
||||||
|
def makeCallback(self, method, *args, **kw):
|
||||||
|
#print '*** makeCallback', self, method, args, kw
|
||||||
|
def cb(result, method=method, args=args, kw=kw):
|
||||||
|
value = method(result, *args, **kw)
|
||||||
|
#print '*** cb', self, result, method, args, kw, value
|
||||||
|
self.callback(value)
|
||||||
|
return cb
|
||||||
|
|
||||||
|
def getattr(self, deferring, attr):
|
||||||
|
ctx = deferring.context
|
||||||
|
if isinstance(ctx, Deferring):
|
||||||
|
ctx.addCallback(self.makeCallback(self.getattr, attr))
|
||||||
|
else:
|
||||||
|
value = getattr(deferring.context, attr)
|
||||||
|
self.context = value
|
||||||
|
self.callback(value)
|
||||||
|
|
||||||
|
def call(self, deferring, *args, **kw):
|
||||||
|
ctx = deferring.context
|
||||||
|
value = ctx(*args, **kw)
|
||||||
|
self.context = value
|
||||||
|
self.callback(value)
|
||||||
|
|
55
twisted/deferring.txt
Normal file
55
twisted/deferring.txt
Normal file
|
@ -0,0 +1,55 @@
|
||||||
|
======================
|
||||||
|
Working with Deferreds
|
||||||
|
======================
|
||||||
|
|
||||||
|
$Id$
|
||||||
|
|
||||||
|
>>> from cybertools.twisted.tests import tester
|
||||||
|
|
||||||
|
>>> from cybertools.twisted.deferring import Deferring
|
||||||
|
|
||||||
|
>>> class Demo(object):
|
||||||
|
... color = 'green'
|
||||||
|
|
||||||
|
>>> class Demo2(object):
|
||||||
|
... color = 'red'
|
||||||
|
... other = Demo()
|
||||||
|
|
||||||
|
>>> demo = Demo()
|
||||||
|
>>> demoDef = Deferring(demo)
|
||||||
|
>>> col = demoDef.color
|
||||||
|
>>> col
|
||||||
|
<Deferring on ...>
|
||||||
|
>>> print col
|
||||||
|
<Deferring on ...>
|
||||||
|
|
||||||
|
>>> def printIt(result):
|
||||||
|
... print 'result:', str(result)
|
||||||
|
|
||||||
|
>>> col.addCallback(printIt)
|
||||||
|
<Deferring on ...>
|
||||||
|
|
||||||
|
>>> tester.iterate()
|
||||||
|
result: green
|
||||||
|
>>> col
|
||||||
|
<Deferring on 'green'>
|
||||||
|
|
||||||
|
>>> demo2 = Demo2()
|
||||||
|
>>> demo2Def = Deferring(demo2)
|
||||||
|
>>> other = demo2Def.other
|
||||||
|
>>> other
|
||||||
|
<Deferring on <Deferring on ...>>
|
||||||
|
|
||||||
|
>>> ocol = other.color
|
||||||
|
|
||||||
|
>>> ocol.addCallback(printIt)
|
||||||
|
<Deferring on ...>
|
||||||
|
|
||||||
|
>>> tester.iterate()
|
||||||
|
result: green
|
||||||
|
|
||||||
|
>>> ocol
|
||||||
|
<Deferring on 'green'>
|
||||||
|
|
||||||
|
>>> other
|
||||||
|
<Deferring on <Demo object ...>>
|
36
twisted/tests.py
Executable file
36
twisted/tests.py
Executable file
|
@ -0,0 +1,36 @@
|
||||||
|
# $Id$
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
import doctest
|
||||||
|
from twisted.internet import reactor
|
||||||
|
|
||||||
|
import cybertools.twisted.deferring
|
||||||
|
|
||||||
|
|
||||||
|
class Tester(object):
|
||||||
|
|
||||||
|
def iterate(self, n=10, delays={}):
|
||||||
|
for i in range(n):
|
||||||
|
delay = delays.get(i, 0)
|
||||||
|
reactor.iterate(delay)
|
||||||
|
|
||||||
|
tester = Tester()
|
||||||
|
|
||||||
|
|
||||||
|
class Test(unittest.TestCase):
|
||||||
|
"Basic tests for modules in the util package."
|
||||||
|
|
||||||
|
def testBasicStuff(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def test_suite():
|
||||||
|
flags = doctest.NORMALIZE_WHITESPACE | doctest.ELLIPSIS
|
||||||
|
return unittest.TestSuite((
|
||||||
|
#unittest.makeSuite(Test), # we don't need this
|
||||||
|
#doctest.DocTestSuite(cybertools.util.property, optionflags=flags),
|
||||||
|
doctest.DocFileSuite('deferring.txt', optionflags=flags),
|
||||||
|
))
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main(defaultTest='test_suite')
|
Loading…
Add table
Reference in a new issue