473,573 Members | 2,800 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ConfigParser: writes a list but reads a string?

It looks like ConfigParser will accept a list to be writing to the
*.ini file; but when reading it back in, it treats it as a string.

Example:

############### ############### #
import ConfigParser
def whatzit(thingna me, thing):
print thingname, "value:", thing
print thingname, "length:", len(thing)
print thingname, type(thing)

cfgfile = "cfgtest.in i"
config1 = ConfigParser.Co nfigParser()
config1.add_sec tion("test")

t1 = range(1,11)
config1.set("te st", "testlist", t1)
outfile=open(cf gfile,"w")
config1.write(o utfile)
outfile.close()

config2 = ConfigParser.Co nfigParser()
config2.read(cf gfile)
t2 = config2.get("te st","testlist ")

whatzit("t1", t1)
whatzit("t2", t2)

############### ############### #

Output is:

t1 value: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
t1 length: 10
t1 <type 'list'>
t2 value: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
t2 length: 31
t2 <type 'str'>

That is, t1 is a list of length 10, consisting of:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
and is written out; but t2 is read back in as a string
"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
of length 31.

It took me a while to figure this out, since they looked identical in
print statements.

Is there a pythonic way to read in a list from a .INI file with
ConfigParser? Is this expected behavior for ConfigParser? I would
not expect this conversion; rather, an exception when trying to write
the list if the list is not supported.

Jan 16 '06 #1
10 11770
Terry Carroll enlightened us with:
It looks like ConfigParser will accept a list to be writing to the
*.ini file; but when reading it back in, it treats it as a string.
It doesn't say so explicitly in the manual, but I did find this:

"""The values in defaults must be appropriate for the "%()s" string
interpolation." ""

So if the defaults go through %s, perhaps all values do.
Is there a pythonic way to read in a list from a .INI file with
ConfigParser?
I'd pickle() the list, and store that instead. Then you can unpicle()
it and regain your list.
I would not expect this conversion; rather, an exception when trying
to write the list if the list is not supported.


I agree with you.

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
Jan 16 '06 #2
ConfigObj will read and write list values.

You get all sorts of other advantages as well (nested subsections to
any depth), and the resulting code will be much simpler.

from configobj import ConfigObj
cfgfile = "cfgtest.in i"

cfg = ConfigObj(cfgfi le)
t1 = range(1,11)
# no *need* to create a subsection
cfg['test'] = {}
cfg['test']['testlist'] = t1
cfg.write()

To read it back in :

from configobj import ConfigObj
cfgfile = "cfgtest.in i"

cfg = ConfigObj(cfgfi le)
print cfg['test']['testlist']

http://www.voidspace.org.uk/python/configobj.html

All the best,

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml

Jan 16 '06 #3

"Terry Carroll" <ca*****@nosp am-tjc.com> wrote in message
news:de******** *************** *********@4ax.c om...
It looks like ConfigParser will accept a list to be writing to the
*.ini file; but when reading it back in, it treats it as a string.
ConfigParser is nasty because it does not really support type conversions
but still permits writing incompatible types to the configuration - i.e. the
conversions are generally one-way. It probably will croak on Floats in the
same way. The "string interpolation" works in mysterious ways too.

If you need ConfigParser to work then let every config item be strings and
convert manually as necessary.
Is there a pythonic way to read in a list from a .INI file with
ConfigParser? Is this expected behavior for ConfigParser?
Not in the way we normally use "expected" - but with ConfigParser it is to
be expected ;-)
I would
not expect this conversion; rather, an exception when trying to write
the list if the list is not supported.


If you want both text format config files and types that work then use
"ZConfig" instead.

PS:

ZConfig has no built-in way of writing a configuration - but this is not a
severe limitation: If the configuration is split into a "static-", which get
updated before a run and a "state-" set of files with things that are
changed by the application it is easier to keep the configuration consistent
anyway (it is not so hard to dump a dict into a ZConfig format).
Jan 16 '06 #4
Note that ConfigObj also supports type conversion via the ``validate``
module which comes with it. This validates a config file against an
(optional) ``configspec`` which you supply, and does all the
conversion. It reports any errors it encounters.

Syntax for reading and writing is a standard 'ini-like' format with an
extended syntax for nested sections. The major differences are that you
can have values in the root section, and you should use '=' as the
``keyword = value`` divider.

All the best,

Fuzzyman
http://www.voidspace.org.uk/python/configobj.html

Jan 16 '06 #5
Terry Carroll wrote:
It looks like ConfigParser will accept a list to be writing to the
*.ini file; but when reading it back in, it treats it as a string.

Example:

############### ############### #
import ConfigParser
def whatzit(thingna me, thing):
print thingname, "value:", thing
print thingname, "length:", len(thing)
print thingname, type(thing)

cfgfile = "cfgtest.in i"
config1 = ConfigParser.Co nfigParser()
config1.add_sec tion("test")

t1 = range(1,11)
config1.set("te st", "testlist", t1)
outfile=open(cf gfile,"w")
config1.write(o utfile)
outfile.close()

config2 = ConfigParser.Co nfigParser()
config2.read(cf gfile)
t2 = config2.get("te st","testlist ")

whatzit("t1", t1)
whatzit("t2", t2)

############### ############### #

Output is:

t1 value: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
t1 length: 10
t1 <type 'list'>
t2 value: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
t2 length: 31
t2 <type 'str'>

That is, t1 is a list of length 10, consisting of:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
and is written out; but t2 is read back in as a string
"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
of length 31.

It took me a while to figure this out, since they looked identical in
print statements.

Is there a pythonic way to read in a list from a .INI file with
ConfigParser? Is this expected behavior for ConfigParser? I would
not expect this conversion; rather, an exception when trying to write
the list if the list is not supported.


To read lists from .INI files I use following:

listvalues=INI. get(section, option).split(' ,')

where INI is an instance of ConfigParser

There is the problem of if list items contain commas.

-Larry Bates
Jan 17 '06 #6
On Tue, 17 Jan 2006 09:11:24 -0600, Larry Bates
<la*********@we bsafe.com> wrote:

To read lists from .INI files I use following:

listvalues=INI .get(section, option).split(' ,')

where INI is an instance of ConfigParser

There is the problem of if list items contain commas.


Thanks; that's basically pretty close to what I ended up with:

t3string=config 2.get("test","t estlist")
t3 = [int(x) for x in t3string[1:-1].split(",")]
whatzit("t3", t3)

Gives:

t3 value: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
t3 length: 10
t3 <type 'list'>

i.e., the same as the original list of integers that was written out.

I know, this just assumes the input will be a list in [] brackets,
with no verification of that. That's okay, because it's just an app
for me, not for any outside user. If anyone else was using this, I'd
extract the list with a regexp.

I just want to be able to edit the config file outside of the program
to change things. (Sybren, that's why Pickle was not appealing to me.)

Thanks also to Sybren, Fuzzyman and Frithiof for their contributions.
Nice to know I'm not doing anything wrong, anyway.

Jan 18 '06 #7
I'm interested in the same sort of config file issues.

Unfortunately the restricted execution has fallen out of favor. My
preferred solution would be to have a configEval() function that is
just like the regular Python eval() but only accepts a subset of the
python language, e.g. creating the basic built in datatypes and
variable assignment and perhaps a short list of safe library functions:

favoriteColors = [ 'red', 'blue', 'orange']
props = {'fish': 7, 'cow': 'milk'}
# a comment
otherstuff = (props, favoritColors, 7)

While currently I just write my config file in python and eval() it,
this sucks from a security standpoint (e.g. someone could replace the
config file with a python program that deletes my hard drive). Thanks
for the configObj suggestion, I'll look into that.

making the config file XML and using xml.dom is another option,
although XML is a bit ugly to edit by hand.
--jfc

Jan 20 '06 #8

funkyj wrote:
I'm interested in the same sort of config file issues.

Unfortunately the restricted execution has fallen out of favor. My
preferred solution would be to have a configEval() function that is
just like the regular Python eval() but only accepts a subset of the
python language, e.g. creating the basic built in datatypes and
variable assignment and perhaps a short list of safe library functions:

favoriteColors = [ 'red', 'blue', 'orange']
props = {'fish': 7, 'cow': 'milk'}
# a comment
otherstuff = (props, favoritColors, 7)

While currently I just write my config file in python and eval() it,
this sucks from a security standpoint (e.g. someone could replace the
config file with a python program that deletes my hard drive). Thanks
for the configObj suggestion, I'll look into that.

making the config file XML and using xml.dom is another option,
although XML is a bit ugly to edit by hand.
On the cookbook there is a serialize and de-serialize function that
works with the basic datatypes, but doesn't have the associated risks
of using eval.

ConfigObj is still a better solution IMHO. :-)

All the best,

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml

--jfc


Jan 21 '06 #9
funkyj wrote:
making the config file XML and using xml.dom is another option,
although XML is a bit ugly to edit by hand.
--jfc

I am seriously intrigued by ConfigObj. I am currently using an crude
improvisation involving tab-delimited fields to store metadata for
recordings -- not configuration data -- in text files. I had been planning
to convert to XML, but now I am wondering whether ConfigObj would be
easier. I would like for the metadata files to be editable, but editing
does not have to be easy as it needs to be done rarely. I've never used
XML, so I am wondering whether there are other tradeoffs between that
approach and ConfigObj that I should be aware of. I was thinking of XML
mainly to have a more robust format. For example, it would be nice if it
were possible to add fields without obsoleting early versions of the
reader. Crossplatform compatibility is also desirable.
--
Jeffrey Barish

Jan 22 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2510
by: gbgbgbgb | last post by:
Hi, I have a definition bool operator<(string s_s, string s_t) { .... } and a variable list<string> concomp;
2
5060
by: s | last post by:
I'm getting compile errors on the following code: <code> #include <iostream> #include <fstream> #include <list> #include <string> using namespace std;
4
9096
by: blrmaani | last post by:
Here is what I want: string s1 = "This is a list of string"; list<string> s2 = s1.some_method(); Now, I should be able to traverse list s2 and get each member ( which is of type 'string' ). I know that this can be achieved using strtok. But I was wondering
3
8483
by: aquanutz | last post by:
Ok, I have a list of strings (list<string> stringList) that I want to sort alphabetcially, only "sort(stringList.begin(), stringList.end()); ) does not work. Any insight would be helpful. Thanks!
9
4419
by: incredible | last post by:
how to sort link list of string
2
1420
by: Gandalf | last post by:
hi, there is some function to verify that a string is already in the list or i have to make a cicle? thank u
4
13198
by: shapper | last post by:
Hello, How can I transform a Generic List(Of String) to a string as follows: "value1,value2,value3,value4, ..." Thanks, Miguel
12
4362
by: aparnakakkar2003 | last post by:
can any one tell me if I give the followiing string in input: ABC abc BBC then how I can get ABC abc BBC
1
3759
by: cakeathon | last post by:
I'm working my way through the accelerated C++ book, and exercise 10-5, & 10-6 have me stuck I have the follwing class in a header file: class String_list { public: String_list(); void addString (const std::string s); void print_strings () const;
9
2782
by: shapper | last post by:
Hello, How can I filter a List(Of String)? I need to get the list elements which start with the letters contained in the variable Text. Thanks, Miguel
0
8027
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8202
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7788
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6421
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5594
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5292
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3733
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2216
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1041
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.