The PEAK Developers' Center   ComposingHierarchies UserPreferences
 
HelpContents Search Diffs Info Edit Subscribe XML Print View
Version as of 2003-07-15 15:16:48

Clear message


1 Introduction

The purpose of this page is to give a quick overview of how to compose component hierarchies using the PEAK framework, using working code. I have inlined comments in the code example, so hopefully, it will be self explanatory.

2 Code

Here's the code example that I came up with while working out how to compose hiearchies, and "aquire" objects from parent components in PEAK.

    1 #file components1.py
    2 from peak.api import *
    3 from peak.interface import Interface, Attribute
    4 
    5 #------------------------------------------------------------
    6 class ISpam(Interface):
    7     """Spam interface"""
    8     def spam():
    9         """provide spam"""
   10 
   11 class Spam(binding.Component):
   12     protocols.advise(instancesProvide=[ISpam,],)
   13     def spam(self):
   14         return "spam"
   15 
   16 #------------------------------------------------------------
   17 class IEggs(Interface):
   18     """Eggs interface"""
   19     def eggs():
   20         """provide eggs"""
   21 
   22 class Eggs(binding.Component):
   23     protocols.advise(instancesProvide=[IEggs,],)
   24     def eggs(self):
   25         return "eggs"
   26 
   27 class OtherEggs(binding.Component):
   28     protocols.advise(instancesProvide=[IEggs,],)
   29     def eggs(self):
   30         return "othereggs"
   31 
   32 
   33 #------------------------------------------------------------
   34 class GrandChildCmp(binding.Component):
   35     #acquired from SimpleCmp
   36     gcSpam = binding.bindToProperty("simplecmpApp.spam")
   37     gcColor = binding.bindToProperty("simplecmpApp.color")
   38     gcEggs = binding.bindToProperty("simplecmpApp.eggs")
   39     #acquired from ChildCmp
   40     gcOtherEggs = binding.bindTo(IEggs)
   41     #acquired from SimpleCmp
   42     gcStr = binding.bindTo("sc_string")
   43 
   44 class ChildCmp(binding.Component):
   45     #acquired from SimpleCmp
   46     cEggs = binding.bindTo(IEggs)
   47     cSpam = binding.bindTo(ISpam)
   48     cStr = binding.bindTo("sc_string")
   49     #overrides IEggs provided by SimpleCmp
   50     __eggs = binding.New(OtherEggs, offerAs=[IEggs])
   51 
   52     #make a GrandChild child of this component
   53     grandChild = binding.New(GrandChildCmp)
   54 
   55 class SimpleCmp(binding.Component):
   56     #simple component variable. Child components can look up directly by name
   57     sc_string = "simple-c!"
   58     #__eggs and __spam are registered as Interfaces and properties
   59     #They can be bound either using binding.bindTo(<InterfaceName>)
   60     #or binding.bindToProperty(<property.name>)
   61     __eggs = binding.New(Eggs, offerAs=[IEggs, PropertyName("simplecmpApp.eggs")])
   62     __spam = binding.New(Spam, offerAs=[ISpam, PropertyName("simplecmpApp.spam")])
   63     #__color is a Constant bound to a property
   64     __color = binding.Constant(value="red", offerAs=["simplecmpApp.color"])
   65 
   66     #make a ChildCmp child of this component
   67     child = binding.New(ChildCmp)
   68 
   69 #------------------------------------------------------------
   70 if __name__ == "__main__":
   71 
   72     sc = SimpleCmp()
   73     c = sc.child
   74     gc = c.grandChild
   75 
   76 
   77     print "c.eggs: ",c.cEggs.eggs()
   78     print "c.eggs: ",c.cSpam.spam()
   79     print "c.cStr: ",c.cStr
   80     print "gc.spam: ",gc.gcSpam.spam()
   81     print "gc.eggs: ",gc.gcEggs.eggs()
   82     print "gcOther.eggs: ",gc.gcOtherEggs.eggs()
   83     print "gc.myColor: ",gc.gcColor
   84     print "gc.gcStr: ",gc.gcStr
   85     print "done"

3 Running the example

Running this example should produce output similar to below:
[joel@banzai playground1]$ python components1.py 
c.eggs:  othereggs 
c.eggs:  spam 
c.cStr:  simple-c! 
gc.spam:  spam 
gc.eggs:  eggs 
gcOther.eggs:  othereggs 
gc.myColor:  red 
gc.gcStr:  simple-c! 
done 

PythonPowered
EditText of this page (last modified 2003-07-15 15:16:48)
FindPage by browsing, title search , text search or an index
Or try one of these actions: AttachFile, DeletePage, LikePages, LocalSiteMap, SpellCheck