Get traits of class without super-class traits?

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

Get traits of class without super-class traits?

Des.P
class X(HasTraits):
        # some traits

class Y(X):
        # some traits

Y.class_traits() includes traits inherited from X. How do I get just the traits defined locally in Y?

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

Re: Get traits of class without super-class traits?

Chris Colbert
Someone may jump in and correct me. But I don't believe that that is natively possible because of how Traits handles trait inheritance.

You could get that info by writing your metaclass or doing a diff between the class traits of your class and the parent class.

On Mon, Oct 15, 2012 at 3:42 PM, Des.P <[hidden email]> wrote:
class X(HasTraits):
        # some traits

class Y(X):
        # some traits

Y.class_traits() includes traits inherited from X. How do I get just the traits defined locally in Y?

Thanks!
_______________________________________________
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: Get traits of class without super-class traits?

Robert Kern
On Mon, Oct 15, 2012 at 9:04 PM, Chris Colbert <[hidden email]> wrote:
> Someone may jump in and correct me. But I don't believe that that is
> natively possible because of how Traits handles trait inheritance.
>
> You could get that info by writing your metaclass or doing a diff between
> the class traits of your class and the parent class.

That's what I do:

https://bitbucket.org/robertkern/kernmagic/src/a5a52b5f365abbbc2f9d5bd6c8a228cddb841b0d/kernmagic/mymagics.py?at=default#cl-377

> On Mon, Oct 15, 2012 at 3:42 PM, Des.P <[hidden email]> wrote:
>>
>> class X(HasTraits):
>>         # some traits
>>
>> class Y(X):
>>         # some traits
>>
>> Y.class_traits() includes traits inherited from X. How do I get just the
>> traits defined locally in Y?
>>
>> Thanks!
>> _______________________________________________
>> 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
>



--
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: Get traits of class without super-class traits?

Des.P
Thanks, Robert.

Does your approach correctly catch the local trait definition which specializes a base trait definition, such as the trait for 'b' in class A2 below?

class B1: pass
class B2(B1): pass

class A1:
   b = Instance(B1)
   c = Int
class A2(A1):
   b = Instance(B2)

Does traits keep around, anywhere, information about the local definition, since everything else is derived from that?

If not, can I rely on a inherited & non-overriden trait to have the same CTrait identity as the corresponding base? i.e.

A1.class_traits()['c'] is A2.class_traits()['c']

Thanks!


On Oct 15, 2012, at 4:03 PM, Robert Kern wrote:

> On Mon, Oct 15, 2012 at 9:04 PM, Chris Colbert <[hidden email]> wrote:
>> Someone may jump in and correct me. But I don't believe that that is
>> natively possible because of how Traits handles trait inheritance.
>>
>> You could get that info by writing your metaclass or doing a diff between
>> the class traits of your class and the parent class.
>
> That's what I do:
>
> https://bitbucket.org/robertkern/kernmagic/src/a5a52b5f365abbbc2f9d5bd6c8a228cddb841b0d/kernmagic/mymagics.py?at=default#cl-377
>
>> On Mon, Oct 15, 2012 at 3:42 PM, Des.P <[hidden email]> wrote:
>>>
>>> class X(HasTraits):
>>>        # some traits
>>>
>>> class Y(X):
>>>        # some traits
>>>
>>> Y.class_traits() includes traits inherited from X. How do I get just the
>>> traits defined locally in Y?
>>>
>>> Thanks!
>>> _______________________________________________
>>> 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
>>
>
>
>
> --
> Robert Kern
> Enthought
> _______________________________________________
> 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: Get traits of class without super-class traits?

Pietro Berkes


On Tue, Oct 16, 2012 at 12:05 AM, Des.P <[hidden email]> wrote:
Thanks, Robert.

Does your approach correctly catch the local trait definition which specializes a base trait definition, such as the trait for 'b' in class A2 below?

class B1: pass
class B2(B1): pass

class A1:
   b = Instance(B1)
   c = Int
class A2(A1):
   b = Instance(B2)

Does traits keep around, anywhere, information about the local definition, since everything else is derived from that?

If not, can I rely on a inherited & non-overriden trait to have the same CTrait identity as the corresponding base? i.e.

A1.class_traits()['c'] is A2.class_traits()['c']

 

I'm not sure if you can rely on the identity, but in case you can, this code would do what you need:

a1_traits = set(A1.class_traits().items())
a2_and_a1_traits = set(A2.class_traits().items())

a1_traits = a2_and_a1_traits.difference(a1_traits)

Best,
Pietro



On Oct 15, 2012, at 4:03 PM, Robert Kern wrote:

> On Mon, Oct 15, 2012 at 9:04 PM, Chris Colbert <[hidden email]> wrote:
>> Someone may jump in and correct me. But I don't believe that that is
>> natively possible because of how Traits handles trait inheritance.
>>
>> You could get that info by writing your metaclass or doing a diff between
>> the class traits of your class and the parent class.
>
> That's what I do:
>
> https://bitbucket.org/robertkern/kernmagic/src/a5a52b5f365abbbc2f9d5bd6c8a228cddb841b0d/kernmagic/mymagics.py?at=default#cl-377
>
>> On Mon, Oct 15, 2012 at 3:42 PM, Des.P <[hidden email]> wrote:
>>>
>>> class X(HasTraits):
>>>        # some traits
>>>
>>> class Y(X):
>>>        # some traits
>>>
>>> Y.class_traits() includes traits inherited from X. How do I get just the
>>> traits defined locally in Y?
>>>
>>> Thanks!
>>> _______________________________________________
>>> 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
>>
>
>
>
> --
> Robert Kern
> Enthought
> _______________________________________________
> 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



--
Pietro Berkes
Scientific software developer
Enthought UK



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

Re: Get traits of class without super-class traits?

Robert Kern
In reply to this post by Des.P
On Tue, Oct 16, 2012 at 12:05 AM, Des.P <[hidden email]> wrote:
> Thanks, Robert.
>
> Does your approach correctly catch the local trait definition which specializes a base trait definition, such as the trait for 'b' in class A2 below?

My function doesn't bother with the trait type itself, just the name
and the value, and it works from root class down to leaf class and
assigns the trait to the first class it appears in.

> class B1: pass
> class B2(B1): pass
>
> class A1:
>    b = Instance(B1)
>    c = Int
> class A2(A1):
>    b = Instance(B2)
>
> Does traits keep around, anywhere, information about the local definition, since everything else is derived from that?

I'm not sure what information you are talking about. Do you mean that
you want to see whether a CTrait holds an Int() or an Instance(B1) or
an Instance(B2)?

[~]
|27> A2.class_traits()['c'].trait_type
<traits.trait_types.Int at 0x5779c90>

[~]
|28> A2.class_traits()['b'].trait_type
<traits.trait_types.Instance at 0x5779cb0>

[~]
|29> A2.class_traits()['b'].trait_type.klass
__main__.B2

> If not, can I rely on a inherited & non-overriden trait to have the same CTrait identity as the corresponding base? i.e.
>
> A1.class_traits()['c'] is A2.class_traits()['c']

It seems to be true, but I'm not completely sure that it is guaranteed.

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