getting python objects from traits

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

getting python objects from traits

Satrajit Ghosh
hi,

is there a way to get at the underlying value of a trait as a python object instead of an object with trait bindings? as an example, i would like to get a dict when i get the value of foo and a list when i get the value of goo. it's fine for a.foo and a.goo to have trait bindings, but when i call get() that returns a dict it would be nice to see that as a python entity without trait bindings.

many thanks,

cheers,

satra


In [32]: class A(HasTraits):
    foo = Dict
    goo = List
    moo = Int
   ....:    
   ....:    

In [36]: a = A()

In [37]: a.foo = dict(hey=1)

In [38]: a.goo = [1,2,3,4]

In [39]: a.moo = 2

In [40]: type(a.foo)
Out[40]: <class 'enthought.traits.trait_handlers.TraitDictObject'>

In [41]: type(a.get()['foo'])
Out[41]: <class 'enthought.traits.trait_handlers.TraitDictObject'>

In [42]: type(a.goo)
Out[42]: <class 'enthought.traits.trait_handlers.TraitListObject'>

In [43]: type(a.get()['goo'])
Out[43]: <class 'enthought.traits.trait_handlers.TraitListObject'>

In [44]: type(a.get()['moo'])
Out[44]: <type 'int'>

In [45]: type(a.get())
Out[45]: <type 'dict'>


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

Re: getting python objects from traits

Peter Wang
On Tue, May 11, 2010 at 5:28 PM, Satrajit Ghosh <[hidden email]> wrote:
> hi,
>
> is there a way to get at the underlying value of a trait as a python object
> instead of an object with trait bindings? as an example, i would like to get
> a dict when i get the value of foo and a list when i get the value of goo.

This situation exists for container types like List and Dict because
that is the only way we can get notification about item changes.  You
can circumvent this by doing the following:

bare_list = a.goo[:]
bare_dict = dict(a.foo)

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

Re: getting python objects from traits

Satrajit Ghosh
thanks peter.

This situation exists for container types like List and Dict because
that is the only way we can get notification about item changes.  You
can circumvent this by doing the following:

bare_list = a.goo[:]
bare_dict = dict(a.foo)

this is great and it works, but i have a question regarding the behavior of get().

calling b = a.get() returns my traited class as a dict with no trait bindings. is there a reason why the notifications are retained? perhaps i'm missing the point of the get function.

cheers,

satra

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

Re: getting python objects from traits

Robert Kern

.trait_get() (the preferred alias of .get()) returns a dict mapping names of traits to their values, not a single value even if you only requested one.  .trait_get() is primarily for getting multiple traits at once, not a single trait.

On May 11, 2010 7:17 PM, "Satrajit Ghosh" <[hidden email]> wrote:

thanks peter.

> This situation exists for container types like List and Dict because
> that is the only way we can...


this is great and it works, but i have a question regarding the behavior of get().

calling b = a.get() returns my traited class as a dict with no trait bindings. is there a reason why the notifications are retained? perhaps i'm missing the point of the get function.

cheers,

satra

_______________________________________________
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