Help with Traits Enum types

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

Help with Traits Enum types

Rick Muller
Thanks for Traits; I'm just learning it, but have been having a lot of fun thus far.

One thing that I haven't figured out how to do is to set the default value of an Enum type, when I want it to be something other than the first value.

choice = Enum(1,2,3,default=2)

or something. How can I do this?

Thanks,

Rick


--
Rick Muller
[hidden email]
505-750-7557


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

Re: Help with Traits Enum types

Robert Kern
On Tue, Feb 9, 2010 at 11:53, Rick Muller <[hidden email]> wrote:
> Thanks for Traits; I'm just learning it, but have been having a lot of fun
> thus far.
>
> One thing that I haven't figured out how to do is to set the default value
> of an Enum type, when I want it to be something other than the first value.
>
> choice = Enum(1,2,3,default=2)
>
> or something. How can I do this?

We should probably fix Enum's constructor to allow this, but a simple
subclass will do the trick, too:

In [8]: class DefaultEnum(Enum):
   ...:     def __init__(self, *args, **kwds):
   ...:         super(DefaultEnum, self).__init__(*args, **kwds)
   ...:         if 'default' in kwds:
   ...:             self.default_value = kwds['default']
   ...:
   ...:

In [9]: class A(HasTraits):
   ...:     x = DefaultEnum(1,2,3, default=2)
   ...:
   ...:

In [10]: a = A()

In [11]: a.x
Out[11]: 2

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Help with Traits Enum types

Warren Weckesser
Robert Kern wrote:

> On Tue, Feb 9, 2010 at 11:53, Rick Muller <[hidden email]> wrote:
>  
>> Thanks for Traits; I'm just learning it, but have been having a lot of fun
>> thus far.
>>
>> One thing that I haven't figured out how to do is to set the default value
>> of an Enum type, when I want it to be something other than the first value.
>>
>> choice = Enum(1,2,3,default=2)
>>
>> or something. How can I do this?
>>    
>
>  
> We should probably fix Enum's constructor to allow this, but a simple
> subclass will do the trick, too:
>
> In [8]: class DefaultEnum(Enum):
>    ...:     def __init__(self, *args, **kwds):
>    ...:         super(DefaultEnum, self).__init__(*args, **kwds)
>    ...:         if 'default' in kwds:
>    ...:             self.default_value = kwds['default']
>    ...:
>    ...:
>
> In [9]: class A(HasTraits):
>    ...:     x = DefaultEnum(1,2,3, default=2)
>    ...:
>    ...:
>
> In [10]: a = A()
>
> In [11]: a.x
> Out[11]: 2
>
>  


Nice tip--nice enough that I added it to the "Traits Cookbook" in the
Traits wiki:

https://svn.enthought.com/enthought/wiki/Traits


Warren

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

Re: Help with Traits Enum types

Rick Muller
In reply to this post by Robert Kern
Awesome!

Now that I have you on the line, how about a slightly harder question. I'd like for my Enum to return ints, say, but display text strings. I tried this and it didn't work:

class Test(HasTraits):
    battery = Enum(
        CInt(1,label="Li ion"),
        CInt(2,label="Zn air"),
        CInt(3,label="NiMH"))

It doesn't really have to be a CInt, I just want some way of displaying something other than the integer values, like a string.

On Tue, Feb 9, 2010 at 11:01 AM, Robert Kern <[hidden email]> wrote:
On Tue, Feb 9, 2010 at 11:53, Rick Muller <[hidden email]> wrote:
> Thanks for Traits; I'm just learning it, but have been having a lot of fun
> thus far.
>
> One thing that I haven't figured out how to do is to set the default value
> of an Enum type, when I want it to be something other than the first value.
>
> choice = Enum(1,2,3,default=2)
>
> or something. How can I do this?

We should probably fix Enum's constructor to allow this, but a simple
subclass will do the trick, too:

In [8]: class DefaultEnum(Enum):
  ...:     def __init__(self, *args, **kwds):
  ...:         super(DefaultEnum, self).__init__(*args, **kwds)
  ...:         if 'default' in kwds:
  ...:             self.default_value = kwds['default']
  ...:
  ...:

In [9]: class A(HasTraits):
  ...:     x = DefaultEnum(1,2,3, default=2)
  ...:
  ...:

In [10]: a = A()

In [11]: a.x
Out[11]: 2

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
 -- Umberto Eco
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev



--
Rick Muller
[hidden email]
505-750-7557


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

Re: Help with Traits Enum types

Robert Kern
On Tue, Feb 9, 2010 at 13:04, Rick Muller <[hidden email]> wrote:
> Awesome!

Also,

  battery = Enum(2, 1, 2, 3)

> Now that I have you on the line, how about a slightly harder question. I'd
> like for my Enum to return ints, say, but display text strings. I tried this
> and it didn't work:
>
> class Test(HasTraits):
>     battery = Enum(
>         CInt(1,label="Li ion"),
>         CInt(2,label="Zn air"),
>         CInt(3,label="NiMH"))
>
> It doesn't really have to be a CInt, I just want some way of displaying
> something other than the integer values, like a string.

I recommend specifying this information in the EnumEditor itself. See
the EnumEditor section in the Traits UI User's Guide.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev
Reply | Threaded
Open this post in threaded view
|

Re: Help with Traits Enum types

Rick Muller


On Tue, Feb 9, 2010 at 12:10 PM, Robert Kern <[hidden email]> wrote:
On Tue, Feb 9, 2010 at 13:04, Rick Muller <[hidden email]> wrote:
> Awesome!

Also,

 battery = Enum(2, 1, 2, 3)


I tried this, but this just makes a longer menu, with "2" inserted twice. I like your earlier suggestion more, where only the numbers 1,2,3 are in the choices, and 2 is selected.

> Now that I have you on the line, how about a slightly harder question. I'd
> like for my Enum to return ints, say, but display text strings. I tried this
> and it didn't work:
>
> class Test(HasTraits):
>     battery = Enum(
>         CInt(1,label="Li ion"),
>         CInt(2,label="Zn air"),
>         CInt(3,label="NiMH"))
>
> It doesn't really have to be a CInt, I just want some way of displaying
> something other than the integer values, like a string.

I recommend specifying this information in the EnumEditor itself. See
the EnumEditor section in the Traits UI User's Guide.


Thanks, I'll take a look.

--
Rick Muller
[hidden email]
505-750-7557


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