On 11/16/2012 2:02 PM, Ulrich Eckhardt wrote:
Am 16.11.2012 13:06, schrieb chip9munk:
I would like to use conf file to get all the variables in my code. And
it works great. I use the following (simple example):
execfile("example.conf", config)
print config["value1"]
and it works like a charm.
This works, but in general importing configuration data by loading and
executing code is a questionable approach. The problem is in particular
that the code parser is always more strict with the syntax than a
configuration file should be. Also, it presents the danger of code
injection, especially when exec'ing or importing untrusted code.
huh... ok, the thing is that there will actually be no code in the
config file, just some variables and values.. it will be more like a
"setting file"... so no execution of the config file is necessary, just
getting and setting variables...
That said, if you really want full programmability inside that
configuration and are aware of the implications, you can do that. In
that case, I would rather call this a Python module though and instead
"from settings.py import *" to import any setting from this module (this
is similar to exec(), but less hack-ish). I use something similar to
import settings for automated tests, but still wouldn't recommend it for
general use.
thank you for the tip!
If you don't want that, use a configuration file parser instead. Python
comes with one, see the section "13.2 Configuration file parser" at
http://docs.python.org/2/library/, which can both read and write simple
configuration files.
yes I will use it
I should also mention that I use Python 3.. so some of the solutions I
came across are not compatible...
No you don't, Above code clearly uses a print statement instead of a
print function. :P
Yes i do :) i just did not c/p code from my script but from some webpage
:)) i have print (config["value1"])
Anyhow, concerning the link above, replace the 2 with
a 3 and skip to section 14.2.
thank you i am using configparser... the only problem as i mention in
another post is that it reads lists as string... so some additional
parsing is necessary..
thanks!
--
http://mail.python.org/mailman/listinfo/python-list