Usage of Property trait ?

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

Usage of Property trait ?

Des.P
In looking at the docs for Property(fget,fset,fvalidate,force,handler,trait, **metadata), I am not sure how to use the available parameters, or what their types (e.g. signatures of fset, fvalidate, ... ) are.

If I have:

class Car(HasTraits):
        value = Int

class Person(HasTraits):
        cars = List(Instance(Car))
        cheapest_car = Property(...??...)

I would like cheapest_car to be treated as a derived property (with some getter, possibly a setter), but whose type must be a Car.

I would also like to reflect on:
        Person.class_traits()
and determine that cheapest_car is a Property, it's type is a Car, and possibly what other attributes it is derived from.

So my questions, in summary:
How do I indicate what the type of a Property trait is?
How / where can I determine from reflecting on the class that this
   (a) is a Property
   (b) what it's type is
   (c) what it depends on
When is this declared type checked? On calling the setter? On every return value from the getter?

Thanks!

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

Re: Usage of Property trait ?

Adam Hughes


On Sun, Jan 20, 2013 at 2:33 AM, Des.P <[hidden email]> wrote:
In looking at the docs for Property(fget,fset,fvalidate,force,handler,trait, **metadata), I am not sure how to use the available parameters, or what their types (e.g. signatures of fset, fvalidate, ... ) are.

If I have:

class Car(HasTraits):
        value = Int

class Person(HasTraits):
        cars = List(Instance(Car))
        cheapest_car = Property(...??...)

I would like cheapest_car to be treated as a derived property (with some getter, possibly a setter), but whose type must be a Car.

I would also like to reflect on:
        Person.class_traits()
and determine that cheapest_car is a Property, it's type is a Car, and possibly what other attributes it is derived from.

So my questions, in summary:
How do I indicate what the type of a Property trait is?

You can do this in your code as something like:

class Person(HasTraits):
        cars = List(Instance(Car))
        cheapest_car = Property(Instance(Car), depends_on='cars, money, x,y,z')  (This is where you set the type)

def _get_cheapest_car(self):
    return cars+money+x+y+z

def _set_cheapest_car(self, x,y,z,cars,money)
   ... do some stuff...
   self.cheapest_car=Car()

Someone else will have to chime in on the questions about reflecting I'm afraid.

How / where can I determine from reflecting on the class that this
   (a) is a Property
   (b) what it's type is
   (c) what it depends on
When is this declared type checked? On calling the setter? On every return value from the getter?

Thanks!

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



--
Adam Hughes
Physics Ph.D Candidate
George Washington University

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

Re: Usage of Property trait ?

Didrik Pinte-2
On 20 January 2013 20:13, Adam Hughes <[hidden email]> wrote:

>
>
> On Sun, Jan 20, 2013 at 2:33 AM, Des.P <[hidden email]> wrote:
>>
>> In looking at the docs for
>> Property(fget,fset,fvalidate,force,handler,trait, **metadata), I am not sure
>> how to use the available parameters, or what their types (e.g. signatures of
>> fset, fvalidate, ... ) are.
>>
>> If I have:
>>
>> class Car(HasTraits):
>>         value = Int
>>
>> class Person(HasTraits):
>>         cars = List(Instance(Car))
>>         cheapest_car = Property(...??...)
>>
>> I would like cheapest_car to be treated as a derived property (with some
>> getter, possibly a setter), but whose type must be a Car.
>>
>> I would also like to reflect on:
>>         Person.class_traits()
>> and determine that cheapest_car is a Property, it's type is a Car, and
>> possibly what other attributes it is derived from.
>>
>> So my questions, in summary:
>> How do I indicate what the type of a Property trait is?
>
>
> You can do this in your code as something like:
>
> class Person(HasTraits):
>         cars = List(Instance(Car))
>         cheapest_car = Property(Instance(Car), depends_on='cars, money,
> x,y,z')  (This is where you set the type)
>
> def _get_cheapest_car(self):
>     return cars+money+x+y+z
>
> def _set_cheapest_car(self, x,y,z,cars,money)
>    ... do some stuff...
>    self.cheapest_car=Car()
>
> Someone else will have to chime in on the questions about reflecting I'm
> afraid.
>
>> How / where can I determine from reflecting on the class that this
>>    (a) is a Property
>>    (b) what it's type is
>>    (c) what it depends on
>> When is this declared type checked? On calling the setter? On every return
>> value from the getter?

You can get the information from the trait definition. See below :

from traits.api import HasTraits, Str, Int, Property

class Person(HasTraits):
    name = Str
    name_len = Property(Int, depends_on='name')

me = Person(name='John')
name_len_trait = me.traits()['name_len']


print name_len_trait.depends_on # shows the dependency list
print name_len_trait.type # shows the trait type
print name_len_trait.trait_type # shows the target type


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