[TransWarp] Components + existing classes

Joel Boehland joel at memes.com
Thu Jul 17 22:35:16 EDT 2003


Hi--
I my further adventures of playing w/ 
PEAK components, I'm exploring the 
issues of using PEAK components along 
with (and extending) existing classes.

I've been able to get an example to 
work, and I just wanted to run it by the 
list to get comments on whether this is 
done the intended way. The main problem 
I'm dealing with here, just to be clear, 
is: Correctly writing Components that 
extend binding.Component AND some 
non-Component class that has a non-empty 
__init__ method. If you don't have an 
__init__ method, you can just use 
binding.New(<class>) to have it bound. 
For non-empty init methods, you have to 
make a sort of factory method for that 
component and use it in 
binding.Once(<factoryMeth>). At least 
that's the way that I've done it. If 
that isn't the way to go, I'd appreciate 
some pointers.

Here's some sample code:
==============================================
#file components2.py
from peak.api import *

#------------------------------------------------------------
class Foo(object):
     def __init__(self, fooStuff):
         self.fooStuff = fooStuff

     def foo(self):
         return "fooStuff: "+self.fooStuff

#------------------------------------------------------------
class ChildFooCmp(binding.Component, Foo):
     #acquired from SimpleCmp
     cStr = binding.bindTo("sc_string")

     def __init__(self, 
parentComponent=None, foo=None):
         binding.Component.__init__(self,
 
          parentComponent=parentComponent)
         Foo.__init__(self, foo)

class SimpleCmp(binding.Component):
     #simple component variable. Child 
components
     #can look up directly  by name
     sc_string = "simple-c!"

     #make a ChildFooCmp child of this 
component
     def makeChild(self, instDict, 
attrName):
         return 
ChildFooCmp(parentComponent=self, 
foo=self.sc_string)
     #bind child to a component variable
     child = binding.Once(makeChild, 
attrName="child")
#------------------------------------------------------------
if __name__ == "__main__":

     sc = SimpleCmp()
     c = sc.child
     print "c.foo: ",c.foo()
     print "c.cStr: ",c.cStr
     print "done"

=======================================================
running this prints out:
c.foo:  fooStuff: simple-c!
c.cStr:  simple-c!
done




More information about the PEAK mailing list