setting tratis

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

setting tratis

Ross Harder
In the past I have written python scripts and classes that "read"
parameter configuration files by just executing them

For instance:

class a:
        def __init__(self, paramfile):

                execfile(paramfile, self.__dict__)

if __name__=="__main__":
        an=a('params.py')
        print an.param1
        print an.param2


so params.py will have

param1=4
param2="outputfileanme"


This is really nice because the configuration files become python
scripts and the user can do math or string operations or whatever.  As
long as the correct values get set for the program execution in the
params.py file, everything is great.

I've noticed that if the class is traited and particular traits are
set in the execfile command that the _traitname_changed methods are
not run.  The traits are indeed set during the execfile, but the
methods that are supposed to run when they change do not get executed.
 Can this be made to happen?

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

Re: setting tratis

Ross Harder
I've managed to figure this out on my own.  Discovered the
__getstate__ and __setstate__ methods of HasTraits.

The following works as I expect:

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

class a(HasTraits):

        param1=Int
        param2=Str
#       def __init__(self, paramfile):
#               HasTraits.__init__(self)
#               execfile(paramfile, self.__dict__)

        def _param1_changed(self, old, new):
                print "setting param1", old, new
        def _param2_changed(self, old, new):
                print "setting param2", old, new

        def execfile(self, filename):
                state=self.__getstate__()
                print state
                execfile(filename, self.__dict__, state)
                print state
                self.__setstate__(state)

if __name__=="__main__":
#       an=a('params.py')
        an=a()
        an.execfile('params.py')

        an.configure_traits()


On Tue, May 11, 2010 at 1:36 PM, Ross Harder <[hidden email]> wrote:

> In the past I have written python scripts and classes that "read"
> parameter configuration files by just executing them
>
> For instance:
>
> class a:
>        def __init__(self, paramfile):
>
>                execfile(paramfile, self.__dict__)
>
> if __name__=="__main__":
>        an=a('params.py')
>        print an.param1
>        print an.param2
>
>
> so params.py will have
>
> param1=4
> param2="outputfileanme"
>
>
> This is really nice because the configuration files become python
> scripts and the user can do math or string operations or whatever.  As
> long as the correct values get set for the program execution in the
> params.py file, everything is great.
>
> I've noticed that if the class is traited and particular traits are
> set in the execfile command that the _traitname_changed methods are
> not run.  The traits are indeed set during the execfile, but the
> methods that are supposed to run when they change do not get executed.
>  Can this be made to happen?
>
> Ross
>
_______________________________________________
Enthought-Dev mailing list
[hidden email]
https://mail.enthought.com/mailman/listinfo/enthought-dev