By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,591 Members | 1,155 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,591 IT Pros & Developers. It's quick & easy.

Comments in ConfigParser module

P: n/a
Hi list:

I have run across a situation with ConfigParser Module. It refers to
the comments in the configuration filed. According to the
http://docs.python.org/dev/lib/module-ConfigParser.html it states that
lines starting with "#" and ";" are ignored.
So lines like:
; comment
# comment
are considered comments. So far so good.

The module also allows the comments to appear in the same line as the
"name = value" constructs. The only difference being that this is only
possible with ";" and not with "#" character. I did not see this in the
documentation but this is how it is behaving.
So while the following line takes the comment as a comment.
*name = value ; comment*
the next line does not result in the same behavior.
*name = value # comment*
the name element would have "value # comment" as its value, when what I
really want is for "comment" to be taken as a comment.

QUESTION...So the question is:
Can you use "#" and ";" as comment characters? and if so why does the
"#" not apply for the same situations as the ";"? I'm working with fc7
(I synced it sometime last week) and python 2.5 (r25:51908, Feb 13 2007,
09:13:20)

Any comment greatly appreciated....
Just for reference:
On the RFC 822 <http://www.faqs.org/rfcs/rfc822.html(a document
referenced in the documentation) there is a mention of ";" being used as
comment character but not necessarily at the beginning of the line.
So anything that comes after the ";" is ignored.
name = value ; comment (";comment" would be ignored)

Apr 6 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Joel Andres Granados wrote:
Hi list:
Any comment greatly appreciated....
Very clever.
Apr 6 '07 #2

P: n/a
Joel Andres Granados wrote:
The module also allows the comments to appear in the same line as the
"name = value" constructs. The only difference being that this is only
possible with ";" and not with "#" character. I did not see this in the
documentation but this is how it is behaving.
Yes, it's not documented. There is only a comment in the source code:

# ';' is a comment delimiter only if it
follows
# a spacing character

I think it's either a bug in the code or in the documentation.
QUESTION...So the question is:
Can you use "#" and ";" as comment characters? and if so why does the
"#" not apply for the same situations as the ";"?
If you follow the documentation, comments are ONLY allowed to start a
line.
The actual implementation discards any text following a <space>;
sequence.
Just for reference:
On the RFC 822 <http://www.faqs.org/rfcs/rfc822.html(a document
referenced in the documentation) there is a mention of ";" being used as
comment character but not necessarily at the beginning of the line.
RFC822 uses ";" to include comments in the syntax rules, not for
comments in the actual message headers (parenthesis are used there).

--
Gabriel Genellina

Apr 7 '07 #3

P: n/a
Joel Granados wrote:
>

On 4/6/07, *James Stroud* <js*****@mbi.ucla.edu
<mailto:js*****@mbi.ucla.edu>wrote:

Joel Andres Granados wrote:
Hi list:
Any comment greatly appreciated....

Very clever.
--
http://mail.python.org/mailman/listinfo/python-list
<http://mail.python.org/mailman/listinfo/python-list>
clever ???
Well I would expect that the ";" character to behave the same way that
the "#", as they are both characters that come before a comment. But it
doesn't. So I'm missing something here. Why is the different behavior
clever?

--
Joel Andrés Granados
MedellÃ*n Colombia
--
jg******@eafit.edu.co <mailto:jg******@eafit.edu.co>
jo***********@ieee.org <mailto:jo***********@ieee.org>
I was referring to his asking for "comments" in a post about "comments".
I thought this was a clever way to conclude. I have no idea why the
behavior of the module is the way it is. Seems like a bug to me, but I
have not read the official .ini file specification, so I'm not sure its
working correctly.

Cheers,

James
Apr 9 '07 #4

P: n/a
En Sun, 08 Apr 2007 06:17:46 -0300, Joel Granados
<jo***********@gmail.comescribió:
On 7 Apr 2007 13:19:06 -0700, Gabriel Genellina <ga*******@yahoo.com.ar>
wrote:
>>
Joel Andres Granados wrote:
The module also allows the comments to appear in the same line as the
"name = value" constructs. The only difference being that this is
only
possible with ";" and not with "#" character. I did not see this in
the
documentation but this is how it is behaving.

Yes, it's not documented. There is only a comment in the source code:

# ';' is a comment delimiter only if it
follows
# a spacing character

I think it's either a bug in the code or in the documentation.

Yep, IMO its a weirdness in the behavior that is not documented.
It was a bit hard, but I finally found the original Windows ini-file
specification at <http://www.microsoft.com/technet/archive/wfw/2_ch6.mspx>
where you can see ; used as comment delimiter.
ConfigParser tries to handle both RFC822-like headers (with name: value,
including continuation lines, but ignoring almost everything else in the
syntax) and ini files (with ; as comments), plus some unique features like
# comments and "rem" lines removal (?). That is, it tries to cover a lot
of formats without a clear specification.
Add wrong defaults usage, and the need for SafeConfigParser, and some
other gotchas... then switch to use alternative implementations like
ConfigObj <http://www.voidspace.org.uk/python/configobj.html>

--
Gabriel Genellina

Apr 9 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.