[TransWarp] more mdl_XXX methods ?

Phillip J. Eby pje at telecommunity.com
Wed Apr 2 09:26:22 EST 2003

At 03:28 PM 4/2/03 +0200, Ulrich Eck wrote:
>Hi Phillip,
>a few times i wanted to have more functionality from the model
>element/feature/type classes.
>currently we have:
>mdl_fromString (fromString) -> Convert string to state
>mdl_fromFields (fromFields) -> Convert sequence to state
>would it be possible to add the following methods to peak.model-classes:
>mdl_fromXML (fromXML) -> Convert XML to state

Uh, that one's not gonna happen.  There are too many possible XML 
representations of something.  Instead, I'd suggest using the 
'peak.storage.xmi' package to handle serialization to/from XML.

>mdl_toString (toString) -> Convert state to string

This one is actually on my list.

>mdl_toFields (toFields) -> Convert state to fields

This one might show up too.

>mdl_toXML (toXML) -> Convert state to XML

Not gonna happen.

>one cannot add these methods easily because the setup
>is done via metaclasses - that would mean, i need to
>extend supertypes but cannot reuse the standard
>model.Attribute/Collection/... classes but need to
>define them by myself

There's always monkeypatching.  :)  But really, if you wanted to add 
something to say, an Attribute, it's not that difficult:

class MyAttrib(model.Attribute):
     toFields = binding.classAttr(binding.bindTo('typeObject/mdl_toFields'))

MyAttrib and its subclasses will then have a per-class 'toFields()' method 
based on their respective referenced types.

You can also do it like this:

class MyFeatureBase(model.StructuralFeature):
     toFields = binding.classAttr(binding.bindTo('typeObject/mdl_toFields'))

And then derive your own Attribute, Collection, etc. features again.  If 
you look at peak.model.features, you'll see that most of the other classes 
are maybe only 3 lines of code, as simple subclasses of 
StructuralFeature.  This was intentional, to allow for this sort of thing.

Keep in mind, though, that PEAK reserves the 'mdl_' prefix for its own use, 
so if you go around adding 'mdl_' methods, they might get stomped by a 
PEAK-specific thing later on.  Feel free to create your own prefix for any 
special methods like this that you need.

More information about the PEAK mailing list