parameter files

gry at ll.mit.edu gry at ll.mit.edu
Thu Sep 14 03:29:53 CEST 2006


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




More information about the Python-list mailing list