trait views

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

trait views

Ross Harder-2
Hi,

I'd like to have a traited class define several different views of it's traits but also keep the default view of all of it's traits intact without explicitly defining that view (the real class I am trying to do this with has many traits).   Is there a simple way to keep the default, show everything, view around and also define custom views that can be incorporated into the view of a class that contains an instance of the class with tons of traits?

 Below is a simplification of what I have been trying.  

from traits.api import HasTraits, Float, Instance, Int
from traitsui.api import View, Item, InstanceEditor


class Params(HasTraits):
  thing1=Float(1.0)
  thing2=Float(2.0)
  thing3=Int(3)

  l=[Item('thing2')]
  view1=View(l)

  def default_traits_view(self):
    alltraits=[]
    for k,v in self.__dict__.iteritems():
      print k, type(self.__dict__[k]) , type(self.__dict__[k]).__name__=='int'
      alltraits.append(Item(str(k)))
    self.view_all=View(alltraits)
    return 'view_all'

  def get_1thingview(self):
    view_1thing=View(Item('thing2'))
    return view_1thing

class Component(HasTraits):

  s=Float(2.3)
  p=Instance(Params)

  view=View( Item('s'), Item(name='p', style='custom') )
#  view=View( Item('s'), Item(name='p', style='custom') )
#  view=View( Item('s'), Item(name='p', style='custom', editor=InstanceEditor(view='view1')) )

  def __init__(self):
    self.p=Params()


if __name__=='__main__':
  c=Component()
  print c.p.default_traits_view()
  c.configure_traits()


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

Re: trait views

Adam Hughes
Is this along the lines of what you're trying to do:

http://code.enthought.com/projects/traits/docs/html/TUIUG/advanced_view.html

On Thu, May 17, 2012 at 6:41 PM, Ross Harder <[hidden email]> wrote:
Hi,

I'd like to have a traited class define several different views of it's traits but also keep the default view of all of it's traits intact without explicitly defining that view (the real class I am trying to do this with has many traits).   Is there a simple way to keep the default, show everything, view around and also define custom views that can be incorporated into the view of a class that contains an instance of the class with tons of traits?

 Below is a simplification of what I have been trying.  

from traits.api import HasTraits, Float, Instance, Int
from traitsui.api import View, Item, InstanceEditor


class Params(HasTraits):
  thing1=Float(1.0)
  thing2=Float(2.0)
  thing3=Int(3)

  l=[Item('thing2')]
  view1=View(l)

  def default_traits_view(self):
    alltraits=[]
    for k,v in self.__dict__.iteritems():
      print k, type(self.__dict__[k]) , type(self.__dict__[k]).__name__=='int'
      alltraits.append(Item(str(k)))
    self.view_all=View(alltraits)
    return 'view_all'

  def get_1thingview(self):
    view_1thing=View(Item('thing2'))
    return view_1thing

class Component(HasTraits):

  s=Float(2.3)
  p=Instance(Params)

  view=View( Item('s'), Item(name='p', style='custom') )
#  view=View( Item('s'), Item(name='p', style='custom') )
#  view=View( Item('s'), Item(name='p', style='custom', editor=InstanceEditor(view='view1')) )

  def __init__(self):
    self.p=Params()


if __name__=='__main__':
  c=Component()
  print c.p.default_traits_view()
  c.configure_traits()


_______________________________________________
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: trait views

Ross Harder-2
Opps, actually found this in the archive
https://mail.enthought.com/pipermail/enthought-dev/2008-March/013446.html

Should have dug for a few more minutes before emailing, thought the hour I spent was enough to justify bugging everyone....

Ross



From: Adam Hughes <[hidden email]>
To: Ross Harder <[hidden email]>; [hidden email]
Sent: Thursday, May 17, 2012 5:43 PM
Subject: Re: [Enthought-Dev] trait views

Is this along the lines of what you're trying to do:

http://code.enthought.com/projects/traits/docs/html/TUIUG/advanced_view.html

On Thu, May 17, 2012 at 6:41 PM, Ross Harder <[hidden email]> wrote:
Hi,

I'd like to have a traited class define several different views of it's traits but also keep the default view of all of it's traits intact without explicitly defining that view (the real class I am trying to do this with has many traits).   Is there a simple way to keep the default, show everything, view around and also define custom views that can be incorporated into the view of a class that contains an instance of the class with tons of traits?

 Below is a simplification of what I have been trying.  

from traits.api import HasTraits, Float, Instance, Int
from traitsui.api import View, Item, InstanceEditor


class Params(HasTraits):
  thing1=Float(1.0)
  thing2=Float(2.0)
  thing3=Int(3)

  l=[Item('thing2')]
  view1=View(l)

  def default_traits_view(self):
    alltraits=[]
    for k,v in self.__dict__.iteritems():
      print k, type(self.__dict__[k]) , type(self.__dict__[k]).__name__=='int'
      alltraits.append(Item(str(k)))
    self.view_all=View(alltraits)
    return 'view_all'

  def get_1thingview(self):
    view_1thing=View(Item('thing2'))
    return view_1thing

class Component(HasTraits):

  s=Float(2.3)
  p=Instance(Params)

  view=View( Item('s'), Item(name='p', style='custom') )
#  view=View( Item('s'), Item(name='p', style='custom') )
#  view=View( Item('s'), Item(name='p', style='custom', editor=InstanceEditor(view='view1')) )

  def __init__(self):
    self.p=Params()


if __name__=='__main__':
  c=Component()
  print c.p.default_traits_view()
  c.configure_traits()


_______________________________________________
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
|

Exception log

Ross Harder-2
In reply to this post by Adam Hughes
I get this error:

Exception occurred in traits notification handler.
Please check the log file for details.

Where is the log file or where in the documentation does it tell me?

Thanks,
Ross


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

Re: Exception log

Jonathan March
Hi Ross,

What application are you running, in what environment? (OS version, EPD version, etc)

Thanks,
Jonathan

On Sun, Jun 3, 2012 at 11:31 AM, Ross Harder <[hidden email]> wrote:
I get this error:

Exception occurred in traits notification handler.
Please check the log file for details.

Where is the log file or where in the documentation does it tell me?

Thanks,
Ross


_______________________________________________
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: Exception log

Ross Harder-2

Ahh, MacOs 10.5 EPD 7.2 (i think)

I am trying to create my own HasTraits class and just running it with python prog.py

Ross


From: Jonathan March <[hidden email]>
To: Ross Harder <[hidden email]>; [hidden email]
Sent: Sunday, June 3, 2012 12:23 PM
Subject: Re: [Enthought-Dev] Exception log

Hi Ross,

What application are you running, in what environment? (OS version, EPD version, etc)

Thanks,
Jonathan

On Sun, Jun 3, 2012 at 11:31 AM, Ross Harder <[hidden email]> wrote:
I get this error:

Exception occurred in traits notification handler.
Please check the log file for details.

Where is the log file or where in the documentation does it tell me?

Thanks,
Ross


_______________________________________________
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
|

checking for a trait

Ross Harder-2
In reply to this post by Jonathan March
All,

How should I check for a trait within a class?  Or is this just a flawed idea?
I've tried:

from traits.api import HasTraits, Int
class traitedclass(HasTraits):

  i1=Int  
  def __init__(self):
    i1=3
    self.add_trait('i2', Int(5))

tc=traitedclass()
print tc.__dict__.has_key('i1')
print tc.__dict__.has_key('i2')
print tc.i1
print tc.i2
print tc.__dict__.has_key('i1')
print tc.__dict__.has_key('i2')

And get this weird output, notice that printing the value changes it's presence in the instances dict.

MacBook-2:Python rharder$ python dict.py 
False
False
0
5
True
True







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

Re: checking for a trait

Adam Hughes
Any HasTrait subclass has several methods that should let you do this.  Check out the methods in the api.  http://code.enthought.com/projects/files/ets_api/enthought.traits.has_traits.HasTraits.html

For example, if you used the print_traits(), trait_names(), or traits() methods, I think you could access the information you need.

On Sun, Jun 3, 2012 at 3:48 PM, Ross Harder <[hidden email]> wrote:
All,

How should I check for a trait within a class?  Or is this just a flawed idea?
I've tried:

from traits.api import HasTraits, Int
class traitedclass(HasTraits):

  i1=Int  
  def __init__(self):
    i1=3
    self.add_trait('i2', Int(5))

tc=traitedclass()
print tc.__dict__.has_key('i1')
print tc.__dict__.has_key('i2')
print tc.i1
print tc.i2
print tc.__dict__.has_key('i1')
print tc.__dict__.has_key('i2')

And get this weird output, notice that printing the value changes it's presence in the instances dict.

MacBook-2:Python rharder$ python dict.py 
False
False
0
5
True
True







_______________________________________________
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: checking for a trait

Ross Harder-2

Hi,
Thanks for your help.

I'm adding traits at runtime using add_trait.  This seems to make second class traits since they are not seen using any of these methods, unless you print them of course, which is weird.

Most of the methods of HasTraits operated by default only on the trait attributes explicitly defined in the class definition. They do not operate on trait attributes defined by way of wildcards or by calling add_trait().

Ross


From: Adam Hughes <[hidden email]>
To: Ross Harder <[hidden email]>; [hidden email]
Sent: Sunday, June 3, 2012 3:15 PM
Subject: Re: [Enthought-Dev] checking for a trait

Any HasTrait subclass has several methods that should let you do this.  Check out the methods in the api.  http://code.enthought.com/projects/files/ets_api/enthought.traits.has_traits.HasTraits.html

For example, if you used the print_traits(), trait_names(), or traits() methods, I think you could access the information you need.

On Sun, Jun 3, 2012 at 3:48 PM, Ross Harder <[hidden email]> wrote:
All,

How should I check for a trait within a class?  Or is this just a flawed idea?
I've tried:

from traits.api import HasTraits, Int
class traitedclass(HasTraits):

  i1=Int  
  def __init__(self):
    i1=3
    self.add_trait('i2', Int(5))

tc=traitedclass()
print tc.__dict__.has_key('i1')
print tc.__dict__.has_key('i2')
print tc.i1
print tc.i2
print tc.__dict__.has_key('i1')
print tc.__dict__.has_key('i2')

And get this weird output, notice that printing the value changes it's presence in the instances dict.

MacBook-2:Python rharder$ python dict.py 
False
False
0
5
True
True







_______________________________________________
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: checking for a trait

Jonathan March
In reply to this post by Adam Hughes
Ross,

Yours is not at all a flawed idea, but it's necessary to know that Traits uses lazy initialization of traits on an instance. So whether you introspect with your techniques or Adam's, those traits won't yet exist on tc, when you first look, because they have never been explicitly set or got. It's obvious that they have not yet been got. Not so obvious that they have not been set. Here's why:

The reason why i1 hasn't yet been set at that point is that you accidentally omitted the 'self' in:
i1 = 3
The reason why i2 hasn't yet been set is that giving it a default value of 5 is not the same as setting it to 5. The default value will not take effect until the first time you try to get its value, and have not yet explicitly set a value.

Regarding the log: I'm not certain, but I think that there is actually no log file unless the application has created one. If this is correct, then I think that the log message will be printed on the console. Others may have more precise information on this.

hth,
Jonathan

On Sun, Jun 3, 2012 at 3:15 PM, Adam Hughes <[hidden email]> wrote:
Any HasTrait subclass has several methods that should let you do this.  Check out the methods in the api.  http://code.enthought.com/projects/files/ets_api/enthought.traits.has_traits.HasTraits.html

For example, if you used the print_traits(), trait_names(), or traits() methods, I think you could access the information you need.

On Sun, Jun 3, 2012 at 3:48 PM, Ross Harder <[hidden email]> wrote:
All,

How should I check for a trait within a class?  Or is this just a flawed idea?
I've tried:

from traits.api import HasTraits, Int
class traitedclass(HasTraits):

  i1=Int  
  def __init__(self):
    i1=3
    self.add_trait('i2', Int(5))

tc=traitedclass()
print tc.__dict__.has_key('i1')
print tc.__dict__.has_key('i2')
print tc.i1
print tc.i2
print tc.__dict__.has_key('i1')
print tc.__dict__.has_key('i2')

And get this weird output, notice that printing the value changes it's presence in the instances dict.

MacBook-2:Python rharder$ python dict.py 
False
False
0
5
True
True







_______________________________________________
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



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

Re: checking for a trait

Jonathan March
In reply to this post by Ross Harder-2
This is not actually true. I hope that my message which just crossed yours will clarify why.

On Sun, Jun 3, 2012 at 4:06 PM, Ross Harder <[hidden email]> wrote:

Hi,
Thanks for your help.

I'm adding traits at runtime using add_trait.  This seems to make second class traits since they are not seen using any of these methods, unless you print them of course, which is weird.

Most of the methods of HasTraits operated by default only on the trait attributes explicitly defined in the class definition. They do not operate on trait attributes defined by way of wildcards or by calling add_trait().

Ross


From: Adam Hughes <[hidden email]>
To: Ross Harder <[hidden email]>; [hidden email]
Sent: Sunday, June 3, 2012 3:15 PM
Subject: Re: [Enthought-Dev] checking for a trait

Any HasTrait subclass has several methods that should let you do this.  Check out the methods in the api.  http://code.enthought.com/projects/files/ets_api/enthought.traits.has_traits.HasTraits.html

For example, if you used the print_traits(), trait_names(), or traits() methods, I think you could access the information you need.

On Sun, Jun 3, 2012 at 3:48 PM, Ross Harder <[hidden email]> wrote:
All,

How should I check for a trait within a class?  Or is this just a flawed idea?
I've tried:

from traits.api import HasTraits, Int
class traitedclass(HasTraits):

  i1=Int  
  def __init__(self):
    i1=3
    self.add_trait('i2', Int(5))

tc=traitedclass()
print tc.__dict__.has_key('i1')
print tc.__dict__.has_key('i2')
print tc.i1
print tc.i2
print tc.__dict__.has_key('i1')
print tc.__dict__.has_key('i2')

And get this weird output, notice that printing the value changes it's presence in the instances dict.

MacBook-2:Python rharder$ python dict.py 
False
False
0
5
True
True







_______________________________________________
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



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