[PEAK] Avoiding double naming

Phillip J. Eby pje at telecommunity.com
Sun Jul 16 20:09:50 EDT 2006


At 08:26 PM 7/16/2006 +0200, Jean-Philippe Dutreve wrote:
>I'm coding derived attributes separately from their elements so that their 
>rules
>are reused :
>
>def getLocal(name):
>     class attr(model.Attribute):
>          # some 'local' rules here
>     attr.attrName = name
>     return attr
>
>class MyElement(model.Element):
>     foo = getLocal('foo')
>     bar = getLocal('bar')
>
>How could I avoid double naming as in "foo = getLocal('foo')" ?

See "Descriptor Activation" in peak/binding/attributes.txt.  You need to 
create a descriptor type that will receive the "activateInClass" method and 
do the necessary naming then.  In short, you would do something like:

class Local(object):
     protocols.advise(instancesProvide=[binding.IActiveDescriptor])
     def activateInClass(self, klass, attrName):
         class attr(...):
             ...
         attr.attrName = attrName
         setattr(klass, attrName, attr)
         return attr

and then just do 'foo = bar = Local()' in the body of your element class.





More information about the PEAK mailing list