add_trait

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

add_trait

Brennan Williams
Quick questions... if I use add_trait to add a number of traits with
value to an instance of an object, how do I check those traits?
I've tried objectname.trait_get() and also objectname.get_trait(name).
I'll put a code snippet together asap.

Brennan

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

Re: add_trait

Robert Kern
On Tue, May 1, 2012 at 10:09 AM, Brennan Williams
<[hidden email]> wrote:
> Quick questions... if I use add_trait to add a number of traits with
> value to an instance of an object, how do I check those traits?
> I've tried objectname.trait_get() and also objectname.get_trait(name).
> I'll put a code snippet together asap.

I'm not sure what you mean by "check those traits". You mean that you
want to see the TraitType that was added for that trait?

[~]
|1> from traits.api import *

[~]
|2> class A(HasTraits):
..>     pass
..>

[~]
|3> a = A()

[~]
|4> a.add_trait('x', Int(10))

[~]
|5> ct = a.trait('x')

[~]
|6> ct
<traits.traits.CTrait at 0x6a2f608>

[~]
|7> %see ct
    ()                    hash()                help()                repr()
    str()                 .cast()               .clone()
    .comparison_mode()    .default              .default_kind
    .default_value()      .default_value_for()  .delegate()
    .full_info()          .get_editor()         .get_help()
    .get_validate()       .handler()            .info()
    .inner_traits         .is_mapped()          .is_trait_type()
    .post_setattr         .post_setattr_original_value()
    .property()           .rich_comparison()    .set_validate()
    .setattr_original_value()                   .trait_type()         .type
    .validate()           .value_allowed()      .value_property()

[~]
|9> ct.trait_type
<traits.trait_types.Int at 0x6a28f30>

[~]
|10> ct.trait_type.default_value
10

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

Re: add_trait

Brennan Williams
On 1/05/2012 10:45 p.m., Robert Kern wrote:
> On Tue, May 1, 2012 at 10:09 AM, Brennan Williams
> <[hidden email]>  wrote:
>> Quick questions... if I use add_trait to add a number of traits with
>> value to an instance of an object, how do I check those traits?
>> I've tried objectname.trait_get() and also objectname.get_trait(name).
>> I'll put a code snippet together asap.
> I'm not sure what you mean by "check those traits". You mean that you
> want to see the TraitType that was added for that trait?
basically what I want to do is to add traits with values to a list of
objects, then create a table editor and view the list of objects using
that table editor where columns in the table are the traits that I have
added.

>
> [~]
> |1>  from traits.api import *
>
> [~]
> |2>  class A(HasTraits):
> ..>      pass
> ..>
>
> [~]
> |3>  a = A()
>
> [~]
> |4>  a.add_trait('x', Int(10))
>
> [~]
> |5>  ct = a.trait('x')
>
> [~]
> |6>  ct
> <traits.traits.CTrait at 0x6a2f608>
>
> [~]
> |7>  %see ct
>      ()                    hash()                help()                repr()
>      str()                 .cast()               .clone()
>      .comparison_mode()    .default              .default_kind
>      .default_value()      .default_value_for()  .delegate()
>      .full_info()          .get_editor()         .get_help()
>      .get_validate()       .handler()            .info()
>      .inner_traits         .is_mapped()          .is_trait_type()
>      .post_setattr         .post_setattr_original_value()
>      .property()           .rich_comparison()    .set_validate()
>      .setattr_original_value()                   .trait_type()         .type
>      .validate()           .value_allowed()      .value_property()
>
> [~]
> |9>  ct.trait_type
> <traits.trait_types.Int at 0x6a28f30>
>
> [~]
> |10>  ct.trait_type.default_value
> 10
>

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

Re: add_trait

Robert Kern
On Tue, May 1, 2012 at 12:18 PM, Brennan Williams
<[hidden email]> wrote:

> On 1/05/2012 10:45 p.m., Robert Kern wrote:
>> On Tue, May 1, 2012 at 10:09 AM, Brennan Williams
>> <[hidden email]>  wrote:
>>> Quick questions... if I use add_trait to add a number of traits with
>>> value to an instance of an object, how do I check those traits?
>>> I've tried objectname.trait_get() and also objectname.get_trait(name).
>>> I'll put a code snippet together asap.
>> I'm not sure what you mean by "check those traits". You mean that you
>> want to see the TraitType that was added for that trait?
> basically what I want to do is to add traits with values to a list of
> objects, then create a table editor and view the list of objects using
> that table editor where columns in the table are the traits that I have
> added.

._instance_traits() will give you a dict with all of the traits that
were added to the instance using .add_trait().

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

Re: add_trait

Brennan Williams
On 1/05/2012 11:26 p.m., Robert Kern wrote:

> On Tue, May 1, 2012 at 12:18 PM, Brennan Williams
> <[hidden email]>  wrote:
>> On 1/05/2012 10:45 p.m., Robert Kern wrote:
>>> On Tue, May 1, 2012 at 10:09 AM, Brennan Williams
>>> <[hidden email]>    wrote:
>>>> Quick questions... if I use add_trait to add a number of traits with
>>>> value to an instance of an object, how do I check those traits?
>>>> I've tried objectname.trait_get() and also objectname.get_trait(name).
>>>> I'll put a code snippet together asap.
>>> I'm not sure what you mean by "check those traits". You mean that you
>>> want to see the TraitType that was added for that trait?
>> basically what I want to do is to add traits with values to a list of
>> objects, then create a table editor and view the list of objects using
>> that table editor where columns in the table are the traits that I have
>> added.
> ._instance_traits() will give you a dict with all of the traits that
> were added to the instance using .add_trait().
>
I've successfully used ._instance_traits together with .add_trait() and
I now have a create_table_editor() method that builds a table where the
columns are CheckBoxColumns, one for each of the newly added traits.
This display correctly.

Now I need to work out how to set up _changed listeners for those newly
added traits. I usually use explicit trait names, e.g. 'linear' or
'quadratic' so I would have something like....

def _linear_changed(self,old,new):
     self.linear=new
     self.parent.updateEquation()

As an aside (not so important) I'd also like to work out how to put
these newly added traits into a View

Any help much appreciated.

Brennan

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

Re: add_trait

Brennan Williams


On 1/05/2012 11:26 p.m., Robert Kern wrote:

>  On Tue, May 1, 2012 at 12:18 PM, Brennan Williams
>  <[hidden email]>   wrote:
>>  On 1/05/2012 10:45 p.m., Robert Kern wrote:
>>>  On Tue, May 1, 2012 at 10:09 AM, Brennan Williams
>>>  <[hidden email]>     wrote:
>>>>  Quick questions... if I use add_trait to add a number of traits with
>>>>  value to an instance of an object, how do I check those traits?
>>>>  I've tried objectname.trait_get() and also objectname.get_trait(name).
>>>>  I'll put a code snippet together asap.
>>>  I'm not sure what you mean by "check those traits". You mean that you
>>>  want to see the TraitType that was added for that trait?
>>  basically what I want to do is to add traits with values to a list of
>>  objects, then create a table editor and view the list of objects using
>>  that table editor where columns in the table are the traits that I have
>>  added.
>  ._instance_traits() will give you a dict with all of the traits that
>  were added to the instance using .add_trait().
>
I've successfully used ._instance_traits together with .add_trait() and
I now have a create_table_editor() method that builds a table where the
columns are CheckBoxColumns, one for each of the newly added traits.
This display correctly.

Now I need to work out how to set up _changed listeners for those newly
added traits. I usually use explicit trait names, e.g. 'linear' or
'quadratic' so I would have something like....

def _linear_changed(self,old,new):
     self.linear=new
     self.parent.updateEquation()

As an aside (not so important) I'd also like to work out how to put
these newly added traits into a View

Any help much appreciated.

Brennan


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

Re: add_trait

Robert Kern
In reply to this post by Brennan Williams
On Wed, May 2, 2012 at 12:17 AM, Brennan Williams
<[hidden email]> wrote:

> I've successfully used ._instance_traits together with .add_trait() and
> I now have a create_table_editor() method that builds a table where the
> columns are CheckBoxColumns, one for each of the newly added traits.
> This display correctly.
>
> Now I need to work out how to set up _changed listeners for those newly
> added traits. I usually use explicit trait names, e.g. 'linear' or
> 'quadratic' so I would have something like....
>
> def _linear_changed(self,old,new):
>     self.linear=new
>     self.parent.updateEquation()

Use dynamic notifiers:

  http://github.enthought.com/traits/traits_user_manual/notification.html#dynamic-notification

> As an aside (not so important) I'd also like to work out how to put
> these newly added traits into a View

Define a default_traits_view() method that returns a View. This will
be called at run-time, not class definition time, so it will have
access to the instance traits.

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

Re: add_trait

Brennan Williams
On 3/05/2012 12:47 a.m., Robert Kern wrote:

> On Wed, May 2, 2012 at 12:17 AM, Brennan Williams
> <[hidden email]>  wrote:
>> I've successfully used ._instance_traits together with .add_trait() and
>> I now have a create_table_editor() method that builds a table where the
>> columns are CheckBoxColumns, one for each of the newly added traits.
>> This display correctly.
>>
>> Now I need to work out how to set up _changed listeners for those newly
>> added traits. I usually use explicit trait names, e.g. 'linear' or
>> 'quadratic' so I would have something like....
>>
>> def _linear_changed(self,old,new):
>>      self.linear=new
>>      self.parent.updateEquation()
> Use dynamic notifiers:
>
>    http://github.enthought.com/traits/traits_user_manual/notification.html#dynamic-notification
Great. Got on_trait_change working without any problems.
>> As an aside (not so important) I'd also like to work out how to put
>> these newly added traits into a View
> Define a default_traits_view() method that returns a View. This will
> be called at run-time, not class definition time, so it will have
> access to the instance traits.
>
I tend to use something like...

self._my_view=self.create_my_view()
self.edit_traits(view=self.my_view)

and....

def create_my_view(self):
      view=View(VGroup(Item('name1'),....),.....))
      return view

where usually my trait names 'name1','name2' etc are the explit names
defined in the class definition, e.g.

class Thing(HasTraits):
plot_it=Bool(True)
.....

but I now have a bunch of traits defined using add_trait

so how do I use

tract_dict=self._instance_traits()

which returns me a dict of trait names and trait objects

to add these traits to my view?

I don't know what these trait names will be nor how many of them are
there (they are user defined).

Brennan

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

Re: add_trait

Robert Kern
On Wed, May 2, 2012 at 8:02 PM, Brennan Williams
<[hidden email]> wrote:

> On 3/05/2012 12:47 a.m., Robert Kern wrote:
>> On Wed, May 2, 2012 at 12:17 AM, Brennan Williams
>> <[hidden email]>  wrote:
>>> I've successfully used ._instance_traits together with .add_trait() and
>>> I now have a create_table_editor() method that builds a table where the
>>> columns are CheckBoxColumns, one for each of the newly added traits.
>>> This display correctly.
>>>
>>> Now I need to work out how to set up _changed listeners for those newly
>>> added traits. I usually use explicit trait names, e.g. 'linear' or
>>> 'quadratic' so I would have something like....
>>>
>>> def _linear_changed(self,old,new):
>>>      self.linear=new
>>>      self.parent.updateEquation()
>> Use dynamic notifiers:
>>
>>    http://github.enthought.com/traits/traits_user_manual/notification.html#dynamic-notification
> Great. Got on_trait_change working without any problems.
>>> As an aside (not so important) I'd also like to work out how to put
>>> these newly added traits into a View
>> Define a default_traits_view() method that returns a View. This will
>> be called at run-time, not class definition time, so it will have
>> access to the instance traits.
>>
> I tend to use something like...
>
> self._my_view=self.create_my_view()
> self.edit_traits(view=self.my_view)
>
> and....
>
> def create_my_view(self):
>      view=View(VGroup(Item('name1'),....),.....))
>      return view
>
> where usually my trait names 'name1','name2' etc are the explit names
> defined in the class definition, e.g.
>
> class Thing(HasTraits):
> plot_it=Bool(True)
> .....
>
> but I now have a bunch of traits defined using add_trait
>
> so how do I use
>
> tract_dict=self._instance_traits()
>
> which returns me a dict of trait names and trait objects
>
> to add these traits to my view?
>
> I don't know what these trait names will be nor how many of them are
> there (they are user defined).

In your method, just make a list of Items and do View(*items). A
simple example, just using the default editors for the traits in
alphabetical order:

def default_traits_view(self):
    trait_dict = self._instance_traits()
    items = [Item(name) for name in sorted(trait_dict)]
    return View(*items)

--
Robert Kern
Enthought
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev