Using HasTraits functions to set extended trait names

classic Classic list List threaded Threaded
13 messages Options
Reply | Threaded
Open this post in threaded view
|

Using HasTraits functions to set extended trait names

Adam Hughes
Hi,

I've been using some of the built-in features of HasTraits to run
simulations, mainly by using trait_get() and trait_set() methods.  I've
designed a structure that I think is fairly powerful for simulations,
because it allows a user to specify a set of ranges for valid float traits,
then the simulation will update all of these traits step by step, so it's
very easy to run simulations over many different types of variables in the
program using this same framework.  All the user has to provide are the
correct trait names.

For example, if I had traits A, B my simulation would update them both as
it runs along.  In practice, my objects are fairly complex, so the traits
that I really want to iterate over a buried deep in the class heirarchy.
In reality, I might want to run a simulation that varies A, and the trait
B.C.D where D is deep in the class heirarchy, a sub-sub-class of B.

The trait_get, and trait_set work() fine when I provide class trait names
like 'A' and 'B', but doesn't work with the extended notation as far as I
can tell (e.g. trait 'B.C.D').  Also I noticed that when I list trait names
using the .trait_names() method, it also does not access traits in the
subclasses.  Is there a known way to access subclass traits from the basic
HasTraits functions?

Thanks.
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Using HasTraits functions to set extended trait names

Jonathan Rocher
Hi Adam,

Do you have a sample code to make sure I understand what you are trying to
do? Why could you not use getattr and setattr?

Thanks,
Jonathan

On Mon, Jan 30, 2012 at 7:30 PM, Adam Hughes <[hidden email]>wrote:

> Hi,
>
> I've been using some of the built-in features of HasTraits to run
> simulations, mainly by using trait_get() and trait_set() methods.  I've
> designed a structure that I think is fairly powerful for simulations,
> because it allows a user to specify a set of ranges for valid float traits,
> then the simulation will update all of these traits step by step, so it's
> very easy to run simulations over many different types of variables in the
> program using this same framework.  All the user has to provide are the
> correct trait names.
>
> For example, if I had traits A, B my simulation would update them both as
> it runs along.  In practice, my objects are fairly complex, so the traits
> that I really want to iterate over a buried deep in the class heirarchy.
> In reality, I might want to run a simulation that varies A, and the trait
> B.C.D where D is deep in the class heirarchy, a sub-sub-class of B.
>
> The trait_get, and trait_set work() fine when I provide class trait names
> like 'A' and 'B', but doesn't work with the extended notation as far as I
> can tell (e.g. trait 'B.C.D').  Also I noticed that when I list trait names
> using the .trait_names() method, it also does not access traits in the
> subclasses.  Is there a known way to access subclass traits from the basic
> HasTraits functions?
>
> Thanks.
> _______________________________________________
> Enthought-Dev mailing list
> [hidden email]
> https://mail.enthought.com/mailman/listinfo/enthought-dev
>



--
Jonathan Rocher, PhD
Scientific software developer
Enthought, Inc.
[hidden email]
1-512-536-1057
http://www.enthought.com
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Using HasTraits functions to set extended trait names

Adam Hughes
Jonathan,

Thanks for getting back to me.  I'll get an example out tomorrow, sorry for
the delay.

On Tue, Jan 31, 2012 at 5:03 PM, Jonathan Rocher <[hidden email]>wrote:

> Hi Adam,
>
> Do you have a sample code to make sure I understand what you are trying to
> do? Why could you not use getattr and setattr?
>
> Thanks,
> Jonathan
>
> On Mon, Jan 30, 2012 at 7:30 PM, Adam Hughes <[hidden email]
> >wrote:
>
> > Hi,
> >
> > I've been using some of the built-in features of HasTraits to run
> > simulations, mainly by using trait_get() and trait_set() methods.  I've
> > designed a structure that I think is fairly powerful for simulations,
> > because it allows a user to specify a set of ranges for valid float
> traits,
> > then the simulation will update all of these traits step by step, so it's
> > very easy to run simulations over many different types of variables in
> the
> > program using this same framework.  All the user has to provide are the
> > correct trait names.
> >
> > For example, if I had traits A, B my simulation would update them both as
> > it runs along.  In practice, my objects are fairly complex, so the traits
> > that I really want to iterate over a buried deep in the class heirarchy.
> > In reality, I might want to run a simulation that varies A, and the trait
> > B.C.D where D is deep in the class heirarchy, a sub-sub-class of B.
> >
> > The trait_get, and trait_set work() fine when I provide class trait names
> > like 'A' and 'B', but doesn't work with the extended notation as far as I
> > can tell (e.g. trait 'B.C.D').  Also I noticed that when I list trait
> names
> > using the .trait_names() method, it also does not access traits in the
> > subclasses.  Is there a known way to access subclass traits from the
> basic
> > HasTraits functions?
> >
> > Thanks.
> > _______________________________________________
> > Enthought-Dev mailing list
> > [hidden email]
> > https://mail.enthought.com/mailman/listinfo/enthought-dev
> >
>
>
>
> --
> Jonathan Rocher, PhD
> Scientific software developer
> Enthought, Inc.
> [hidden email]
> 1-512-536-1057
> http://www.enthought.com
> _______________________________________________
> Enthought-Dev mailing list
> [hidden email]
> https://mail.enthought.com/mailman/listinfo/enthought-dev
>
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Using HasTraits functions to set extended trait names

Adam Hughes
I've made a working example.  This code uses a random number generator to
set trait values, and when the trait is a trait of the main class, it
works; however, when it is a trait of the subclass, it doesn't.  I've
chosen this method because I think it's very amenable to simulations; all
the user does is specify the trait he/she wants as a string, rather than
opening the source and referring to the trait variable explicitly.  Aka I
realize I could make this work by saying "self.sub.a.set()" but I'd rather
use this notation if possible.

from traits.api import *
from traitsui.api import *
from random import random

class SubClass(HasTraits):
    a=Float(1.0)

class Main(HasTraits):
    b=Float(2.0)
    sub=Instance(SubClass,())

    set_random=Button

    def _set_random_fired(self):
        bnew={'b':random()}
        self.set(**bnew)  #Works

        anew={'sub.a':random()}
        self.set(**anew) #Doesn't work

    traits_view=View(
                Item('b'), Item('sub', style='custom'), Item('set_random')
            )

Main().configure_traits()

On Tue, Jan 31, 2012 at 8:33 PM, Adam Hughes <[hidden email]>wrote:

> Jonathan,
>
> Thanks for getting back to me.  I'll get an example out tomorrow, sorry
> for the delay.
>
>
> On Tue, Jan 31, 2012 at 5:03 PM, Jonathan Rocher <[hidden email]>wrote:
>
>> Hi Adam,
>>
>> Do you have a sample code to make sure I understand what you are trying to
>> do? Why could you not use getattr and setattr?
>>
>> Thanks,
>> Jonathan
>>
>> On Mon, Jan 30, 2012 at 7:30 PM, Adam Hughes <[hidden email]
>> >wrote:
>>
>> > Hi,
>> >
>> > I've been using some of the built-in features of HasTraits to run
>> > simulations, mainly by using trait_get() and trait_set() methods.  I've
>> > designed a structure that I think is fairly powerful for simulations,
>> > because it allows a user to specify a set of ranges for valid float
>> traits,
>> > then the simulation will update all of these traits step by step, so
>> it's
>> > very easy to run simulations over many different types of variables in
>> the
>> > program using this same framework.  All the user has to provide are the
>> > correct trait names.
>> >
>> > For example, if I had traits A, B my simulation would update them both
>> as
>> > it runs along.  In practice, my objects are fairly complex, so the
>> traits
>> > that I really want to iterate over a buried deep in the class heirarchy.
>> > In reality, I might want to run a simulation that varies A, and the
>> trait
>> > B.C.D where D is deep in the class heirarchy, a sub-sub-class of B.
>> >
>> > The trait_get, and trait_set work() fine when I provide class trait
>> names
>> > like 'A' and 'B', but doesn't work with the extended notation as far as
>> I
>> > can tell (e.g. trait 'B.C.D').  Also I noticed that when I list trait
>> names
>> > using the .trait_names() method, it also does not access traits in the
>> > subclasses.  Is there a known way to access subclass traits from the
>> basic
>> > HasTraits functions?
>> >
>> > Thanks.
>> > _______________________________________________
>> > Enthought-Dev mailing list
>> > [hidden email]
>> > https://mail.enthought.com/mailman/listinfo/enthought-dev
>> >
>>
>>
>>
>> --
>> Jonathan Rocher, PhD
>> Scientific software developer
>> Enthought, Inc.
>> [hidden email]
>> 1-512-536-1057
>> http://www.enthought.com
>> _______________________________________________
>> Enthought-Dev mailing list
>> [hidden email]
>> https://mail.enthought.com/mailman/listinfo/enthought-dev
>>
>
>
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Using HasTraits functions to set extended trait names

Jonathan Rocher
Hi again Adam,

I see now. The way I would do it is to replace
       self.set(**anew) #Doesn't work
by
       setattr(self.sub, 'a', random())

In fact that's what the set/trait_set method does: simply call setattr
dealing with change notification around it. It wouldn't be that hard to
extend the method to accept the input you are suggesting.

Jonathan

On Wed, Feb 1, 2012 at 10:47 AM, Adam Hughes <[hidden email]>wrote:

> I've made a working example.  This code uses a random number generator to
> set trait values, and when the trait is a trait of the main class, it
> works; however, when it is a trait of the subclass, it doesn't.  I've
> chosen this method because I think it's very amenable to simulations; all
> the user does is specify the trait he/she wants as a string, rather than
> opening the source and referring to the trait variable explicitly.  Aka I
> realize I could make this work by saying "self.sub.a.set()" but I'd rather
> use this notation if possible.
>
> from traits.api import *
> from traitsui.api import *
> from random import random
>
> class SubClass(HasTraits):
>    a=Float(1.0)
>
> class Main(HasTraits):
>    b=Float(2.0)
>    sub=Instance(SubClass,())
>
>    set_random=Button
>
>    def _set_random_fired(self):
>        bnew={'b':random()}
>        self.set(**bnew)  #Works
>
>        anew={'sub.a':random()}
>        self.set(**anew) #Doesn't work
>
>    traits_view=View(
>                Item('b'), Item('sub', style='custom'), Item('set_random')
>            )
>
> Main().configure_traits()
>
> On Tue, Jan 31, 2012 at 8:33 PM, Adam Hughes <[hidden email]
> >wrote:
>
> > Jonathan,
> >
> > Thanks for getting back to me.  I'll get an example out tomorrow, sorry
> > for the delay.
> >
> >
> > On Tue, Jan 31, 2012 at 5:03 PM, Jonathan Rocher <[hidden email]
> >wrote:
> >
> >> Hi Adam,
> >>
> >> Do you have a sample code to make sure I understand what you are trying
> to
> >> do? Why could you not use getattr and setattr?
> >>
> >> Thanks,
> >> Jonathan
> >>
> >> On Mon, Jan 30, 2012 at 7:30 PM, Adam Hughes <[hidden email]
> >> >wrote:
> >>
> >> > Hi,
> >> >
> >> > I've been using some of the built-in features of HasTraits to run
> >> > simulations, mainly by using trait_get() and trait_set() methods.
>  I've
> >> > designed a structure that I think is fairly powerful for simulations,
> >> > because it allows a user to specify a set of ranges for valid float
> >> traits,
> >> > then the simulation will update all of these traits step by step, so
> >> it's
> >> > very easy to run simulations over many different types of variables in
> >> the
> >> > program using this same framework.  All the user has to provide are
> the
> >> > correct trait names.
> >> >
> >> > For example, if I had traits A, B my simulation would update them both
> >> as
> >> > it runs along.  In practice, my objects are fairly complex, so the
> >> traits
> >> > that I really want to iterate over a buried deep in the class
> heirarchy.
> >> > In reality, I might want to run a simulation that varies A, and the
> >> trait
> >> > B.C.D where D is deep in the class heirarchy, a sub-sub-class of B.
> >> >
> >> > The trait_get, and trait_set work() fine when I provide class trait
> >> names
> >> > like 'A' and 'B', but doesn't work with the extended notation as far
> as
> >> I
> >> > can tell (e.g. trait 'B.C.D').  Also I noticed that when I list trait
> >> names
> >> > using the .trait_names() method, it also does not access traits in the
> >> > subclasses.  Is there a known way to access subclass traits from the
> >> basic
> >> > HasTraits functions?
> >> >
> >> > Thanks.
> >> > _______________________________________________
> >> > Enthought-Dev mailing list
> >> > [hidden email]
> >> > https://mail.enthought.com/mailman/listinfo/enthought-dev
> >> >
> >>
> >>
> >>
> >> --
> >> Jonathan Rocher, PhD
> >> Scientific software developer
> >> Enthought, Inc.
> >> [hidden email]
> >> 1-512-536-1057
> >> http://www.enthought.com
> >> _______________________________________________
> >> Enthought-Dev mailing list
> >> [hidden email]
> >> https://mail.enthought.com/mailman/listinfo/enthought-dev
> >>
> >
> >
> _______________________________________________
> Enthought-Dev mailing list
> [hidden email]
> https://mail.enthought.com/mailman/listinfo/enthought-dev
>



--
Jonathan Rocher, PhD
Scientific software developer
Enthought, Inc.
[hidden email]
1-512-536-1057
http://www.enthought.com
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Using HasTraits functions to set extended trait names

Jonathan Rocher
To finish, the reason why what you tried doesn't work is that setattr
doesn't take dotted notation in the second argument. So if you get the
string you are describing, you would have to split it on the last dot, and
do an eval("self"+first part) for the first argument and the second part
for the second argument to setattr. Didn't try it but I don't see why that
wouldn't work.

HTH,
Jonathan

On Wed, Feb 1, 2012 at 5:42 PM, Jonathan Rocher <[hidden email]>wrote:

> Hi again Adam,
>
> I see now. The way I would do it is to replace
>        self.set(**anew) #Doesn't work
> by
>        setattr(self.sub, 'a', random())
>
> In fact that's what the set/trait_set method does: simply call setattr
> dealing with change notification around it. It wouldn't be that hard to
> extend the method to accept the input you are suggesting.
>
> Jonathan
>
>
> On Wed, Feb 1, 2012 at 10:47 AM, Adam Hughes <[hidden email]>wrote:
>
>> I've made a working example.  This code uses a random number generator to
>> set trait values, and when the trait is a trait of the main class, it
>> works; however, when it is a trait of the subclass, it doesn't.  I've
>> chosen this method because I think it's very amenable to simulations; all
>> the user does is specify the trait he/she wants as a string, rather than
>> opening the source and referring to the trait variable explicitly.  Aka I
>> realize I could make this work by saying "self.sub.a.set()" but I'd rather
>> use this notation if possible.
>>
>> from traits.api import *
>> from traitsui.api import *
>> from random import random
>>
>> class SubClass(HasTraits):
>>    a=Float(1.0)
>>
>> class Main(HasTraits):
>>    b=Float(2.0)
>>    sub=Instance(SubClass,())
>>
>>    set_random=Button
>>
>>    def _set_random_fired(self):
>>        bnew={'b':random()}
>>        self.set(**bnew)  #Works
>>
>>        anew={'sub.a':random()}
>>        self.set(**anew) #Doesn't work
>>
>>    traits_view=View(
>>                Item('b'), Item('sub', style='custom'), Item('set_random')
>>            )
>>
>> Main().configure_traits()
>>
>> On Tue, Jan 31, 2012 at 8:33 PM, Adam Hughes <[hidden email]
>> >wrote:
>>
>> > Jonathan,
>> >
>> > Thanks for getting back to me.  I'll get an example out tomorrow, sorry
>> > for the delay.
>> >
>> >
>> > On Tue, Jan 31, 2012 at 5:03 PM, Jonathan Rocher <[hidden email]
>> >wrote:
>> >
>> >> Hi Adam,
>> >>
>> >> Do you have a sample code to make sure I understand what you are
>> trying to
>> >> do? Why could you not use getattr and setattr?
>> >>
>> >> Thanks,
>> >> Jonathan
>> >>
>> >> On Mon, Jan 30, 2012 at 7:30 PM, Adam Hughes <[hidden email]
>> >> >wrote:
>> >>
>> >> > Hi,
>> >> >
>> >> > I've been using some of the built-in features of HasTraits to run
>> >> > simulations, mainly by using trait_get() and trait_set() methods.
>>  I've
>> >> > designed a structure that I think is fairly powerful for simulations,
>> >> > because it allows a user to specify a set of ranges for valid float
>> >> traits,
>> >> > then the simulation will update all of these traits step by step, so
>> >> it's
>> >> > very easy to run simulations over many different types of variables
>> in
>> >> the
>> >> > program using this same framework.  All the user has to provide are
>> the
>> >> > correct trait names.
>> >> >
>> >> > For example, if I had traits A, B my simulation would update them
>> both
>> >> as
>> >> > it runs along.  In practice, my objects are fairly complex, so the
>> >> traits
>> >> > that I really want to iterate over a buried deep in the class
>> heirarchy.
>> >> > In reality, I might want to run a simulation that varies A, and the
>> >> trait
>> >> > B.C.D where D is deep in the class heirarchy, a sub-sub-class of B.
>> >> >
>> >> > The trait_get, and trait_set work() fine when I provide class trait
>> >> names
>> >> > like 'A' and 'B', but doesn't work with the extended notation as far
>> as
>> >> I
>> >> > can tell (e.g. trait 'B.C.D').  Also I noticed that when I list trait
>> >> names
>> >> > using the .trait_names() method, it also does not access traits in
>> the
>> >> > subclasses.  Is there a known way to access subclass traits from the
>> >> basic
>> >> > HasTraits functions?
>> >> >
>> >> > Thanks.
>> >> > _______________________________________________
>> >> > Enthought-Dev mailing list
>> >> > [hidden email]
>> >> > https://mail.enthought.com/mailman/listinfo/enthought-dev
>> >> >
>> >>
>> >>
>> >>
>> >> --
>> >> Jonathan Rocher, PhD
>> >> Scientific software developer
>> >> Enthought, Inc.
>> >> [hidden email]
>> >> 1-512-536-1057
>> >> http://www.enthought.com
>> >> _______________________________________________
>> >> Enthought-Dev mailing list
>> >> [hidden email]
>> >> https://mail.enthought.com/mailman/listinfo/enthought-dev
>> >>
>> >
>> >
>> _______________________________________________
>> Enthought-Dev mailing list
>> [hidden email]
>> https://mail.enthought.com/mailman/listinfo/enthought-dev
>>
>
>
>
> --
> Jonathan Rocher, PhD
> Scientific software developer
> Enthought, Inc.
> [hidden email]
> 1-512-536-1057
> http://www.enthought.com
>
>


--
Jonathan Rocher, PhD
Scientific software developer
Enthought, Inc.
[hidden email]
1-512-536-1057
http://www.enthought.com
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Using HasTraits functions to set extended trait names

Adam Hughes
Thanks Jonathan, I appreciate your help very much.

On Wed, Feb 1, 2012 at 6:52 PM, Jonathan Rocher <[hidden email]>wrote:

> To finish, the reason why what you tried doesn't work is that setattr
> doesn't take dotted notation in the second argument. So if you get the
> string you are describing, you would have to split it on the last dot, and
> do an eval("self"+first part) for the first argument and the second part
> for the second argument to setattr. Didn't try it but I don't see why that
> wouldn't work.
>
> HTH,
> Jonathan
>
> On Wed, Feb 1, 2012 at 5:42 PM, Jonathan Rocher <[hidden email]
> >wrote:
>
> > Hi again Adam,
> >
> > I see now. The way I would do it is to replace
> >        self.set(**anew) #Doesn't work
> > by
> >        setattr(self.sub, 'a', random())
> >
> > In fact that's what the set/trait_set method does: simply call setattr
> > dealing with change notification around it. It wouldn't be that hard to
> > extend the method to accept the input you are suggesting.
> >
> > Jonathan
> >
> >
> > On Wed, Feb 1, 2012 at 10:47 AM, Adam Hughes <[hidden email]
> >wrote:
> >
> >> I've made a working example.  This code uses a random number generator
> to
> >> set trait values, and when the trait is a trait of the main class, it
> >> works; however, when it is a trait of the subclass, it doesn't.  I've
> >> chosen this method because I think it's very amenable to simulations;
> all
> >> the user does is specify the trait he/she wants as a string, rather than
> >> opening the source and referring to the trait variable explicitly.  Aka
> I
> >> realize I could make this work by saying "self.sub.a.set()" but I'd
> rather
> >> use this notation if possible.
> >>
> >> from traits.api import *
> >> from traitsui.api import *
> >> from random import random
> >>
> >> class SubClass(HasTraits):
> >>    a=Float(1.0)
> >>
> >> class Main(HasTraits):
> >>    b=Float(2.0)
> >>    sub=Instance(SubClass,())
> >>
> >>    set_random=Button
> >>
> >>    def _set_random_fired(self):
> >>        bnew={'b':random()}
> >>        self.set(**bnew)  #Works
> >>
> >>        anew={'sub.a':random()}
> >>        self.set(**anew) #Doesn't work
> >>
> >>    traits_view=View(
> >>                Item('b'), Item('sub', style='custom'),
> Item('set_random')
> >>            )
> >>
> >> Main().configure_traits()
> >>
> >> On Tue, Jan 31, 2012 at 8:33 PM, Adam Hughes <[hidden email]
> >> >wrote:
> >>
> >> > Jonathan,
> >> >
> >> > Thanks for getting back to me.  I'll get an example out tomorrow,
> sorry
> >> > for the delay.
> >> >
> >> >
> >> > On Tue, Jan 31, 2012 at 5:03 PM, Jonathan Rocher <
> [hidden email]
> >> >wrote:
> >> >
> >> >> Hi Adam,
> >> >>
> >> >> Do you have a sample code to make sure I understand what you are
> >> trying to
> >> >> do? Why could you not use getattr and setattr?
> >> >>
> >> >> Thanks,
> >> >> Jonathan
> >> >>
> >> >> On Mon, Jan 30, 2012 at 7:30 PM, Adam Hughes <
> [hidden email]
> >> >> >wrote:
> >> >>
> >> >> > Hi,
> >> >> >
> >> >> > I've been using some of the built-in features of HasTraits to run
> >> >> > simulations, mainly by using trait_get() and trait_set() methods.
> >>  I've
> >> >> > designed a structure that I think is fairly powerful for
> simulations,
> >> >> > because it allows a user to specify a set of ranges for valid float
> >> >> traits,
> >> >> > then the simulation will update all of these traits step by step,
> so
> >> >> it's
> >> >> > very easy to run simulations over many different types of variables
> >> in
> >> >> the
> >> >> > program using this same framework.  All the user has to provide are
> >> the
> >> >> > correct trait names.
> >> >> >
> >> >> > For example, if I had traits A, B my simulation would update them
> >> both
> >> >> as
> >> >> > it runs along.  In practice, my objects are fairly complex, so the
> >> >> traits
> >> >> > that I really want to iterate over a buried deep in the class
> >> heirarchy.
> >> >> > In reality, I might want to run a simulation that varies A, and the
> >> >> trait
> >> >> > B.C.D where D is deep in the class heirarchy, a sub-sub-class of B.
> >> >> >
> >> >> > The trait_get, and trait_set work() fine when I provide class trait
> >> >> names
> >> >> > like 'A' and 'B', but doesn't work with the extended notation as
> far
> >> as
> >> >> I
> >> >> > can tell (e.g. trait 'B.C.D').  Also I noticed that when I list
> trait
> >> >> names
> >> >> > using the .trait_names() method, it also does not access traits in
> >> the
> >> >> > subclasses.  Is there a known way to access subclass traits from
> the
> >> >> basic
> >> >> > HasTraits functions?
> >> >> >
> >> >> > Thanks.
> >> >> > _______________________________________________
> >> >> > Enthought-Dev mailing list
> >> >> > [hidden email]
> >> >> > https://mail.enthought.com/mailman/listinfo/enthought-dev
> >> >> >
> >> >>
> >> >>
> >> >>
> >> >> --
> >> >> Jonathan Rocher, PhD
> >> >> Scientific software developer
> >> >> Enthought, Inc.
> >> >> [hidden email]
> >> >> 1-512-536-1057
> >> >> http://www.enthought.com
> >> >> _______________________________________________
> >> >> Enthought-Dev mailing list
> >> >> [hidden email]
> >> >> https://mail.enthought.com/mailman/listinfo/enthought-dev
> >> >>
> >> >
> >> >
> >> _______________________________________________
> >> Enthought-Dev mailing list
> >> [hidden email]
> >> https://mail.enthought.com/mailman/listinfo/enthought-dev
> >>
> >
> >
> >
> > --
> > Jonathan Rocher, PhD
> > Scientific software developer
> > Enthought, Inc.
> > [hidden email]
> > 1-512-536-1057
> > http://www.enthought.com
> >
> >
>
>
> --
> Jonathan Rocher, PhD
> Scientific software developer
> Enthought, Inc.
> [hidden email]
> 1-512-536-1057
> http://www.enthought.com
> _______________________________________________
> Enthought-Dev mailing list
> [hidden email]
> https://mail.enthought.com/mailman/listinfo/enthought-dev
>
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Using HasTraits functions to set extended trait names

Adam Hughes
Works great, thanks!

from traits.api import *
from traitsui.api import *
from random import random

class SubClass(HasTraits):
    a=Float(1.0)

class Main(HasTraits):
    b=Float(2.0)
    sub=Instance(SubClass,())
        user_spec_traits=Str('sub.a')

    set_random=Button

    def _set_random_fired(self):
        split=self.user_spec_traits.split('.')
        self.setty( eval('self.'+split[0]), str(split[1]) )
    def setty(self, obj, trait):
        setattr(obj, trait, random())

    traits_view=View(
                Item('user_spec_traits'),Item('b'), Item('sub',
style='custom'), Item('set_random')
            )

Main().configure_traits()

I like this because it potentially presents a general formalism for
simulations over builtin quantities.  For example I could mask a bunch of
deeply-built-in traits to the user, and they could just select the ones
they want to include in a simulation.

Anyway, thanks again

On Wed, Feb 1, 2012 at 8:16 PM, Adam Hughes <[hidden email]> wrote:

> Thanks Jonathan, I appreciate your help very much.
>
>
> On Wed, Feb 1, 2012 at 6:52 PM, Jonathan Rocher <[hidden email]>wrote:
>
>> To finish, the reason why what you tried doesn't work is that setattr
>> doesn't take dotted notation in the second argument. So if you get the
>> string you are describing, you would have to split it on the last dot, and
>> do an eval("self"+first part) for the first argument and the second part
>> for the second argument to setattr. Didn't try it but I don't see why that
>> wouldn't work.
>>
>> HTH,
>> Jonathan
>>
>> On Wed, Feb 1, 2012 at 5:42 PM, Jonathan Rocher <[hidden email]
>> >wrote:
>>
>> > Hi again Adam,
>> >
>> > I see now. The way I would do it is to replace
>> >        self.set(**anew) #Doesn't work
>> > by
>> >        setattr(self.sub, 'a', random())
>> >
>> > In fact that's what the set/trait_set method does: simply call setattr
>> > dealing with change notification around it. It wouldn't be that hard to
>> > extend the method to accept the input you are suggesting.
>> >
>> > Jonathan
>> >
>> >
>> > On Wed, Feb 1, 2012 at 10:47 AM, Adam Hughes <[hidden email]
>> >wrote:
>> >
>> >> I've made a working example.  This code uses a random number generator
>> to
>> >> set trait values, and when the trait is a trait of the main class, it
>> >> works; however, when it is a trait of the subclass, it doesn't.  I've
>> >> chosen this method because I think it's very amenable to simulations;
>> all
>> >> the user does is specify the trait he/she wants as a string, rather
>> than
>> >> opening the source and referring to the trait variable explicitly.
>>  Aka I
>> >> realize I could make this work by saying "self.sub.a.set()" but I'd
>> rather
>> >> use this notation if possible.
>> >>
>> >> from traits.api import *
>> >> from traitsui.api import *
>> >> from random import random
>> >>
>> >> class SubClass(HasTraits):
>> >>    a=Float(1.0)
>> >>
>> >> class Main(HasTraits):
>> >>    b=Float(2.0)
>> >>    sub=Instance(SubClass,())
>> >>
>> >>    set_random=Button
>> >>
>> >>    def _set_random_fired(self):
>> >>        bnew={'b':random()}
>> >>        self.set(**bnew)  #Works
>> >>
>> >>        anew={'sub.a':random()}
>> >>        self.set(**anew) #Doesn't work
>> >>
>> >>    traits_view=View(
>> >>                Item('b'), Item('sub', style='custom'),
>> Item('set_random')
>> >>            )
>> >>
>> >> Main().configure_traits()
>> >>
>> >> On Tue, Jan 31, 2012 at 8:33 PM, Adam Hughes <[hidden email]
>> >> >wrote:
>> >>
>> >> > Jonathan,
>> >> >
>> >> > Thanks for getting back to me.  I'll get an example out tomorrow,
>> sorry
>> >> > for the delay.
>> >> >
>> >> >
>> >> > On Tue, Jan 31, 2012 at 5:03 PM, Jonathan Rocher <
>> [hidden email]
>> >> >wrote:
>> >> >
>> >> >> Hi Adam,
>> >> >>
>> >> >> Do you have a sample code to make sure I understand what you are
>> >> trying to
>> >> >> do? Why could you not use getattr and setattr?
>> >> >>
>> >> >> Thanks,
>> >> >> Jonathan
>> >> >>
>> >> >> On Mon, Jan 30, 2012 at 7:30 PM, Adam Hughes <
>> [hidden email]
>> >> >> >wrote:
>> >> >>
>> >> >> > Hi,
>> >> >> >
>> >> >> > I've been using some of the built-in features of HasTraits to run
>> >> >> > simulations, mainly by using trait_get() and trait_set() methods.
>> >>  I've
>> >> >> > designed a structure that I think is fairly powerful for
>> simulations,
>> >> >> > because it allows a user to specify a set of ranges for valid
>> float
>> >> >> traits,
>> >> >> > then the simulation will update all of these traits step by step,
>> so
>> >> >> it's
>> >> >> > very easy to run simulations over many different types of
>> variables
>> >> in
>> >> >> the
>> >> >> > program using this same framework.  All the user has to provide
>> are
>> >> the
>> >> >> > correct trait names.
>> >> >> >
>> >> >> > For example, if I had traits A, B my simulation would update them
>> >> both
>> >> >> as
>> >> >> > it runs along.  In practice, my objects are fairly complex, so the
>> >> >> traits
>> >> >> > that I really want to iterate over a buried deep in the class
>> >> heirarchy.
>> >> >> > In reality, I might want to run a simulation that varies A, and
>> the
>> >> >> trait
>> >> >> > B.C.D where D is deep in the class heirarchy, a sub-sub-class of
>> B.
>> >> >> >
>> >> >> > The trait_get, and trait_set work() fine when I provide class
>> trait
>> >> >> names
>> >> >> > like 'A' and 'B', but doesn't work with the extended notation as
>> far
>> >> as
>> >> >> I
>> >> >> > can tell (e.g. trait 'B.C.D').  Also I noticed that when I list
>> trait
>> >> >> names
>> >> >> > using the .trait_names() method, it also does not access traits in
>> >> the
>> >> >> > subclasses.  Is there a known way to access subclass traits from
>> the
>> >> >> basic
>> >> >> > HasTraits functions?
>> >> >> >
>> >> >> > Thanks.
>> >> >> > _______________________________________________
>> >> >> > Enthought-Dev mailing list
>> >> >> > [hidden email]
>> >> >> > https://mail.enthought.com/mailman/listinfo/enthought-dev
>> >> >> >
>> >> >>
>> >> >>
>> >> >>
>> >> >> --
>> >> >> Jonathan Rocher, PhD
>> >> >> Scientific software developer
>> >> >> Enthought, Inc.
>> >> >> [hidden email]
>> >> >> 1-512-536-1057
>> >> >> http://www.enthought.com
>> >> >> _______________________________________________
>> >> >> Enthought-Dev mailing list
>> >> >> [hidden email]
>> >> >> https://mail.enthought.com/mailman/listinfo/enthought-dev
>> >> >>
>> >> >
>> >> >
>> >> _______________________________________________
>> >> Enthought-Dev mailing list
>> >> [hidden email]
>> >> https://mail.enthought.com/mailman/listinfo/enthought-dev
>> >>
>> >
>> >
>> >
>> > --
>> > Jonathan Rocher, PhD
>> > Scientific software developer
>> > Enthought, Inc.
>> > [hidden email]
>> > 1-512-536-1057
>> > http://www.enthought.com
>> >
>> >
>>
>>
>> --
>> Jonathan Rocher, PhD
>> Scientific software developer
>> Enthought, Inc.
>> [hidden email]
>> 1-512-536-1057
>> http://www.enthought.com
>> _______________________________________________
>> Enthought-Dev mailing list
>> [hidden email]
>> https://mail.enthought.com/mailman/listinfo/enthought-dev
>>
>
>
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Using HasTraits functions to set extended trait names

Jonathan Rocher
Good. Note that the code below only works with 1 level of attribute depth.
For any depth, replace
self.setty( eval('self.'+split[0]), str(split[1]) )
with
self.setty( eval('self.'+".".join(split[:-1])), str(split[-1]) )

Jonathan

On Thu, Feb 2, 2012 at 2:20 PM, Adam Hughes <[hidden email]> wrote:

> Works great, thanks!
>
> from traits.api import *
> from traitsui.api import *
> from random import random
>
> class SubClass(HasTraits):
>    a=Float(1.0)
>
> class Main(HasTraits):
>    b=Float(2.0)
>    sub=Instance(SubClass,())
>         user_spec_traits=Str('sub.a')
>
>    set_random=Button
>
>    def _set_random_fired(self):
>        split=self.user_spec_traits.split('.')
>        self.setty( eval('self.'+split[0]), str(split[1]) )
>    def setty(self, obj, trait):
>        setattr(obj, trait, random())
>
>    traits_view=View(
>                Item('user_spec_traits'),Item('b'), Item('sub',
> style='custom'), Item('set_random')
>            )
>
> Main().configure_traits()
>
> I like this because it potentially presents a general formalism for
> simulations over builtin quantities.  For example I could mask a bunch of
> deeply-built-in traits to the user, and they could just select the ones
> they want to include in a simulation.
>
> Anyway, thanks again
>
> On Wed, Feb 1, 2012 at 8:16 PM, Adam Hughes <[hidden email]>
> wrote:
>
> > Thanks Jonathan, I appreciate your help very much.
> >
> >
> > On Wed, Feb 1, 2012 at 6:52 PM, Jonathan Rocher <[hidden email]
> >wrote:
> >
> >> To finish, the reason why what you tried doesn't work is that setattr
> >> doesn't take dotted notation in the second argument. So if you get the
> >> string you are describing, you would have to split it on the last dot,
> and
> >> do an eval("self"+first part) for the first argument and the second part
> >> for the second argument to setattr. Didn't try it but I don't see why
> that
> >> wouldn't work.
> >>
> >> HTH,
> >> Jonathan
> >>
> >> On Wed, Feb 1, 2012 at 5:42 PM, Jonathan Rocher <[hidden email]
> >> >wrote:
> >>
> >> > Hi again Adam,
> >> >
> >> > I see now. The way I would do it is to replace
> >> >        self.set(**anew) #Doesn't work
> >> > by
> >> >        setattr(self.sub, 'a', random())
> >> >
> >> > In fact that's what the set/trait_set method does: simply call setattr
> >> > dealing with change notification around it. It wouldn't be that hard
> to
> >> > extend the method to accept the input you are suggesting.
> >> >
> >> > Jonathan
> >> >
> >> >
> >> > On Wed, Feb 1, 2012 at 10:47 AM, Adam Hughes <[hidden email]
> >> >wrote:
> >> >
> >> >> I've made a working example.  This code uses a random number
> generator
> >> to
> >> >> set trait values, and when the trait is a trait of the main class, it
> >> >> works; however, when it is a trait of the subclass, it doesn't.  I've
> >> >> chosen this method because I think it's very amenable to simulations;
> >> all
> >> >> the user does is specify the trait he/she wants as a string, rather
> >> than
> >> >> opening the source and referring to the trait variable explicitly.
> >>  Aka I
> >> >> realize I could make this work by saying "self.sub.a.set()" but I'd
> >> rather
> >> >> use this notation if possible.
> >> >>
> >> >> from traits.api import *
> >> >> from traitsui.api import *
> >> >> from random import random
> >> >>
> >> >> class SubClass(HasTraits):
> >> >>    a=Float(1.0)
> >> >>
> >> >> class Main(HasTraits):
> >> >>    b=Float(2.0)
> >> >>    sub=Instance(SubClass,())
> >> >>
> >> >>    set_random=Button
> >> >>
> >> >>    def _set_random_fired(self):
> >> >>        bnew={'b':random()}
> >> >>        self.set(**bnew)  #Works
> >> >>
> >> >>        anew={'sub.a':random()}
> >> >>        self.set(**anew) #Doesn't work
> >> >>
> >> >>    traits_view=View(
> >> >>                Item('b'), Item('sub', style='custom'),
> >> Item('set_random')
> >> >>            )
> >> >>
> >> >> Main().configure_traits()
> >> >>
> >> >> On Tue, Jan 31, 2012 at 8:33 PM, Adam Hughes <
> [hidden email]
> >> >> >wrote:
> >> >>
> >> >> > Jonathan,
> >> >> >
> >> >> > Thanks for getting back to me.  I'll get an example out tomorrow,
> >> sorry
> >> >> > for the delay.
> >> >> >
> >> >> >
> >> >> > On Tue, Jan 31, 2012 at 5:03 PM, Jonathan Rocher <
> >> [hidden email]
> >> >> >wrote:
> >> >> >
> >> >> >> Hi Adam,
> >> >> >>
> >> >> >> Do you have a sample code to make sure I understand what you are
> >> >> trying to
> >> >> >> do? Why could you not use getattr and setattr?
> >> >> >>
> >> >> >> Thanks,
> >> >> >> Jonathan
> >> >> >>
> >> >> >> On Mon, Jan 30, 2012 at 7:30 PM, Adam Hughes <
> >> [hidden email]
> >> >> >> >wrote:
> >> >> >>
> >> >> >> > Hi,
> >> >> >> >
> >> >> >> > I've been using some of the built-in features of HasTraits to
> run
> >> >> >> > simulations, mainly by using trait_get() and trait_set()
> methods.
> >> >>  I've
> >> >> >> > designed a structure that I think is fairly powerful for
> >> simulations,
> >> >> >> > because it allows a user to specify a set of ranges for valid
> >> float
> >> >> >> traits,
> >> >> >> > then the simulation will update all of these traits step by
> step,
> >> so
> >> >> >> it's
> >> >> >> > very easy to run simulations over many different types of
> >> variables
> >> >> in
> >> >> >> the
> >> >> >> > program using this same framework.  All the user has to provide
> >> are
> >> >> the
> >> >> >> > correct trait names.
> >> >> >> >
> >> >> >> > For example, if I had traits A, B my simulation would update
> them
> >> >> both
> >> >> >> as
> >> >> >> > it runs along.  In practice, my objects are fairly complex, so
> the
> >> >> >> traits
> >> >> >> > that I really want to iterate over a buried deep in the class
> >> >> heirarchy.
> >> >> >> > In reality, I might want to run a simulation that varies A, and
> >> the
> >> >> >> trait
> >> >> >> > B.C.D where D is deep in the class heirarchy, a sub-sub-class of
> >> B.
> >> >> >> >
> >> >> >> > The trait_get, and trait_set work() fine when I provide class
> >> trait
> >> >> >> names
> >> >> >> > like 'A' and 'B', but doesn't work with the extended notation as
> >> far
> >> >> as
> >> >> >> I
> >> >> >> > can tell (e.g. trait 'B.C.D').  Also I noticed that when I list
> >> trait
> >> >> >> names
> >> >> >> > using the .trait_names() method, it also does not access traits
> in
> >> >> the
> >> >> >> > subclasses.  Is there a known way to access subclass traits from
> >> the
> >> >> >> basic
> >> >> >> > HasTraits functions?
> >> >> >> >
> >> >> >> > Thanks.
> >> >> >> > _______________________________________________
> >> >> >> > Enthought-Dev mailing list
> >> >> >> > [hidden email]
> >> >> >> > https://mail.enthought.com/mailman/listinfo/enthought-dev
> >> >> >> >
> >> >> >>
> >> >> >>
> >> >> >>
> >> >> >> --
> >> >> >> Jonathan Rocher, PhD
> >> >> >> Scientific software developer
> >> >> >> Enthought, Inc.
> >> >> >> [hidden email]
> >> >> >> 1-512-536-1057
> >> >> >> http://www.enthought.com
> >> >> >> _______________________________________________
> >> >> >> Enthought-Dev mailing list
> >> >> >> [hidden email]
> >> >> >> https://mail.enthought.com/mailman/listinfo/enthought-dev
> >> >> >>
> >> >> >
> >> >> >
> >> >> _______________________________________________
> >> >> Enthought-Dev mailing list
> >> >> [hidden email]
> >> >> https://mail.enthought.com/mailman/listinfo/enthought-dev
> >> >>
> >> >
> >> >
> >> >
> >> > --
> >> > Jonathan Rocher, PhD
> >> > Scientific software developer
> >> > Enthought, Inc.
> >> > [hidden email]
> >> > 1-512-536-1057
> >> > http://www.enthought.com
> >> >
> >> >
> >>
> >>
> >> --
> >> Jonathan Rocher, PhD
> >> Scientific software developer
> >> Enthought, Inc.
> >> [hidden email]
> >> 1-512-536-1057
> >> http://www.enthought.com
> >> _______________________________________________
> >> Enthought-Dev mailing list
> >> [hidden email]
> >> https://mail.enthought.com/mailman/listinfo/enthought-dev
> >>
> >
> >
> _______________________________________________
> Enthought-Dev mailing list
> [hidden email]
> https://mail.enthought.com/mailman/listinfo/enthought-dev
>



--
Jonathan Rocher, PhD
Scientific software developer
Enthought, Inc.
[hidden email]
1-512-536-1057
http://www.enthought.com
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Using HasTraits functions to set extended trait names

Adam Hughes
Awesome, was just going to try to figure that one out next.

Thanks for saving me an hour of work.

On Thu, Feb 2, 2012 at 3:26 PM, Jonathan Rocher <[hidden email]>wrote:

> Good. Note that the code below only works with 1 level of attribute depth.
> For any depth, replace
> self.setty( eval('self.'+split[0]), str(split[1]) )
> with
> self.setty( eval('self.'+".".join(split[:-1])), str(split[-1]) )
>
> Jonathan
>
> On Thu, Feb 2, 2012 at 2:20 PM, Adam Hughes <[hidden email]>
> wrote:
>
> > Works great, thanks!
> >
> > from traits.api import *
> > from traitsui.api import *
> > from random import random
> >
> > class SubClass(HasTraits):
> >    a=Float(1.0)
> >
> > class Main(HasTraits):
> >    b=Float(2.0)
> >    sub=Instance(SubClass,())
> >         user_spec_traits=Str('sub.a')
> >
> >    set_random=Button
> >
> >    def _set_random_fired(self):
> >        split=self.user_spec_traits.split('.')
> >        self.setty( eval('self.'+split[0]), str(split[1]) )
> >    def setty(self, obj, trait):
> >        setattr(obj, trait, random())
> >
> >    traits_view=View(
> >                Item('user_spec_traits'),Item('b'), Item('sub',
> > style='custom'), Item('set_random')
> >            )
> >
> > Main().configure_traits()
> >
> > I like this because it potentially presents a general formalism for
> > simulations over builtin quantities.  For example I could mask a bunch of
> > deeply-built-in traits to the user, and they could just select the ones
> > they want to include in a simulation.
> >
> > Anyway, thanks again
> >
> > On Wed, Feb 1, 2012 at 8:16 PM, Adam Hughes <[hidden email]>
> > wrote:
> >
> > > Thanks Jonathan, I appreciate your help very much.
> > >
> > >
> > > On Wed, Feb 1, 2012 at 6:52 PM, Jonathan Rocher <[hidden email]
> > >wrote:
> > >
> > >> To finish, the reason why what you tried doesn't work is that setattr
> > >> doesn't take dotted notation in the second argument. So if you get the
> > >> string you are describing, you would have to split it on the last dot,
> > and
> > >> do an eval("self"+first part) for the first argument and the second
> part
> > >> for the second argument to setattr. Didn't try it but I don't see why
> > that
> > >> wouldn't work.
> > >>
> > >> HTH,
> > >> Jonathan
> > >>
> > >> On Wed, Feb 1, 2012 at 5:42 PM, Jonathan Rocher <
> [hidden email]
> > >> >wrote:
> > >>
> > >> > Hi again Adam,
> > >> >
> > >> > I see now. The way I would do it is to replace
> > >> >        self.set(**anew) #Doesn't work
> > >> > by
> > >> >        setattr(self.sub, 'a', random())
> > >> >
> > >> > In fact that's what the set/trait_set method does: simply call
> setattr
> > >> > dealing with change notification around it. It wouldn't be that hard
> > to
> > >> > extend the method to accept the input you are suggesting.
> > >> >
> > >> > Jonathan
> > >> >
> > >> >
> > >> > On Wed, Feb 1, 2012 at 10:47 AM, Adam Hughes <
> [hidden email]
> > >> >wrote:
> > >> >
> > >> >> I've made a working example.  This code uses a random number
> > generator
> > >> to
> > >> >> set trait values, and when the trait is a trait of the main class,
> it
> > >> >> works; however, when it is a trait of the subclass, it doesn't.
>  I've
> > >> >> chosen this method because I think it's very amenable to
> simulations;
> > >> all
> > >> >> the user does is specify the trait he/she wants as a string, rather
> > >> than
> > >> >> opening the source and referring to the trait variable explicitly.
> > >>  Aka I
> > >> >> realize I could make this work by saying "self.sub.a.set()" but I'd
> > >> rather
> > >> >> use this notation if possible.
> > >> >>
> > >> >> from traits.api import *
> > >> >> from traitsui.api import *
> > >> >> from random import random
> > >> >>
> > >> >> class SubClass(HasTraits):
> > >> >>    a=Float(1.0)
> > >> >>
> > >> >> class Main(HasTraits):
> > >> >>    b=Float(2.0)
> > >> >>    sub=Instance(SubClass,())
> > >> >>
> > >> >>    set_random=Button
> > >> >>
> > >> >>    def _set_random_fired(self):
> > >> >>        bnew={'b':random()}
> > >> >>        self.set(**bnew)  #Works
> > >> >>
> > >> >>        anew={'sub.a':random()}
> > >> >>        self.set(**anew) #Doesn't work
> > >> >>
> > >> >>    traits_view=View(
> > >> >>                Item('b'), Item('sub', style='custom'),
> > >> Item('set_random')
> > >> >>            )
> > >> >>
> > >> >> Main().configure_traits()
> > >> >>
> > >> >> On Tue, Jan 31, 2012 at 8:33 PM, Adam Hughes <
> > [hidden email]
> > >> >> >wrote:
> > >> >>
> > >> >> > Jonathan,
> > >> >> >
> > >> >> > Thanks for getting back to me.  I'll get an example out tomorrow,
> > >> sorry
> > >> >> > for the delay.
> > >> >> >
> > >> >> >
> > >> >> > On Tue, Jan 31, 2012 at 5:03 PM, Jonathan Rocher <
> > >> [hidden email]
> > >> >> >wrote:
> > >> >> >
> > >> >> >> Hi Adam,
> > >> >> >>
> > >> >> >> Do you have a sample code to make sure I understand what you are
> > >> >> trying to
> > >> >> >> do? Why could you not use getattr and setattr?
> > >> >> >>
> > >> >> >> Thanks,
> > >> >> >> Jonathan
> > >> >> >>
> > >> >> >> On Mon, Jan 30, 2012 at 7:30 PM, Adam Hughes <
> > >> [hidden email]
> > >> >> >> >wrote:
> > >> >> >>
> > >> >> >> > Hi,
> > >> >> >> >
> > >> >> >> > I've been using some of the built-in features of HasTraits to
> > run
> > >> >> >> > simulations, mainly by using trait_get() and trait_set()
> > methods.
> > >> >>  I've
> > >> >> >> > designed a structure that I think is fairly powerful for
> > >> simulations,
> > >> >> >> > because it allows a user to specify a set of ranges for valid
> > >> float
> > >> >> >> traits,
> > >> >> >> > then the simulation will update all of these traits step by
> > step,
> > >> so
> > >> >> >> it's
> > >> >> >> > very easy to run simulations over many different types of
> > >> variables
> > >> >> in
> > >> >> >> the
> > >> >> >> > program using this same framework.  All the user has to
> provide
> > >> are
> > >> >> the
> > >> >> >> > correct trait names.
> > >> >> >> >
> > >> >> >> > For example, if I had traits A, B my simulation would update
> > them
> > >> >> both
> > >> >> >> as
> > >> >> >> > it runs along.  In practice, my objects are fairly complex, so
> > the
> > >> >> >> traits
> > >> >> >> > that I really want to iterate over a buried deep in the class
> > >> >> heirarchy.
> > >> >> >> > In reality, I might want to run a simulation that varies A,
> and
> > >> the
> > >> >> >> trait
> > >> >> >> > B.C.D where D is deep in the class heirarchy, a sub-sub-class
> of
> > >> B.
> > >> >> >> >
> > >> >> >> > The trait_get, and trait_set work() fine when I provide class
> > >> trait
> > >> >> >> names
> > >> >> >> > like 'A' and 'B', but doesn't work with the extended notation
> as
> > >> far
> > >> >> as
> > >> >> >> I
> > >> >> >> > can tell (e.g. trait 'B.C.D').  Also I noticed that when I
> list
> > >> trait
> > >> >> >> names
> > >> >> >> > using the .trait_names() method, it also does not access
> traits
> > in
> > >> >> the
> > >> >> >> > subclasses.  Is there a known way to access subclass traits
> from
> > >> the
> > >> >> >> basic
> > >> >> >> > HasTraits functions?
> > >> >> >> >
> > >> >> >> > Thanks.
> > >> >> >> > _______________________________________________
> > >> >> >> > Enthought-Dev mailing list
> > >> >> >> > [hidden email]
> > >> >> >> > https://mail.enthought.com/mailman/listinfo/enthought-dev
> > >> >> >> >
> > >> >> >>
> > >> >> >>
> > >> >> >>
> > >> >> >> --
> > >> >> >> Jonathan Rocher, PhD
> > >> >> >> Scientific software developer
> > >> >> >> Enthought, Inc.
> > >> >> >> [hidden email]
> > >> >> >> 1-512-536-1057
> > >> >> >> http://www.enthought.com
> > >> >> >> _______________________________________________
> > >> >> >> Enthought-Dev mailing list
> > >> >> >> [hidden email]
> > >> >> >> https://mail.enthought.com/mailman/listinfo/enthought-dev
> > >> >> >>
> > >> >> >
> > >> >> >
> > >> >> _______________________________________________
> > >> >> Enthought-Dev mailing list
> > >> >> [hidden email]
> > >> >> https://mail.enthought.com/mailman/listinfo/enthought-dev
> > >> >>
> > >> >
> > >> >
> > >> >
> > >> > --
> > >> > Jonathan Rocher, PhD
> > >> > Scientific software developer
> > >> > Enthought, Inc.
> > >> > [hidden email]
> > >> > 1-512-536-1057
> > >> > http://www.enthought.com
> > >> >
> > >> >
> > >>
> > >>
> > >> --
> > >> Jonathan Rocher, PhD
> > >> Scientific software developer
> > >> Enthought, Inc.
> > >> [hidden email]
> > >> 1-512-536-1057
> > >> http://www.enthought.com
> > >> _______________________________________________
> > >> Enthought-Dev mailing list
> > >> [hidden email]
> > >> https://mail.enthought.com/mailman/listinfo/enthought-dev
> > >>
> > >
> > >
> > _______________________________________________
> > Enthought-Dev mailing list
> > [hidden email]
> > https://mail.enthought.com/mailman/listinfo/enthought-dev
> >
>
>
>
> --
> Jonathan Rocher, PhD
> Scientific software developer
> Enthought, Inc.
> [hidden email]
> 1-512-536-1057
> http://www.enthought.com
> _______________________________________________
> Enthought-Dev mailing list
> [hidden email]
> https://mail.enthought.com/mailman/listinfo/enthought-dev
>
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Using HasTraits functions to set extended trait names

Simon Jagoe
Adam,

There is actually a setattr-like function in traits to do this already.

from traits.trait_base import xsetattr

xsetattr(my_obj, 'foo.bar.baz', value)

On Thu, Feb 2, 2012 at 3:28 PM, Adam Hughes <[hidden email]> wrote:

> Awesome, was just going to try to figure that one out next.
>
> Thanks for saving me an hour of work.
>
> On Thu, Feb 2, 2012 at 3:26 PM, Jonathan Rocher <[hidden email]
> >wrote:
>
> > Good. Note that the code below only works with 1 level of attribute
> depth.
> > For any depth, replace
> > self.setty( eval('self.'+split[0]), str(split[1]) )
> > with
> > self.setty( eval('self.'+".".join(split[:-1])), str(split[-1]) )
> >
> > Jonathan
> >
> > On Thu, Feb 2, 2012 at 2:20 PM, Adam Hughes <[hidden email]>
> > wrote:
> >
> > > Works great, thanks!
> > >
> > > from traits.api import *
> > > from traitsui.api import *
> > > from random import random
> > >
> > > class SubClass(HasTraits):
> > >    a=Float(1.0)
> > >
> > > class Main(HasTraits):
> > >    b=Float(2.0)
> > >    sub=Instance(SubClass,())
> > >         user_spec_traits=Str('sub.a')
> > >
> > >    set_random=Button
> > >
> > >    def _set_random_fired(self):
> > >        split=self.user_spec_traits.split('.')
> > >        self.setty( eval('self.'+split[0]), str(split[1]) )
> > >    def setty(self, obj, trait):
> > >        setattr(obj, trait, random())
> > >
> > >    traits_view=View(
> > >                Item('user_spec_traits'),Item('b'), Item('sub',
> > > style='custom'), Item('set_random')
> > >            )
> > >
> > > Main().configure_traits()
> > >
> > > I like this because it potentially presents a general formalism for
> > > simulations over builtin quantities.  For example I could mask a bunch
> of
> > > deeply-built-in traits to the user, and they could just select the ones
> > > they want to include in a simulation.
> > >
> > > Anyway, thanks again
> > >
> > > On Wed, Feb 1, 2012 at 8:16 PM, Adam Hughes <[hidden email]>
> > > wrote:
> > >
> > > > Thanks Jonathan, I appreciate your help very much.
> > > >
> > > >
> > > > On Wed, Feb 1, 2012 at 6:52 PM, Jonathan Rocher <
> [hidden email]
> > > >wrote:
> > > >
> > > >> To finish, the reason why what you tried doesn't work is that
> setattr
> > > >> doesn't take dotted notation in the second argument. So if you get
> the
> > > >> string you are describing, you would have to split it on the last
> dot,
> > > and
> > > >> do an eval("self"+first part) for the first argument and the second
> > part
> > > >> for the second argument to setattr. Didn't try it but I don't see
> why
> > > that
> > > >> wouldn't work.
> > > >>
> > > >> HTH,
> > > >> Jonathan
> > > >>
> > > >> On Wed, Feb 1, 2012 at 5:42 PM, Jonathan Rocher <
> > [hidden email]
> > > >> >wrote:
> > > >>
> > > >> > Hi again Adam,
> > > >> >
> > > >> > I see now. The way I would do it is to replace
> > > >> >        self.set(**anew) #Doesn't work
> > > >> > by
> > > >> >        setattr(self.sub, 'a', random())
> > > >> >
> > > >> > In fact that's what the set/trait_set method does: simply call
> > setattr
> > > >> > dealing with change notification around it. It wouldn't be that
> hard
> > > to
> > > >> > extend the method to accept the input you are suggesting.
> > > >> >
> > > >> > Jonathan
> > > >> >
> > > >> >
> > > >> > On Wed, Feb 1, 2012 at 10:47 AM, Adam Hughes <
> > [hidden email]
> > > >> >wrote:
> > > >> >
> > > >> >> I've made a working example.  This code uses a random number
> > > generator
> > > >> to
> > > >> >> set trait values, and when the trait is a trait of the main
> class,
> > it
> > > >> >> works; however, when it is a trait of the subclass, it doesn't.
> >  I've
> > > >> >> chosen this method because I think it's very amenable to
> > simulations;
> > > >> all
> > > >> >> the user does is specify the trait he/she wants as a string,
> rather
> > > >> than
> > > >> >> opening the source and referring to the trait variable
> explicitly.
> > > >>  Aka I
> > > >> >> realize I could make this work by saying "self.sub.a.set()" but
> I'd
> > > >> rather
> > > >> >> use this notation if possible.
> > > >> >>
> > > >> >> from traits.api import *
> > > >> >> from traitsui.api import *
> > > >> >> from random import random
> > > >> >>
> > > >> >> class SubClass(HasTraits):
> > > >> >>    a=Float(1.0)
> > > >> >>
> > > >> >> class Main(HasTraits):
> > > >> >>    b=Float(2.0)
> > > >> >>    sub=Instance(SubClass,())
> > > >> >>
> > > >> >>    set_random=Button
> > > >> >>
> > > >> >>    def _set_random_fired(self):
> > > >> >>        bnew={'b':random()}
> > > >> >>        self.set(**bnew)  #Works
> > > >> >>
> > > >> >>        anew={'sub.a':random()}
> > > >> >>        self.set(**anew) #Doesn't work
> > > >> >>
> > > >> >>    traits_view=View(
> > > >> >>                Item('b'), Item('sub', style='custom'),
> > > >> Item('set_random')
> > > >> >>            )
> > > >> >>
> > > >> >> Main().configure_traits()
> > > >> >>
> > > >> >> On Tue, Jan 31, 2012 at 8:33 PM, Adam Hughes <
> > > [hidden email]
> > > >> >> >wrote:
> > > >> >>
> > > >> >> > Jonathan,
> > > >> >> >
> > > >> >> > Thanks for getting back to me.  I'll get an example out
> tomorrow,
> > > >> sorry
> > > >> >> > for the delay.
> > > >> >> >
> > > >> >> >
> > > >> >> > On Tue, Jan 31, 2012 at 5:03 PM, Jonathan Rocher <
> > > >> [hidden email]
> > > >> >> >wrote:
> > > >> >> >
> > > >> >> >> Hi Adam,
> > > >> >> >>
> > > >> >> >> Do you have a sample code to make sure I understand what you
> are
> > > >> >> trying to
> > > >> >> >> do? Why could you not use getattr and setattr?
> > > >> >> >>
> > > >> >> >> Thanks,
> > > >> >> >> Jonathan
> > > >> >> >>
> > > >> >> >> On Mon, Jan 30, 2012 at 7:30 PM, Adam Hughes <
> > > >> [hidden email]
> > > >> >> >> >wrote:
> > > >> >> >>
> > > >> >> >> > Hi,
> > > >> >> >> >
> > > >> >> >> > I've been using some of the built-in features of HasTraits
> to
> > > run
> > > >> >> >> > simulations, mainly by using trait_get() and trait_set()
> > > methods.
> > > >> >>  I've
> > > >> >> >> > designed a structure that I think is fairly powerful for
> > > >> simulations,
> > > >> >> >> > because it allows a user to specify a set of ranges for
> valid
> > > >> float
> > > >> >> >> traits,
> > > >> >> >> > then the simulation will update all of these traits step by
> > > step,
> > > >> so
> > > >> >> >> it's
> > > >> >> >> > very easy to run simulations over many different types of
> > > >> variables
> > > >> >> in
> > > >> >> >> the
> > > >> >> >> > program using this same framework.  All the user has to
> > provide
> > > >> are
> > > >> >> the
> > > >> >> >> > correct trait names.
> > > >> >> >> >
> > > >> >> >> > For example, if I had traits A, B my simulation would update
> > > them
> > > >> >> both
> > > >> >> >> as
> > > >> >> >> > it runs along.  In practice, my objects are fairly complex,
> so
> > > the
> > > >> >> >> traits
> > > >> >> >> > that I really want to iterate over a buried deep in the
> class
> > > >> >> heirarchy.
> > > >> >> >> > In reality, I might want to run a simulation that varies A,
> > and
> > > >> the
> > > >> >> >> trait
> > > >> >> >> > B.C.D where D is deep in the class heirarchy, a
> sub-sub-class
> > of
> > > >> B.
> > > >> >> >> >
> > > >> >> >> > The trait_get, and trait_set work() fine when I provide
> class
> > > >> trait
> > > >> >> >> names
> > > >> >> >> > like 'A' and 'B', but doesn't work with the extended
> notation
> > as
> > > >> far
> > > >> >> as
> > > >> >> >> I
> > > >> >> >> > can tell (e.g. trait 'B.C.D').  Also I noticed that when I
> > list
> > > >> trait
> > > >> >> >> names
> > > >> >> >> > using the .trait_names() method, it also does not access
> > traits
> > > in
> > > >> >> the
> > > >> >> >> > subclasses.  Is there a known way to access subclass traits
> > from
> > > >> the
> > > >> >> >> basic
> > > >> >> >> > HasTraits functions?
> > > >> >> >> >
> > > >> >> >> > Thanks.
> > > >> >> >> > _______________________________________________
> > > >> >> >> > Enthought-Dev mailing list
> > > >> >> >> > [hidden email]
> > > >> >> >> > https://mail.enthought.com/mailman/listinfo/enthought-dev
> > > >> >> >> >
> > > >> >> >>
> > > >> >> >>
> > > >> >> >>
> > > >> >> >> --
> > > >> >> >> Jonathan Rocher, PhD
> > > >> >> >> Scientific software developer
> > > >> >> >> Enthought, Inc.
> > > >> >> >> [hidden email]
> > > >> >> >> 1-512-536-1057
> > > >> >> >> http://www.enthought.com
> > > >> >> >> _______________________________________________
> > > >> >> >> Enthought-Dev mailing list
> > > >> >> >> [hidden email]
> > > >> >> >> https://mail.enthought.com/mailman/listinfo/enthought-dev
> > > >> >> >>
> > > >> >> >
> > > >> >> >
> > > >> >> _______________________________________________
> > > >> >> Enthought-Dev mailing list
> > > >> >> [hidden email]
> > > >> >> https://mail.enthought.com/mailman/listinfo/enthought-dev
> > > >> >>
> > > >> >
> > > >> >
> > > >> >
> > > >> > --
> > > >> > Jonathan Rocher, PhD
> > > >> > Scientific software developer
> > > >> > Enthought, Inc.
> > > >> > [hidden email]
> > > >> > 1-512-536-1057
> > > >> > http://www.enthought.com
> > > >> >
> > > >> >
> > > >>
> > > >>
> > > >> --
> > > >> Jonathan Rocher, PhD
> > > >> Scientific software developer
> > > >> Enthought, Inc.
> > > >> [hidden email]
> > > >> 1-512-536-1057
> > > >> http://www.enthought.com
> > > >> _______________________________________________
> > > >> Enthought-Dev mailing list
> > > >> [hidden email]
> > > >> https://mail.enthought.com/mailman/listinfo/enthought-dev
> > > >>
> > > >
> > > >
> > > _______________________________________________
> > > Enthought-Dev mailing list
> > > [hidden email]
> > > https://mail.enthought.com/mailman/listinfo/enthought-dev
> > >
> >
> >
> >
> > --
> > Jonathan Rocher, PhD
> > Scientific software developer
> > Enthought, Inc.
> > [hidden email]
> > 1-512-536-1057
> > http://www.enthought.com
> > _______________________________________________
> > Enthought-Dev mailing list
> > [hidden email]
> > https://mail.enthought.com/mailman/listinfo/enthought-dev
> >
> _______________________________________________
> Enthought-Dev mailing list
> [hidden email]
> https://mail.enthought.com/mailman/listinfo/enthought-dev
>



--
Simon Jagoe
Enthought Ltd
+44 79 312 11 506
[hidden email]
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Using HasTraits functions to set extended trait names

Simon Jagoe
For completeness, there is also a corresponding xgetattr in the same module.

On Thu, Feb 2, 2012 at 3:33 PM, Simon Jagoe <[hidden email]> wrote:

> Adam,
>
> There is actually a setattr-like function in traits to do this already.
>
> from traits.trait_base import xsetattr
>
> xsetattr(my_obj, 'foo.bar.baz', value)
>
>
> On Thu, Feb 2, 2012 at 3:28 PM, Adam Hughes <[hidden email]>wrote:
>
>> Awesome, was just going to try to figure that one out next.
>>
>> Thanks for saving me an hour of work.
>>
>> On Thu, Feb 2, 2012 at 3:26 PM, Jonathan Rocher <[hidden email]
>> >wrote:
>>
>> > Good. Note that the code below only works with 1 level of attribute
>> depth.
>> > For any depth, replace
>> > self.setty( eval('self.'+split[0]), str(split[1]) )
>> > with
>> > self.setty( eval('self.'+".".join(split[:-1])), str(split[-1]) )
>> >
>> > Jonathan
>> >
>> > On Thu, Feb 2, 2012 at 2:20 PM, Adam Hughes <[hidden email]>
>> > wrote:
>> >
>> > > Works great, thanks!
>> > >
>> > > from traits.api import *
>> > > from traitsui.api import *
>> > > from random import random
>> > >
>> > > class SubClass(HasTraits):
>> > >    a=Float(1.0)
>> > >
>> > > class Main(HasTraits):
>> > >    b=Float(2.0)
>> > >    sub=Instance(SubClass,())
>> > >         user_spec_traits=Str('sub.a')
>> > >
>> > >    set_random=Button
>> > >
>> > >    def _set_random_fired(self):
>> > >        split=self.user_spec_traits.split('.')
>> > >        self.setty( eval('self.'+split[0]), str(split[1]) )
>> > >    def setty(self, obj, trait):
>> > >        setattr(obj, trait, random())
>> > >
>> > >    traits_view=View(
>> > >                Item('user_spec_traits'),Item('b'), Item('sub',
>> > > style='custom'), Item('set_random')
>> > >            )
>> > >
>> > > Main().configure_traits()
>> > >
>> > > I like this because it potentially presents a general formalism for
>> > > simulations over builtin quantities.  For example I could mask a
>> bunch of
>> > > deeply-built-in traits to the user, and they could just select the
>> ones
>> > > they want to include in a simulation.
>> > >
>> > > Anyway, thanks again
>> > >
>> > > On Wed, Feb 1, 2012 at 8:16 PM, Adam Hughes <[hidden email]>
>> > > wrote:
>> > >
>> > > > Thanks Jonathan, I appreciate your help very much.
>> > > >
>> > > >
>> > > > On Wed, Feb 1, 2012 at 6:52 PM, Jonathan Rocher <
>> [hidden email]
>> > > >wrote:
>> > > >
>> > > >> To finish, the reason why what you tried doesn't work is that
>> setattr
>> > > >> doesn't take dotted notation in the second argument. So if you get
>> the
>> > > >> string you are describing, you would have to split it on the last
>> dot,
>> > > and
>> > > >> do an eval("self"+first part) for the first argument and the second
>> > part
>> > > >> for the second argument to setattr. Didn't try it but I don't see
>> why
>> > > that
>> > > >> wouldn't work.
>> > > >>
>> > > >> HTH,
>> > > >> Jonathan
>> > > >>
>> > > >> On Wed, Feb 1, 2012 at 5:42 PM, Jonathan Rocher <
>> > [hidden email]
>> > > >> >wrote:
>> > > >>
>> > > >> > Hi again Adam,
>> > > >> >
>> > > >> > I see now. The way I would do it is to replace
>> > > >> >        self.set(**anew) #Doesn't work
>> > > >> > by
>> > > >> >        setattr(self.sub, 'a', random())
>> > > >> >
>> > > >> > In fact that's what the set/trait_set method does: simply call
>> > setattr
>> > > >> > dealing with change notification around it. It wouldn't be that
>> hard
>> > > to
>> > > >> > extend the method to accept the input you are suggesting.
>> > > >> >
>> > > >> > Jonathan
>> > > >> >
>> > > >> >
>> > > >> > On Wed, Feb 1, 2012 at 10:47 AM, Adam Hughes <
>> > [hidden email]
>> > > >> >wrote:
>> > > >> >
>> > > >> >> I've made a working example.  This code uses a random number
>> > > generator
>> > > >> to
>> > > >> >> set trait values, and when the trait is a trait of the main
>> class,
>> > it
>> > > >> >> works; however, when it is a trait of the subclass, it doesn't.
>> >  I've
>> > > >> >> chosen this method because I think it's very amenable to
>> > simulations;
>> > > >> all
>> > > >> >> the user does is specify the trait he/she wants as a string,
>> rather
>> > > >> than
>> > > >> >> opening the source and referring to the trait variable
>> explicitly.
>> > > >>  Aka I
>> > > >> >> realize I could make this work by saying "self.sub.a.set()" but
>> I'd
>> > > >> rather
>> > > >> >> use this notation if possible.
>> > > >> >>
>> > > >> >> from traits.api import *
>> > > >> >> from traitsui.api import *
>> > > >> >> from random import random
>> > > >> >>
>> > > >> >> class SubClass(HasTraits):
>> > > >> >>    a=Float(1.0)
>> > > >> >>
>> > > >> >> class Main(HasTraits):
>> > > >> >>    b=Float(2.0)
>> > > >> >>    sub=Instance(SubClass,())
>> > > >> >>
>> > > >> >>    set_random=Button
>> > > >> >>
>> > > >> >>    def _set_random_fired(self):
>> > > >> >>        bnew={'b':random()}
>> > > >> >>        self.set(**bnew)  #Works
>> > > >> >>
>> > > >> >>        anew={'sub.a':random()}
>> > > >> >>        self.set(**anew) #Doesn't work
>> > > >> >>
>> > > >> >>    traits_view=View(
>> > > >> >>                Item('b'), Item('sub', style='custom'),
>> > > >> Item('set_random')
>> > > >> >>            )
>> > > >> >>
>> > > >> >> Main().configure_traits()
>> > > >> >>
>> > > >> >> On Tue, Jan 31, 2012 at 8:33 PM, Adam Hughes <
>> > > [hidden email]
>> > > >> >> >wrote:
>> > > >> >>
>> > > >> >> > Jonathan,
>> > > >> >> >
>> > > >> >> > Thanks for getting back to me.  I'll get an example out
>> tomorrow,
>> > > >> sorry
>> > > >> >> > for the delay.
>> > > >> >> >
>> > > >> >> >
>> > > >> >> > On Tue, Jan 31, 2012 at 5:03 PM, Jonathan Rocher <
>> > > >> [hidden email]
>> > > >> >> >wrote:
>> > > >> >> >
>> > > >> >> >> Hi Adam,
>> > > >> >> >>
>> > > >> >> >> Do you have a sample code to make sure I understand what you
>> are
>> > > >> >> trying to
>> > > >> >> >> do? Why could you not use getattr and setattr?
>> > > >> >> >>
>> > > >> >> >> Thanks,
>> > > >> >> >> Jonathan
>> > > >> >> >>
>> > > >> >> >> On Mon, Jan 30, 2012 at 7:30 PM, Adam Hughes <
>> > > >> [hidden email]
>> > > >> >> >> >wrote:
>> > > >> >> >>
>> > > >> >> >> > Hi,
>> > > >> >> >> >
>> > > >> >> >> > I've been using some of the built-in features of HasTraits
>> to
>> > > run
>> > > >> >> >> > simulations, mainly by using trait_get() and trait_set()
>> > > methods.
>> > > >> >>  I've
>> > > >> >> >> > designed a structure that I think is fairly powerful for
>> > > >> simulations,
>> > > >> >> >> > because it allows a user to specify a set of ranges for
>> valid
>> > > >> float
>> > > >> >> >> traits,
>> > > >> >> >> > then the simulation will update all of these traits step by
>> > > step,
>> > > >> so
>> > > >> >> >> it's
>> > > >> >> >> > very easy to run simulations over many different types of
>> > > >> variables
>> > > >> >> in
>> > > >> >> >> the
>> > > >> >> >> > program using this same framework.  All the user has to
>> > provide
>> > > >> are
>> > > >> >> the
>> > > >> >> >> > correct trait names.
>> > > >> >> >> >
>> > > >> >> >> > For example, if I had traits A, B my simulation would
>> update
>> > > them
>> > > >> >> both
>> > > >> >> >> as
>> > > >> >> >> > it runs along.  In practice, my objects are fairly
>> complex, so
>> > > the
>> > > >> >> >> traits
>> > > >> >> >> > that I really want to iterate over a buried deep in the
>> class
>> > > >> >> heirarchy.
>> > > >> >> >> > In reality, I might want to run a simulation that varies A,
>> > and
>> > > >> the
>> > > >> >> >> trait
>> > > >> >> >> > B.C.D where D is deep in the class heirarchy, a
>> sub-sub-class
>> > of
>> > > >> B.
>> > > >> >> >> >
>> > > >> >> >> > The trait_get, and trait_set work() fine when I provide
>> class
>> > > >> trait
>> > > >> >> >> names
>> > > >> >> >> > like 'A' and 'B', but doesn't work with the extended
>> notation
>> > as
>> > > >> far
>> > > >> >> as
>> > > >> >> >> I
>> > > >> >> >> > can tell (e.g. trait 'B.C.D').  Also I noticed that when I
>> > list
>> > > >> trait
>> > > >> >> >> names
>> > > >> >> >> > using the .trait_names() method, it also does not access
>> > traits
>> > > in
>> > > >> >> the
>> > > >> >> >> > subclasses.  Is there a known way to access subclass traits
>> > from
>> > > >> the
>> > > >> >> >> basic
>> > > >> >> >> > HasTraits functions?
>> > > >> >> >> >
>> > > >> >> >> > Thanks.
>> > > >> >> >> > _______________________________________________
>> > > >> >> >> > Enthought-Dev mailing list
>> > > >> >> >> > [hidden email]
>> > > >> >> >> > https://mail.enthought.com/mailman/listinfo/enthought-dev
>> > > >> >> >> >
>> > > >> >> >>
>> > > >> >> >>
>> > > >> >> >>
>> > > >> >> >> --
>> > > >> >> >> Jonathan Rocher, PhD
>> > > >> >> >> Scientific software developer
>> > > >> >> >> Enthought, Inc.
>> > > >> >> >> [hidden email]
>> > > >> >> >> 1-512-536-1057
>> > > >> >> >> http://www.enthought.com
>> > > >> >> >> _______________________________________________
>> > > >> >> >> Enthought-Dev mailing list
>> > > >> >> >> [hidden email]
>> > > >> >> >> https://mail.enthought.com/mailman/listinfo/enthought-dev
>> > > >> >> >>
>> > > >> >> >
>> > > >> >> >
>> > > >> >> _______________________________________________
>> > > >> >> Enthought-Dev mailing list
>> > > >> >> [hidden email]
>> > > >> >> https://mail.enthought.com/mailman/listinfo/enthought-dev
>> > > >> >>
>> > > >> >
>> > > >> >
>> > > >> >
>> > > >> > --
>> > > >> > Jonathan Rocher, PhD
>> > > >> > Scientific software developer
>> > > >> > Enthought, Inc.
>> > > >> > [hidden email]
>> > > >> > 1-512-536-1057
>> > > >> > http://www.enthought.com
>> > > >> >
>> > > >> >
>> > > >>
>> > > >>
>> > > >> --
>> > > >> Jonathan Rocher, PhD
>> > > >> Scientific software developer
>> > > >> Enthought, Inc.
>> > > >> [hidden email]
>> > > >> 1-512-536-1057
>> > > >> http://www.enthought.com
>> > > >> _______________________________________________
>> > > >> Enthought-Dev mailing list
>> > > >> [hidden email]
>> > > >> https://mail.enthought.com/mailman/listinfo/enthought-dev
>> > > >>
>> > > >
>> > > >
>> > > _______________________________________________
>> > > Enthought-Dev mailing list
>> > > [hidden email]
>> > > https://mail.enthought.com/mailman/listinfo/enthought-dev
>> > >
>> >
>> >
>> >
>> > --
>> > Jonathan Rocher, PhD
>> > Scientific software developer
>> > Enthought, Inc.
>> > [hidden email]
>> > 1-512-536-1057
>> > http://www.enthought.com
>> > _______________________________________________
>> > Enthought-Dev mailing list
>> > [hidden email]
>> > https://mail.enthought.com/mailman/listinfo/enthought-dev
>> >
>> _______________________________________________
>> Enthought-Dev mailing list
>> [hidden email]
>> https://mail.enthought.com/mailman/listinfo/enthought-dev
>>
>
>
>
> --
> Simon Jagoe
> Enthought Ltd
> +44 79 312 11 506
> [hidden email]
>



--
Simon Jagoe
Enthought Ltd
+44 79 312 11 506
[hidden email]
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Using HasTraits functions to set extended trait names

Adam Hughes
Well fancy that..!  Good to know, thanks for the heads up Simon.

On Thu, Feb 2, 2012 at 3:35 PM, Simon Jagoe <[hidden email]> wrote:

> For completeness, there is also a corresponding xgetattr in the same
> module.
>
> On Thu, Feb 2, 2012 at 3:33 PM, Simon Jagoe <[hidden email]> wrote:
>
> > Adam,
> >
> > There is actually a setattr-like function in traits to do this already.
> >
> > from traits.trait_base import xsetattr
> >
> > xsetattr(my_obj, 'foo.bar.baz', value)
> >
> >
> > On Thu, Feb 2, 2012 at 3:28 PM, Adam Hughes <[hidden email]
> >wrote:
> >
> >> Awesome, was just going to try to figure that one out next.
> >>
> >> Thanks for saving me an hour of work.
> >>
> >> On Thu, Feb 2, 2012 at 3:26 PM, Jonathan Rocher <[hidden email]
> >> >wrote:
> >>
> >> > Good. Note that the code below only works with 1 level of attribute
> >> depth.
> >> > For any depth, replace
> >> > self.setty( eval('self.'+split[0]), str(split[1]) )
> >> > with
> >> > self.setty( eval('self.'+".".join(split[:-1])), str(split[-1]) )
> >> >
> >> > Jonathan
> >> >
> >> > On Thu, Feb 2, 2012 at 2:20 PM, Adam Hughes <[hidden email]>
> >> > wrote:
> >> >
> >> > > Works great, thanks!
> >> > >
> >> > > from traits.api import *
> >> > > from traitsui.api import *
> >> > > from random import random
> >> > >
> >> > > class SubClass(HasTraits):
> >> > >    a=Float(1.0)
> >> > >
> >> > > class Main(HasTraits):
> >> > >    b=Float(2.0)
> >> > >    sub=Instance(SubClass,())
> >> > >         user_spec_traits=Str('sub.a')
> >> > >
> >> > >    set_random=Button
> >> > >
> >> > >    def _set_random_fired(self):
> >> > >        split=self.user_spec_traits.split('.')
> >> > >        self.setty( eval('self.'+split[0]), str(split[1]) )
> >> > >    def setty(self, obj, trait):
> >> > >        setattr(obj, trait, random())
> >> > >
> >> > >    traits_view=View(
> >> > >                Item('user_spec_traits'),Item('b'), Item('sub',
> >> > > style='custom'), Item('set_random')
> >> > >            )
> >> > >
> >> > > Main().configure_traits()
> >> > >
> >> > > I like this because it potentially presents a general formalism for
> >> > > simulations over builtin quantities.  For example I could mask a
> >> bunch of
> >> > > deeply-built-in traits to the user, and they could just select the
> >> ones
> >> > > they want to include in a simulation.
> >> > >
> >> > > Anyway, thanks again
> >> > >
> >> > > On Wed, Feb 1, 2012 at 8:16 PM, Adam Hughes <
> [hidden email]>
> >> > > wrote:
> >> > >
> >> > > > Thanks Jonathan, I appreciate your help very much.
> >> > > >
> >> > > >
> >> > > > On Wed, Feb 1, 2012 at 6:52 PM, Jonathan Rocher <
> >> [hidden email]
> >> > > >wrote:
> >> > > >
> >> > > >> To finish, the reason why what you tried doesn't work is that
> >> setattr
> >> > > >> doesn't take dotted notation in the second argument. So if you
> get
> >> the
> >> > > >> string you are describing, you would have to split it on the last
> >> dot,
> >> > > and
> >> > > >> do an eval("self"+first part) for the first argument and the
> second
> >> > part
> >> > > >> for the second argument to setattr. Didn't try it but I don't see
> >> why
> >> > > that
> >> > > >> wouldn't work.
> >> > > >>
> >> > > >> HTH,
> >> > > >> Jonathan
> >> > > >>
> >> > > >> On Wed, Feb 1, 2012 at 5:42 PM, Jonathan Rocher <
> >> > [hidden email]
> >> > > >> >wrote:
> >> > > >>
> >> > > >> > Hi again Adam,
> >> > > >> >
> >> > > >> > I see now. The way I would do it is to replace
> >> > > >> >        self.set(**anew) #Doesn't work
> >> > > >> > by
> >> > > >> >        setattr(self.sub, 'a', random())
> >> > > >> >
> >> > > >> > In fact that's what the set/trait_set method does: simply call
> >> > setattr
> >> > > >> > dealing with change notification around it. It wouldn't be that
> >> hard
> >> > > to
> >> > > >> > extend the method to accept the input you are suggesting.
> >> > > >> >
> >> > > >> > Jonathan
> >> > > >> >
> >> > > >> >
> >> > > >> > On Wed, Feb 1, 2012 at 10:47 AM, Adam Hughes <
> >> > [hidden email]
> >> > > >> >wrote:
> >> > > >> >
> >> > > >> >> I've made a working example.  This code uses a random number
> >> > > generator
> >> > > >> to
> >> > > >> >> set trait values, and when the trait is a trait of the main
> >> class,
> >> > it
> >> > > >> >> works; however, when it is a trait of the subclass, it
> doesn't.
> >> >  I've
> >> > > >> >> chosen this method because I think it's very amenable to
> >> > simulations;
> >> > > >> all
> >> > > >> >> the user does is specify the trait he/she wants as a string,
> >> rather
> >> > > >> than
> >> > > >> >> opening the source and referring to the trait variable
> >> explicitly.
> >> > > >>  Aka I
> >> > > >> >> realize I could make this work by saying "self.sub.a.set()"
> but
> >> I'd
> >> > > >> rather
> >> > > >> >> use this notation if possible.
> >> > > >> >>
> >> > > >> >> from traits.api import *
> >> > > >> >> from traitsui.api import *
> >> > > >> >> from random import random
> >> > > >> >>
> >> > > >> >> class SubClass(HasTraits):
> >> > > >> >>    a=Float(1.0)
> >> > > >> >>
> >> > > >> >> class Main(HasTraits):
> >> > > >> >>    b=Float(2.0)
> >> > > >> >>    sub=Instance(SubClass,())
> >> > > >> >>
> >> > > >> >>    set_random=Button
> >> > > >> >>
> >> > > >> >>    def _set_random_fired(self):
> >> > > >> >>        bnew={'b':random()}
> >> > > >> >>        self.set(**bnew)  #Works
> >> > > >> >>
> >> > > >> >>        anew={'sub.a':random()}
> >> > > >> >>        self.set(**anew) #Doesn't work
> >> > > >> >>
> >> > > >> >>    traits_view=View(
> >> > > >> >>                Item('b'), Item('sub', style='custom'),
> >> > > >> Item('set_random')
> >> > > >> >>            )
> >> > > >> >>
> >> > > >> >> Main().configure_traits()
> >> > > >> >>
> >> > > >> >> On Tue, Jan 31, 2012 at 8:33 PM, Adam Hughes <
> >> > > [hidden email]
> >> > > >> >> >wrote:
> >> > > >> >>
> >> > > >> >> > Jonathan,
> >> > > >> >> >
> >> > > >> >> > Thanks for getting back to me.  I'll get an example out
> >> tomorrow,
> >> > > >> sorry
> >> > > >> >> > for the delay.
> >> > > >> >> >
> >> > > >> >> >
> >> > > >> >> > On Tue, Jan 31, 2012 at 5:03 PM, Jonathan Rocher <
> >> > > >> [hidden email]
> >> > > >> >> >wrote:
> >> > > >> >> >
> >> > > >> >> >> Hi Adam,
> >> > > >> >> >>
> >> > > >> >> >> Do you have a sample code to make sure I understand what
> you
> >> are
> >> > > >> >> trying to
> >> > > >> >> >> do? Why could you not use getattr and setattr?
> >> > > >> >> >>
> >> > > >> >> >> Thanks,
> >> > > >> >> >> Jonathan
> >> > > >> >> >>
> >> > > >> >> >> On Mon, Jan 30, 2012 at 7:30 PM, Adam Hughes <
> >> > > >> [hidden email]
> >> > > >> >> >> >wrote:
> >> > > >> >> >>
> >> > > >> >> >> > Hi,
> >> > > >> >> >> >
> >> > > >> >> >> > I've been using some of the built-in features of
> HasTraits
> >> to
> >> > > run
> >> > > >> >> >> > simulations, mainly by using trait_get() and trait_set()
> >> > > methods.
> >> > > >> >>  I've
> >> > > >> >> >> > designed a structure that I think is fairly powerful for
> >> > > >> simulations,
> >> > > >> >> >> > because it allows a user to specify a set of ranges for
> >> valid
> >> > > >> float
> >> > > >> >> >> traits,
> >> > > >> >> >> > then the simulation will update all of these traits step
> by
> >> > > step,
> >> > > >> so
> >> > > >> >> >> it's
> >> > > >> >> >> > very easy to run simulations over many different types of
> >> > > >> variables
> >> > > >> >> in
> >> > > >> >> >> the
> >> > > >> >> >> > program using this same framework.  All the user has to
> >> > provide
> >> > > >> are
> >> > > >> >> the
> >> > > >> >> >> > correct trait names.
> >> > > >> >> >> >
> >> > > >> >> >> > For example, if I had traits A, B my simulation would
> >> update
> >> > > them
> >> > > >> >> both
> >> > > >> >> >> as
> >> > > >> >> >> > it runs along.  In practice, my objects are fairly
> >> complex, so
> >> > > the
> >> > > >> >> >> traits
> >> > > >> >> >> > that I really want to iterate over a buried deep in the
> >> class
> >> > > >> >> heirarchy.
> >> > > >> >> >> > In reality, I might want to run a simulation that varies
> A,
> >> > and
> >> > > >> the
> >> > > >> >> >> trait
> >> > > >> >> >> > B.C.D where D is deep in the class heirarchy, a
> >> sub-sub-class
> >> > of
> >> > > >> B.
> >> > > >> >> >> >
> >> > > >> >> >> > The trait_get, and trait_set work() fine when I provide
> >> class
> >> > > >> trait
> >> > > >> >> >> names
> >> > > >> >> >> > like 'A' and 'B', but doesn't work with the extended
> >> notation
> >> > as
> >> > > >> far
> >> > > >> >> as
> >> > > >> >> >> I
> >> > > >> >> >> > can tell (e.g. trait 'B.C.D').  Also I noticed that when
> I
> >> > list
> >> > > >> trait
> >> > > >> >> >> names
> >> > > >> >> >> > using the .trait_names() method, it also does not access
> >> > traits
> >> > > in
> >> > > >> >> the
> >> > > >> >> >> > subclasses.  Is there a known way to access subclass
> traits
> >> > from
> >> > > >> the
> >> > > >> >> >> basic
> >> > > >> >> >> > HasTraits functions?
> >> > > >> >> >> >
> >> > > >> >> >> > Thanks.
> >> > > >> >> >> > _______________________________________________
> >> > > >> >> >> > Enthought-Dev mailing list
> >> > > >> >> >> > [hidden email]
> >> > > >> >> >> >
> https://mail.enthought.com/mailman/listinfo/enthought-dev
> >> > > >> >> >> >
> >> > > >> >> >>
> >> > > >> >> >>
> >> > > >> >> >>
> >> > > >> >> >> --
> >> > > >> >> >> Jonathan Rocher, PhD
> >> > > >> >> >> Scientific software developer
> >> > > >> >> >> Enthought, Inc.
> >> > > >> >> >> [hidden email]
> >> > > >> >> >> 1-512-536-1057
> >> > > >> >> >> http://www.enthought.com
> >> > > >> >> >> _______________________________________________
> >> > > >> >> >> Enthought-Dev mailing list
> >> > > >> >> >> [hidden email]
> >> > > >> >> >> https://mail.enthought.com/mailman/listinfo/enthought-dev
> >> > > >> >> >>
> >> > > >> >> >
> >> > > >> >> >
> >> > > >> >> _______________________________________________
> >> > > >> >> Enthought-Dev mailing list
> >> > > >> >> [hidden email]
> >> > > >> >> https://mail.enthought.com/mailman/listinfo/enthought-dev
> >> > > >> >>
> >> > > >> >
> >> > > >> >
> >> > > >> >
> >> > > >> > --
> >> > > >> > Jonathan Rocher, PhD
> >> > > >> > Scientific software developer
> >> > > >> > Enthought, Inc.
> >> > > >> > [hidden email]
> >> > > >> > 1-512-536-1057
> >> > > >> > http://www.enthought.com
> >> > > >> >
> >> > > >> >
> >> > > >>
> >> > > >>
> >> > > >> --
> >> > > >> Jonathan Rocher, PhD
> >> > > >> Scientific software developer
> >> > > >> Enthought, Inc.
> >> > > >> [hidden email]
> >> > > >> 1-512-536-1057
> >> > > >> http://www.enthought.com
> >> > > >> _______________________________________________
> >> > > >> Enthought-Dev mailing list
> >> > > >> [hidden email]
> >> > > >> https://mail.enthought.com/mailman/listinfo/enthought-dev
> >> > > >>
> >> > > >
> >> > > >
> >> > > _______________________________________________
> >> > > Enthought-Dev mailing list
> >> > > [hidden email]
> >> > > https://mail.enthought.com/mailman/listinfo/enthought-dev
> >> > >
> >> >
> >> >
> >> >
> >> > --
> >> > Jonathan Rocher, PhD
> >> > Scientific software developer
> >> > Enthought, Inc.
> >> > [hidden email]
> >> > 1-512-536-1057
> >> > http://www.enthought.com
> >> > _______________________________________________
> >> > Enthought-Dev mailing list
> >> > [hidden email]
> >> > https://mail.enthought.com/mailman/listinfo/enthought-dev
> >> >
> >> _______________________________________________
> >> Enthought-Dev mailing list
> >> [hidden email]
> >> https://mail.enthought.com/mailman/listinfo/enthought-dev
> >>
> >
> >
> >
> > --
> > Simon Jagoe
> > Enthought Ltd
> > +44 79 312 11 506
> > [hidden email]
> >
>
>
>
> --
> Simon Jagoe
> Enthought Ltd
> +44 79 312 11 506
> [hidden email]
> _______________________________________________
> Enthought-Dev mailing list
> [hidden email]
> https://mail.enthought.com/mailman/listinfo/enthought-dev
>
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev