[Nipy-devel] traitswrap

Brian Hawthorne brian.lee.hawthorne at gmail.com
Mon May 22 20:27:14 CDT 2006


hey,
just wanted to point out that with something like the wrapper sketched
below, you could use traits to edit the attributes *any* random python
object, as long as its attributes (using that word in the general sense ;)
have types that can be mapped to traits editors.  i think that would be a
pretty cool utility to have...

regarding the bridge, i was imagining something like this (rough sketch):
> ---------------------------------------
> from attributes import attribute
> from enthought.traits import HasTraits, Trait
>
> class WrapperTrait (Trait):
>     # this is where some work would be
>     def __init__(self, name, types): pass
>
> def traitforval(name, val):
>     return WrapperTrait(name, (type(val),))
>
> def traitforatt(att):
>     return WrapperTrait(att.name, att.implements)
>
> def attsof(obj):
>     return [(name,getattr(obj,name)) for name in dir(obj)]
>
> def traitswrap(obj):
>     newtraits = {}
>
>     # collect traits for unmanaged attributes
>     newtraits.update([(name,traitforval(name,val)) for name,val in
> vars(obj)]):
>
>     # collect traits for managed attributes
>     newtraits.update([(name,traitforatt(att)) for name,att in
> attsof(obj)]\
>                       if issubclass(att, attribute)):
>
>     def __init__(self, obj): self._wrappedobject = obj
>     newtraits["__init__"] = __init__
>     return type("TraitsProxy", (HasTraits,), newtraits)(obj)
> ------------------------------------------
>
> then in practice, we would use the traitswrap function to return a
> HasTraits object with traits corresponding to the attributes of the wrapped
> object.  for example, if you want to edit your object "attributey", do this:
>
>
> traitswrap(attributey).configure_traits()
>
> in the above implementation sketch, the work would go into the
> WrapperTrait (maybe a trait factory, not a trait subclass), which would know
> how to map attribute types to trait types, and overload trait
> getting/setting functionality to delegate to the underlying wrapped object.
> not knowing the traits internals at this time, i can't say exactly how that
> would be done, but it shouldn't be too hard.
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/nipy-devel/attachments/20060522/85f30142/attachment-0002.html 


More information about the Nipy-devel mailing list