Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #33431 > unrolled thread
| Started by | chip9munk <"chip9munk[SSSpAm"@gmail.com> |
|---|---|
| First post | 2012-11-16 13:06 +0100 |
| Last post | 2012-11-16 08:41 -0800 |
| Articles | 11 — 6 participants |
Back to article view | Back to comp.lang.python
editing conf file chip9munk <"chip9munk[SSSpAm"@gmail.com> - 2012-11-16 13:06 +0100
Re: editing conf file chip9munk <"chip9munk[SSSpAm"@gmail.com> - 2012-11-16 13:15 +0100
Re: editing conf file rusi <rustompmody@gmail.com> - 2012-11-16 04:35 -0800
Re: editing conf file chip9munk <"chip9munk[SSSpAm"@gmail.com> - 2012-11-16 13:48 +0100
Re: editing conf file Thomas Bach <thbach@students.uni-mainz.de> - 2012-11-16 14:04 +0100
Re: editing conf file chip9munk <"chip9munk[SSSpAm"@gmail.com> - 2012-11-16 14:28 +0100
Re: editing conf file Tim Chase <python.list@tim.thechases.com> - 2012-11-16 07:43 -0600
Re: editing conf file Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2012-11-16 14:02 +0100
Re: editing conf file chip9munk <"chip9munk[SSSpAm"@gmail.com> - 2012-11-16 14:27 +0100
Re: editing conf file Roy Smith <roy@panix.com> - 2012-11-16 09:08 -0500
Re: editing conf file rusi <rustompmody@gmail.com> - 2012-11-16 08:41 -0800
| From | chip9munk <"chip9munk[SSSpAm"@gmail.com> |
|---|---|
| Date | 2012-11-16 13:06 +0100 |
| Subject | editing conf file |
| Message-ID | <k85a7i$sjn$1@poprovec.arnes.si> |
Hi all!
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.
Now the problem is I do not know how to edit the conf file...
let us say that I calculate something and would like to save it in the
conf file so that it is set for the next run of some program.
How do I edit a specific value in the conf file? (I would like to avoid
editing txt if possible)...
I should also mention that I use Python 3.. so some of the solutions I
came across are not compatible...
Thanks!!
[toc] | [next] | [standalone]
| From | chip9munk <"chip9munk[SSSpAm"@gmail.com> |
|---|---|
| Date | 2012-11-16 13:15 +0100 |
| Message-ID | <k85aoe$uot$1@poprovec.arnes.si> |
| In reply to | #33431 |
ok, I've got it: http://docs.python.org/3.1/library/configparser.html works like a charm! Sorry for the unnecessary question. :/
[toc] | [prev] | [next] | [standalone]
| From | rusi <rustompmody@gmail.com> |
|---|---|
| Date | 2012-11-16 04:35 -0800 |
| Message-ID | <996a6d6c-fdb2-40a0-bd5c-ada2ba35af78@i2g2000pbi.googlegroups.com> |
| In reply to | #33432 |
On Nov 16, 5:15 pm, chip9munk <"chip9munk[SSSpAm"@gmail.com> wrote: > ok, I've got it:http://docs.python.org/3.1/library/configparser.html > > works like a charm! > > Sorry for the unnecessary question. :/ Not an issue. And there may be better options (allows nested sections) http://www.voidspace.org.uk/python/configobj.html A more broadbased comparison http://wiki.python.org/moin/ConfigParserShootout
[toc] | [prev] | [next] | [standalone]
| From | chip9munk <"chip9munk[SSSpAm"@gmail.com> |
|---|---|
| Date | 2012-11-16 13:48 +0100 |
| Message-ID | <k85cnl$84o$1@poprovec.arnes.si> |
| In reply to | #33433 |
On 11/16/2012 1:35 PM, rusi wrote: > And there may be better options (allows nested sections) > http://www.voidspace.org.uk/python/configobj.html > but it does not seem to work with python 3 I have an issue... configparser has four functions: get, getboolean, getfloat and getint. how do I get list from cfg file?! any ideas?
[toc] | [prev] | [next] | [standalone]
| From | Thomas Bach <thbach@students.uni-mainz.de> |
|---|---|
| Date | 2012-11-16 14:04 +0100 |
| Message-ID | <mailman.3749.1353071521.27098.python-list@python.org> |
| In reply to | #33435 |
On Fri, Nov 16, 2012 at 01:48:49PM +0100, chip9munk wrote:
> configparser has four functions: get, getboolean, getfloat and getint.
>
> how do I get list from cfg file?!
AFAIK you have to parse the list yourself. Something like
my_list = [ s.strip() for s in cp.get('section', 'option').split(',') ]
if you use a comma as a separator.
Have a look at YAML if this is not enough for you, as I think lists
are supported there. Haven't had a look myself though, yet.
Regards,
Thomas Bach.
[toc] | [prev] | [next] | [standalone]
| From | chip9munk <"chip9munk[SSSpAm"@gmail.com> |
|---|---|
| Date | 2012-11-16 14:28 +0100 |
| Message-ID | <k85f24$ihn$2@poprovec.arnes.si> |
| In reply to | #33436 |
On 11/16/2012 2:04 PM, Thomas Bach wrote:
> On Fri, Nov 16, 2012 at 01:48:49PM +0100, chip9munk wrote:
>> configparser has four functions: get, getboolean, getfloat and getint.
>>
>> how do I get list from cfg file?!
>
> AFAIK you have to parse the list yourself. Something like
>
> my_list = [ s.strip() for s in cp.get('section', 'option').split(',') ]
>
> if you use a comma as a separator.
yes i guess this is needed, thanks!
> Have a look at YAML if this is not enough for you, as I think lists
> are supported there. Haven't had a look myself though, yet.
>
I will check it out!
Thank you!
[toc] | [prev] | [next] | [standalone]
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2012-11-16 07:43 -0600 |
| Message-ID | <mailman.3751.1353073345.27098.python-list@python.org> |
| In reply to | #33435 |
On 11/16/12 07:04, Thomas Bach wrote:
> On Fri, Nov 16, 2012 at 01:48:49PM +0100, chip9munk wrote:
>> configparser has four functions: get, getboolean, getfloat and getint.
>>
>> how do I get list from cfg file?!
>
> AFAIK you have to parse the list yourself. Something like
>
> my_list = [ s.strip() for s in cp.get('section', 'option').split(',') ]
>
> if you use a comma as a separator.
For slightly more complex option text, you can use the CSV module to
do the heavy lifting, so if you have something like
[section]
option="one, one, one",two,3
then you can have Python give you
my_list = next(csv.reader([cp.get("section", "option")]))
or alternatively use the shlex module and separate them like shell
options:
[section]
option="one, one, one" two 3
then do
my_list = list(shlex.shlex(cp.get("section", "option")))
Or yet one more way using Python list syntax for literals:
[section]
option=["one, one, one", "two", 3]
and get them with
my_list = ast.literal_eval(cp.get("section", "option))
Lots of fun (and batteries-included) ways depending on how you want
to represent the list in the config file and what sorts of data it
can contain.
-tkc
[toc] | [prev] | [next] | [standalone]
| From | Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> |
|---|---|
| Date | 2012-11-16 14:02 +0100 |
| Message-ID | <54qhn9-u85.ln1@satorlaser.homedns.org> |
| In reply to | #33431 |
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.
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.
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.
> 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 Anyhow, concerning the link above, replace the 2 with
a 3 and skip to section 14.2.
Uli
[toc] | [prev] | [next] | [standalone]
| From | chip9munk <"chip9munk[SSSpAm"@gmail.com> |
|---|---|
| Date | 2012-11-16 14:27 +0100 |
| Message-ID | <k85f02$ihn$1@poprovec.arnes.si> |
| In reply to | #33437 |
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!
[toc] | [prev] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2012-11-16 09:08 -0500 |
| Message-ID | <roy-97D44D.09081116112012@news.panix.com> |
| In reply to | #33438 |
Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> wrote: > 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. chip9munk <"chip9munk[SSSpAm"@gmail.com> wrote: > 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... I've been using django for the past couple of years, and I have to say I'm really addicted to their style of executable config files. The ability to put conditional logic in your settings.py file is extremely powerful. Even simple stuff like: DEBUG = songza.config['build_type'] != 'production' adds value. But, yes, Ulrich is 100% correct that it can lead to code injection attacks if you allow reading configs from untrusted sources. Like all powerful tools, it needs to be used with care. These days, if I was writing something that needed a config file and I didn't want to do "import settings" for whatever reason, I would go with YAML. It seems to give an attractive mix of: * supporting complex data structures * easy to for humans to hand-edit * easy for humans to read * safe from code injection attacks
[toc] | [prev] | [next] | [standalone]
| From | rusi <rustompmody@gmail.com> |
|---|---|
| Date | 2012-11-16 08:41 -0800 |
| Message-ID | <f39f5d71-8197-437c-95f4-9f601a436032@qi10g2000pbb.googlegroups.com> |
| In reply to | #33441 |
On Nov 16, 7:08 pm, Roy Smith <r...@panix.com> wrote: > These days, if I was writing something that needed a config file and I > didn't want to do "import settings" for whatever reason, I would go with > YAML. It seems to give an attractive mix of: > > * supporting complex data structures > * easy to for humans to hand-edit > * easy for humans to read > * safe from code injection attacks +1 except for a caveat on the last: Use safe_load and safe_dump. dump and load are vulnerable to code injection attacks
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web