Discussion:
ConfigParser items method
Gordon Williams
2003-10-06 20:04:25 UTC
Permalink
Hi All,

There is a consistency problem between the RawConfigParser and the
ConfigParser and SafeConfigParser with the items method. In the first case
a list of tuples is returned and in the second two a generator is returned.
This is quite confusing and I thought that this was a bug, but the docs
indicate that this is what is supposed to happen.

An items method that returned a list of tuples as it does in the
RawconfigParser would be a useful method to have for both ConfigParser and
SafeConfigParser.

The RawConfigParser docs say that items should return a list:

items( section)

Return a list of (name, value) pairs for each option in the given section.


The ConfigParser docs say that items should return a generator:

items( section[, raw[, vars]])

Create a generator which will return a tuple (name, value) for each option
in the given section. Optional arguments have the same meaning as for the
get() method. New in version 2.3.
Config.config
<ConfigParser.RawConfigParser instance at 0x00E0DAD0>
Config.config.items("personal")
[('age', '21'), ('company', 'Aztec'), ('name', 'karthik')]
Config.config
<ConfigParser.SafeConfigParser instance at 0x00DB1738>
Config.config.items("personal")
<generator object at 0x00DC3350>
... print item
...
('age', '21')
('company', 'Aztec')
('name', 'karthik')
Config.config
<ConfigParser.ConfigParser instance at 0x00E0D378>
Config.config.items("personal")
<generator object at 0x00DB1738>
... print item
...
('age', '21')
('company', 'Aztec')
('name', 'karthik')

It doesn't make sense to me that the same method should return different
objects. Maybe another name for ConfigParser and SafeConfigParser would be
appropriate to indicate that a generator was being returned.

Regards,

Gordon Williams
Fred L. Drake, Jr.
2003-10-06 20:12:45 UTC
Permalink
Post by Gordon Williams
An items method that returned a list of tuples as it does in the
RawconfigParser would be a useful method to have for both ConfigParser and
SafeConfigParser.
I'm happy for these to always return a list. I probably changed this
around when I refactored the classes into raw/classic/safe flavors
without really thinking about it.

If there are no objections, feel free to file a bug report on
SourceForge and assign it to me.


-Fred
--
Fred L. Drake, Jr. <fdrake at acm.org>
PythonLabs at Zope Corporation
Gordon Williams
2003-10-06 20:35:48 UTC
Permalink
Hi Fred,

I cant log into sourceforge bugs. I will leave it in your capable hands.

Regards,

Gordon Williams

----- Original Message -----
From: "Fred L. Drake, Jr." <***@acm.org>
To: "Gordon Williams" <***@cyberus.ca>
Cc: <python-***@python.org>
Sent: Monday, October 06, 2003 4:12 PM
Subject: Re: [Python-Dev] ConfigParser items method
Post by Fred L. Drake, Jr.
Post by Gordon Williams
An items method that returned a list of tuples as it does in the
RawconfigParser would be a useful method to have for both ConfigParser and
SafeConfigParser.
I'm happy for these to always return a list. I probably changed this
around when I refactored the classes into raw/classic/safe flavors
without really thinking about it.
If there are no objections, feel free to file a bug report on
SourceForge and assign it to me.
-Fred
--
Fred L. Drake, Jr. <fdrake at acm.org>
PythonLabs at Zope Corporation
Fred L. Drake, Jr.
2003-10-06 20:41:42 UTC
Permalink
Post by Gordon Williams
I cant log into sourceforge bugs. I will leave it in your capable hands.
Report filed:

http://sourceforge.net/tracker/index.php?func=detail&aid=818861&group_id=5470&atid=105470


-Fred
--
Fred L. Drake, Jr. <fdrake at acm.org>
PythonLabs at Zope Corporation
Gordon Williams
2003-10-07 18:11:36 UTC
Permalink
Hi Fred,

A couple of other things about the ConfigParser module that I find a bit
strange and I'm not sure that is intended behaivior.


1. Option gets converted to lower case and therefore is not case sensitive,
but section is case sensitive. I would have thought that both would be or
neither would be case sensitive. (My preference would be that neither would
be case sensitive.)

example if I have a config.txt file with:
[File 1]
databaseADF adsfa:octago DASFDAS
user:Me
password:blank

then this gets written out it is (were databaseADF is now databaseadf):
[File 1]
databaseadf adsfa = octago DASFDAS
password = blank
user = Me
Config.config
<ConfigParser.SafeConfigParser instance at 0x010EEE40>
Config.config.options('file 1')
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "E:\PROGRA~1\PYTHON23\lib\ConfigParser.py", line 240, in options
raise NoSectionError(section)
NoSectionError: No section: 'file 1'

But using 'dataBASEadf adsfa' instead of 'databaseADF adsfa' or
Config.config.get('File 1', 'dataBASEadf adsfa')
'octago DASFDAS'

The differences in handling the option and section are annoying and should
at least be described in the docs if they cant be changed.

2. SafeConfigParser is the recommended ConfigParser in the docs. I'm not
sure what is meant be safe. When values are read in from a file they are
first converted to strings. This is not true for values set within the
code.
Config.config.set('File 1', 'test', 2)
Config.config.get('File 1', 'test')
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "E:\PROGRA~1\PYTHON23\lib\ConfigParser.py", line 518, in get
return self._interpolate(section, option, value, d)
File "E:\PROGRA~1\PYTHON23\lib\ConfigParser.py", line 576, in _interpolate
self._interpolate_some(option, L, rawval, section, vars, 1)
File "E:\PROGRA~1\PYTHON23\lib\ConfigParser.py", line 585, in
_interpolate_some
p = rest.find("%")
AttributeError: 'int' object has no attribute 'find'

Likely the value assigned to the object should be first converted to a
string before it is stored. The same thing happens if a dict or float is
c= Config.SafeConfigParser({'test':{'1':"One",'2':"Two"}, 'foo':2.3})
c.write(sys.stdout)
[DEFAULT]
test = {'1': 'One', '2': 'Two'}
foo = 2.3
c.get('DEFAULT', 'test')
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "E:\PROGRA~1\PYTHON23\lib\ConfigParser.py", line 518, in get
return self._interpolate(section, option, value, d)
File "E:\PROGRA~1\PYTHON23\lib\ConfigParser.py", line 576, in _interpolate
self._interpolate_some(option, L, rawval, section, vars, 1)
File "E:\PROGRA~1\PYTHON23\lib\ConfigParser.py", line 585, in
_interpolate_some
p = rest.find("%")
AttributeError: 'dict' object has no attribute 'find'
c.get('DEFAULT', 'foo')
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "E:\PROGRA~1\PYTHON23\lib\ConfigParser.py", line 518, in get
return self._interpolate(section, option, value, d)
File "E:\PROGRA~1\PYTHON23\lib\ConfigParser.py", line 576, in _interpolate
self._interpolate_some(option, L, rawval, section, vars, 1)
File "E:\PROGRA~1\PYTHON23\lib\ConfigParser.py", line 585, in
_interpolate_some
p = rest.find("%")
AttributeError: 'float' object has no attribute 'find'
If we set raw= True, then we get back an object (<type 'float'>) and not a
c.get('DEFAULT', 'foo', raw= True)
2.2999999999999998
c.get('DEFAULT', 'junk', vars= {'junk': 99})
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "E:\PROGRA~1\PYTHON23\lib\ConfigParser.py", line 518, in get
return self._interpolate(section, option, value, d)
File "E:\PROGRA~1\PYTHON23\lib\ConfigParser.py", line 576, in _interpolate
self._interpolate_some(option, L, rawval, section, vars, 1)
File "E:\PROGRA~1\PYTHON23\lib\ConfigParser.py", line 585, in
_interpolate_some
p = rest.find("%")
AttributeError: 'int' object has no attribute 'find'


One last comment is that 'interpolation' is a bit confusing in the docs.
Maybe 'substitution' would be a better word.

Thanks,

Gordon Williams
Fred L. Drake, Jr.
2003-10-07 18:48:13 UTC
Permalink
Post by Gordon Williams
Hi Fred,
A couple of other things about the ConfigParser module that I find a bit
strange and I'm not sure that is intended behaivior.
1. Option gets converted to lower case and therefore is not case sensitive,
but section is case sensitive. I would have thought that both would be or
neither would be case sensitive. (My preference would be that neither would
be case sensitive.)
And mine would be that both are case sensitive! ;-) I guess that's
why we have optionxform to override the transform for option names at
least.

Ideally, both option and section names should be transformed, but the
specific transforms should be independently pluggable. I'm not
adverse to a patch which adds a sectionxform, but don't have the time
or motivation to change it myself. Feel free to post a patch to
SourceForge and assign it to me for review. Documentation and tests
are required.

[...examples elided...]
Post by Gordon Williams
The differences in handling the option and section are annoying and should
at least be described in the docs if they cant be changed.
Please suggest specific changes; I don't expect to have much time for
ConfigParser anytime soon, so specific changes (esp. a patch if you
can deal with the LaTeX) would be greatly appreciated.
Post by Gordon Williams
2. SafeConfigParser is the recommended ConfigParser in the docs. I'm not
sure what is meant be safe. When values are read in from a file they are
first converted to strings. This is not true for values set within the
code.
True. I'd suggest that at most, a typecheck for a value being a
string could be added to the code; the documentation may need further
elaboration.

The "Safe" was intended to refer specifically to the string
substitution algorithm; it uses a more careful implementation that
isn't as subject to weird border conditions. Again, the documentation
may require improvements.
...
Post by Gordon Williams
Likely the value assigned to the object should be first converted to a
string before it is stored.
Or an exception should be raised, placing the burden squarely on the
caller to do the right thing, instead of guessing what the right thing
is.
Post by Gordon Williams
One last comment is that 'interpolation' is a bit confusing in the docs.
Maybe 'substitution' would be a better word.
Agreed.

I'd like to suggest two things:

- Get a SourceForge account and file a bug report (you don't need to
be on the Python project, just having an account is sufficient).

- Take a look at some of the alternate configuration libraries; they
may be more suited to your requirements. My current favorite is
ZConfig, for which a new version is expected in the next week or so:

http://www.python.org/pypi?%3Aaction=search&name=ZConfig

But I might be biased about this one. ;-)


-Fred
--
Fred L. Drake, Jr. <fdrake at acm.org>
PythonLabs at Zope Corporation
Loading...