parameter files

Discussion in 'Python' started by Russ, Sep 14, 2006.

  1. Russ

    Russ Guest

    I have a python module (file) that has a set of parameters associated
    with it. Let's say the module is called "code.py." I would like to keep
    the parameter assignments in a separate file so that I can save a copy
    for each "run" without having to save the entire code.py file. Let's
    say the parameter file is called "parameters.py."

    Normally, code.py would simply import the parameters.py file. However,
    I don't want the parameters to be accessible to any other file that
    imports the code.py file. to prevent such access, I preface the name of
    each parameter with an underscore. But then the parameters aren't even
    visible in code.py! So I decided to use "execfile" instead of import so
    the parameters are visible.

    That solved the problem, but I am just wondering if there is a better
    and/or more standard way to handle a situation like this. Any
    suggestions? Thanks.
     
    Russ, Sep 14, 2006
    #1
    1. Advertising

  2. Russ

    Guest

    Russ wrote:
    > I have a python module (file) that has a set of parameters associated
    > with it. Let's say the module is called "code.py." I would like to keep
    > the parameter assignments in a separate file so that I can save a copy
    > for each "run" without having to save the entire code.py file. Let's
    > say the parameter file is called "parameters.py."
    >
    > Normally, code.py would simply import the parameters.py file. However,
    > I don't want the parameters to be accessible to any other file that
    > imports the code.py file. to prevent such access, I preface the name of
    > each parameter with an underscore. But then the parameters aren't even
    > visible in code.py! So I decided to use "execfile" instead of import so
    > the parameters are visible.
    >
    > That solved the problem, but I am just wondering if there is a better
    > and/or more standard way to handle a situation like this. Any
    > suggestions? Thanks.


    I would try a configuration file, instead of a python module.
    See ConfigParser:
    <http://docs.python.org/lib/module-ConfigParser.html>.
    You can save values for each "run" in a separate [section].
    Execfile is a pretty big hammer for this.

    -- George
     
    , Sep 14, 2006
    #2
    1. Advertising

  3. Russ

    Russ Guest


    > I would try a configuration file, instead of a python module.
    > See ConfigParser:
    > <http://docs.python.org/lib/module-ConfigParser.html>.
    > You can save values for each "run" in a separate [section].
    > Execfile is a pretty big hammer for this.


    Hey, that looks interesting, but those docs don't do it for me. Can you
    point me to some more extensive examples of how to use ConfigParser?
    Thanks.
     
    Russ, Sep 14, 2006
    #3
  4. At Thursday 14/9/2006 01:10, Russ wrote:

    > > I would try a configuration file, instead of a python module.
    > > See ConfigParser:
    > > <http://docs.python.org/lib/module-ConfigParser.html>.
    > > You can save values for each "run" in a separate [section].
    > > Execfile is a pretty big hammer for this.

    >
    >Hey, that looks interesting, but those docs don't do it for me. Can you
    >point me to some more extensive examples of how to use ConfigParser?


    Just forget about interpolation and such; declare a section for each
    run in your config file:

    [run_name_one]
    a=123
    b=Test
    c=4.0

    [run_two]
    a=456
    b=Whatever
    c=0.1

    config = ConfigParser.ConfigParser()
    config.read(filename)
    a = config.getint('run_two','a') # a==456
    b = config.get('run_name_one','b') # b=='Test'
    section = 'run_two'
    c = config.getfloat(section,'c') # c==0.1


    Gabriel Genellina
    Softlab SRL





    __________________________________________________
    Preguntá. Respondé. Descubrí.
    Todo lo que querías saber, y lo que ni imaginabas,
    está en Yahoo! Respuestas (Beta).
    ¡Probalo ya!
    http://www.yahoo.com.ar/respuestas
     
    Gabriel Genellina, Sep 14, 2006
    #4
  5. Russ

    Russ Guest

    Thanks for the examples.

    I don't think you understood what I meant by a "run." All I meant is
    that I want to save the configuration, for reference purposes, that was
    used for a particular run. That way I can reproduce the results if
    necessary, and I can avoid confusion about which parameters were used
    to get particular results.

    I don't need a section for each run. I only need one set of parameters.
    I suppose I could use the sections for different modules or classes,
    each of which needs its own parameters.


    Gabriel Genellina wrote:
    > At Thursday 14/9/2006 01:10, Russ wrote:
    >
    > > > I would try a configuration file, instead of a python module.
    > > > See ConfigParser:
    > > > <http://docs.python.org/lib/module-ConfigParser.html>.
    > > > You can save values for each "run" in a separate [section].
    > > > Execfile is a pretty big hammer for this.

    > >
    > >Hey, that looks interesting, but those docs don't do it for me. Can you
    > >point me to some more extensive examples of how to use ConfigParser?

    >
    > Just forget about interpolation and such; declare a section for each
    > run in your config file:
    >
    > [run_name_one]
    > a=123
    > b=Test
    > c=4.0
    >
    > [run_two]
    > a=456
    > b=Whatever
    > c=0.1
    >
    > config = ConfigParser.ConfigParser()
    > config.read(filename)
    > a = config.getint('run_two','a') # a==456
    > b = config.get('run_name_one','b') # b=='Test'
    > section = 'run_two'
    > c = config.getfloat(section,'c') # c==0.1
    >
    >
    > Gabriel Genellina
    > Softlab SRL
    >
    >
    >
    >
    >
    > __________________________________________________
    > Preguntá. Respondé. Descubrí.
    > Todo lo que querías saber, y lo que ni imaginabas,
    > está en Yahoo! Respuestas (Beta).
    > ¡Probalo ya!
    > http://www.yahoo.com.ar/respuestas
     
    Russ, Sep 14, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    549
    Matteo
    Mar 29, 2007
  2. Stuart Redmann
    Replies:
    5
    Views:
    530
    Stuart Redmann
    Dec 14, 2007
  3. Replies:
    5
    Views:
    662
  4. Isti
    Replies:
    2
    Views:
    761
  5. Mister B
    Replies:
    8
    Views:
    600
    Nick Keighley
    Aug 26, 2010
Loading...

Share This Page